LLVM 22.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"
37
38using namespace llvm;
39
40#define GEN_CHECK_COMPRESS_INSTR
41#include "RISCVGenCompressInstEmitter.inc"
42
43#define GET_INSTRINFO_CTOR_DTOR
44#define GET_INSTRINFO_NAMED_OPS
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
69using namespace RISCV;
70
71#define GET_RISCVVPseudosTable_IMPL
72#include "RISCVGenSearchableTables.inc"
73
74} // namespace llvm::RISCVVPseudosTable
75
76namespace llvm::RISCV {
77
78#define GET_RISCVMaskedPseudosTable_IMPL
79#include "RISCVGenSearchableTables.inc"
80
81} // end namespace llvm::RISCV
82
84 : RISCVGenInstrInfo(STI, RISCV::ADJCALLSTACKDOWN, RISCV::ADJCALLSTACKUP),
85 STI(STI) {}
86
87#define GET_INSTRINFO_HELPERS
88#include "RISCVGenInstrInfo.inc"
89
91 if (STI.hasStdExtZca())
92 return MCInstBuilder(RISCV::C_NOP);
93 return MCInstBuilder(RISCV::ADDI)
94 .addReg(RISCV::X0)
95 .addReg(RISCV::X0)
96 .addImm(0);
97}
98
100 int &FrameIndex) const {
101 TypeSize Dummy = TypeSize::getZero();
102 return isLoadFromStackSlot(MI, FrameIndex, Dummy);
103}
104
105static std::optional<unsigned> getLMULForRVVWholeLoadStore(unsigned Opcode) {
106 switch (Opcode) {
107 default:
108 return std::nullopt;
109 case RISCV::VS1R_V:
110 case RISCV::VL1RE8_V:
111 case RISCV::VL1RE16_V:
112 case RISCV::VL1RE32_V:
113 case RISCV::VL1RE64_V:
114 return 1;
115 case RISCV::VS2R_V:
116 case RISCV::VL2RE8_V:
117 case RISCV::VL2RE16_V:
118 case RISCV::VL2RE32_V:
119 case RISCV::VL2RE64_V:
120 return 2;
121 case RISCV::VS4R_V:
122 case RISCV::VL4RE8_V:
123 case RISCV::VL4RE16_V:
124 case RISCV::VL4RE32_V:
125 case RISCV::VL4RE64_V:
126 return 4;
127 case RISCV::VS8R_V:
128 case RISCV::VL8RE8_V:
129 case RISCV::VL8RE16_V:
130 case RISCV::VL8RE32_V:
131 case RISCV::VL8RE64_V:
132 return 8;
133 }
134}
135
137 int &FrameIndex,
138 TypeSize &MemBytes) const {
139 switch (MI.getOpcode()) {
140 default:
141 return 0;
142 case RISCV::LB:
143 case RISCV::LBU:
144 MemBytes = TypeSize::getFixed(1);
145 break;
146 case RISCV::LH:
147 case RISCV::LH_INX:
148 case RISCV::LHU:
149 case RISCV::FLH:
150 MemBytes = TypeSize::getFixed(2);
151 break;
152 case RISCV::LW:
153 case RISCV::LW_INX:
154 case RISCV::FLW:
155 case RISCV::LWU:
156 MemBytes = TypeSize::getFixed(4);
157 break;
158 case RISCV::LD:
159 case RISCV::LD_RV32:
160 case RISCV::FLD:
161 MemBytes = TypeSize::getFixed(8);
162 break;
163 case RISCV::VL1RE8_V:
164 case RISCV::VL2RE8_V:
165 case RISCV::VL4RE8_V:
166 case RISCV::VL8RE8_V:
167 if (!MI.getOperand(1).isFI())
168 return Register();
169 FrameIndex = MI.getOperand(1).getIndex();
170 unsigned LMUL = *getLMULForRVVWholeLoadStore(MI.getOpcode());
172 return MI.getOperand(0).getReg();
173 }
174
175 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
176 MI.getOperand(2).getImm() == 0) {
177 FrameIndex = MI.getOperand(1).getIndex();
178 return MI.getOperand(0).getReg();
179 }
180
181 return 0;
182}
183
185 int &FrameIndex) const {
186 TypeSize Dummy = TypeSize::getZero();
187 return isStoreToStackSlot(MI, FrameIndex, Dummy);
188}
189
191 int &FrameIndex,
192 TypeSize &MemBytes) const {
193 switch (MI.getOpcode()) {
194 default:
195 return 0;
196 case RISCV::SB:
197 MemBytes = TypeSize::getFixed(1);
198 break;
199 case RISCV::SH:
200 case RISCV::SH_INX:
201 case RISCV::FSH:
202 MemBytes = TypeSize::getFixed(2);
203 break;
204 case RISCV::SW:
205 case RISCV::SW_INX:
206 case RISCV::FSW:
207 MemBytes = TypeSize::getFixed(4);
208 break;
209 case RISCV::SD:
210 case RISCV::SD_RV32:
211 case RISCV::FSD:
212 MemBytes = TypeSize::getFixed(8);
213 break;
214 case RISCV::VS1R_V:
215 case RISCV::VS2R_V:
216 case RISCV::VS4R_V:
217 case RISCV::VS8R_V:
218 if (!MI.getOperand(1).isFI())
219 return Register();
220 FrameIndex = MI.getOperand(1).getIndex();
221 unsigned LMUL = *getLMULForRVVWholeLoadStore(MI.getOpcode());
223 return MI.getOperand(0).getReg();
224 }
225
226 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
227 MI.getOperand(2).getImm() == 0) {
228 FrameIndex = MI.getOperand(1).getIndex();
229 return MI.getOperand(0).getReg();
230 }
231
232 return 0;
233}
234
236 const MachineInstr &MI) const {
237 switch (RISCV::getRVVMCOpcode(MI.getOpcode())) {
238 case RISCV::VMV_V_X:
239 case RISCV::VFMV_V_F:
240 case RISCV::VMV_V_I:
241 case RISCV::VMV_S_X:
242 case RISCV::VFMV_S_F:
243 case RISCV::VID_V:
244 return MI.getOperand(1).isUndef();
245 default:
247 }
248}
249
250static bool forwardCopyWillClobberTuple(unsigned DstReg, unsigned SrcReg,
251 unsigned NumRegs) {
252 return DstReg > SrcReg && (DstReg - SrcReg) < NumRegs;
253}
254
256 const MachineBasicBlock &MBB,
259 RISCVVType::VLMUL LMul) {
261 return false;
262
263 assert(MBBI->getOpcode() == TargetOpcode::COPY &&
264 "Unexpected COPY instruction.");
265 Register SrcReg = MBBI->getOperand(1).getReg();
267
268 bool FoundDef = false;
269 bool FirstVSetVLI = false;
270 unsigned FirstSEW = 0;
271 while (MBBI != MBB.begin()) {
272 --MBBI;
273 if (MBBI->isMetaInstruction())
274 continue;
275
276 if (RISCVInstrInfo::isVectorConfigInstr(*MBBI)) {
277 // There is a vsetvli between COPY and source define instruction.
278 // vy = def_vop ... (producing instruction)
279 // ...
280 // vsetvli
281 // ...
282 // vx = COPY vy
283 if (!FoundDef) {
284 if (!FirstVSetVLI) {
285 FirstVSetVLI = true;
286 unsigned FirstVType = MBBI->getOperand(2).getImm();
287 RISCVVType::VLMUL FirstLMul = RISCVVType::getVLMUL(FirstVType);
288 FirstSEW = RISCVVType::getSEW(FirstVType);
289 // The first encountered vsetvli must have the same lmul as the
290 // register class of COPY.
291 if (FirstLMul != LMul)
292 return false;
293 }
294 // Only permit `vsetvli x0, x0, vtype` between COPY and the source
295 // define instruction.
296 if (!RISCVInstrInfo::isVLPreservingConfig(*MBBI))
297 return false;
298 continue;
299 }
300
301 // MBBI is the first vsetvli before the producing instruction.
302 unsigned VType = MBBI->getOperand(2).getImm();
303 // If there is a vsetvli between COPY and the producing instruction.
304 if (FirstVSetVLI) {
305 // If SEW is different, return false.
306 if (RISCVVType::getSEW(VType) != FirstSEW)
307 return false;
308 }
309
310 // If the vsetvli is tail undisturbed, keep the whole register move.
311 if (!RISCVVType::isTailAgnostic(VType))
312 return false;
313
314 // The checking is conservative. We only have register classes for
315 // LMUL = 1/2/4/8. We should be able to convert vmv1r.v to vmv.v.v
316 // for fractional LMUL operations. However, we could not use the vsetvli
317 // lmul for widening operations. The result of widening operation is
318 // 2 x LMUL.
319 return LMul == RISCVVType::getVLMUL(VType);
320 } else if (MBBI->isInlineAsm() || MBBI->isCall()) {
321 return false;
322 } else if (MBBI->getNumDefs()) {
323 // Check all the instructions which will change VL.
324 // For example, vleff has implicit def VL.
325 if (MBBI->modifiesRegister(RISCV::VL, /*TRI=*/nullptr))
326 return false;
327
328 // Only converting whole register copies to vmv.v.v when the defining
329 // value appears in the explicit operands.
330 for (const MachineOperand &MO : MBBI->explicit_operands()) {
331 if (!MO.isReg() || !MO.isDef())
332 continue;
333 if (!FoundDef && TRI->regsOverlap(MO.getReg(), SrcReg)) {
334 // We only permit the source of COPY has the same LMUL as the defined
335 // operand.
336 // There are cases we need to keep the whole register copy if the LMUL
337 // is different.
338 // For example,
339 // $x0 = PseudoVSETIVLI 4, 73 // vsetivli zero, 4, e16,m2,ta,m
340 // $v28m4 = PseudoVWADD_VV_M2 $v26m2, $v8m2
341 // # The COPY may be created by vlmul_trunc intrinsic.
342 // $v26m2 = COPY renamable $v28m2, implicit killed $v28m4
343 //
344 // After widening, the valid value will be 4 x e32 elements. If we
345 // convert the COPY to vmv.v.v, it will only copy 4 x e16 elements.
346 // FIXME: The COPY of subregister of Zvlsseg register will not be able
347 // to convert to vmv.v.[v|i] under the constraint.
348 if (MO.getReg() != SrcReg)
349 return false;
350
351 // In widening reduction instructions with LMUL_1 input vector case,
352 // only checking the LMUL is insufficient due to reduction result is
353 // always LMUL_1.
354 // For example,
355 // $x11 = PseudoVSETIVLI 1, 64 // vsetivli a1, 1, e8, m1, ta, mu
356 // $v8m1 = PseudoVWREDSUM_VS_M1 $v26, $v27
357 // $v26 = COPY killed renamable $v8
358 // After widening, The valid value will be 1 x e16 elements. If we
359 // convert the COPY to vmv.v.v, it will only copy 1 x e8 elements.
360 uint64_t TSFlags = MBBI->getDesc().TSFlags;
362 return false;
363
364 // If the producing instruction does not depend on vsetvli, do not
365 // convert COPY to vmv.v.v. For example, VL1R_V or PseudoVRELOAD.
366 if (!RISCVII::hasSEWOp(TSFlags) || !RISCVII::hasVLOp(TSFlags))
367 return false;
368
369 // Found the definition.
370 FoundDef = true;
371 DefMBBI = MBBI;
372 break;
373 }
374 }
375 }
376 }
377
378 return false;
379}
380
383 const DebugLoc &DL, MCRegister DstReg, MCRegister SrcReg, bool KillSrc,
384 const TargetRegisterClass *RegClass) const {
385 const RISCVRegisterInfo *TRI = STI.getRegisterInfo();
387 unsigned NF = RISCVRI::getNF(RegClass->TSFlags);
388
389 uint16_t SrcEncoding = TRI->getEncodingValue(SrcReg);
390 uint16_t DstEncoding = TRI->getEncodingValue(DstReg);
391 auto [LMulVal, Fractional] = RISCVVType::decodeVLMUL(LMul);
392 assert(!Fractional && "It is impossible be fractional lmul here.");
393 unsigned NumRegs = NF * LMulVal;
394 bool ReversedCopy =
395 forwardCopyWillClobberTuple(DstEncoding, SrcEncoding, NumRegs);
396 if (ReversedCopy) {
397 // If the src and dest overlap when copying a tuple, we need to copy the
398 // registers in reverse.
399 SrcEncoding += NumRegs - 1;
400 DstEncoding += NumRegs - 1;
401 }
402
403 unsigned I = 0;
404 auto GetCopyInfo = [&](uint16_t SrcEncoding, uint16_t DstEncoding)
405 -> std::tuple<RISCVVType::VLMUL, const TargetRegisterClass &, unsigned,
406 unsigned, unsigned> {
407 if (ReversedCopy) {
408 // For reversed copying, if there are enough aligned registers(8/4/2), we
409 // can do a larger copy(LMUL8/4/2).
410 // Besides, we have already known that DstEncoding is larger than
411 // SrcEncoding in forwardCopyWillClobberTuple, so the difference between
412 // DstEncoding and SrcEncoding should be >= LMUL value we try to use to
413 // avoid clobbering.
414 uint16_t Diff = DstEncoding - SrcEncoding;
415 if (I + 8 <= NumRegs && Diff >= 8 && SrcEncoding % 8 == 7 &&
416 DstEncoding % 8 == 7)
417 return {RISCVVType::LMUL_8, RISCV::VRM8RegClass, RISCV::VMV8R_V,
418 RISCV::PseudoVMV_V_V_M8, RISCV::PseudoVMV_V_I_M8};
419 if (I + 4 <= NumRegs && Diff >= 4 && SrcEncoding % 4 == 3 &&
420 DstEncoding % 4 == 3)
421 return {RISCVVType::LMUL_4, RISCV::VRM4RegClass, RISCV::VMV4R_V,
422 RISCV::PseudoVMV_V_V_M4, RISCV::PseudoVMV_V_I_M4};
423 if (I + 2 <= NumRegs && Diff >= 2 && SrcEncoding % 2 == 1 &&
424 DstEncoding % 2 == 1)
425 return {RISCVVType::LMUL_2, RISCV::VRM2RegClass, RISCV::VMV2R_V,
426 RISCV::PseudoVMV_V_V_M2, RISCV::PseudoVMV_V_I_M2};
427 // Or we should do LMUL1 copying.
428 return {RISCVVType::LMUL_1, RISCV::VRRegClass, RISCV::VMV1R_V,
429 RISCV::PseudoVMV_V_V_M1, RISCV::PseudoVMV_V_I_M1};
430 }
431
432 // For forward copying, if source register encoding and destination register
433 // encoding are aligned to 8/4/2, we can do a LMUL8/4/2 copying.
434 if (I + 8 <= NumRegs && SrcEncoding % 8 == 0 && DstEncoding % 8 == 0)
435 return {RISCVVType::LMUL_8, RISCV::VRM8RegClass, RISCV::VMV8R_V,
436 RISCV::PseudoVMV_V_V_M8, RISCV::PseudoVMV_V_I_M8};
437 if (I + 4 <= NumRegs && SrcEncoding % 4 == 0 && DstEncoding % 4 == 0)
438 return {RISCVVType::LMUL_4, RISCV::VRM4RegClass, RISCV::VMV4R_V,
439 RISCV::PseudoVMV_V_V_M4, RISCV::PseudoVMV_V_I_M4};
440 if (I + 2 <= NumRegs && SrcEncoding % 2 == 0 && DstEncoding % 2 == 0)
441 return {RISCVVType::LMUL_2, RISCV::VRM2RegClass, RISCV::VMV2R_V,
442 RISCV::PseudoVMV_V_V_M2, RISCV::PseudoVMV_V_I_M2};
443 // Or we should do LMUL1 copying.
444 return {RISCVVType::LMUL_1, RISCV::VRRegClass, RISCV::VMV1R_V,
445 RISCV::PseudoVMV_V_V_M1, RISCV::PseudoVMV_V_I_M1};
446 };
447
448 while (I != NumRegs) {
449 // For non-segment copying, we only do this once as the registers are always
450 // aligned.
451 // For segment copying, we may do this several times. If the registers are
452 // aligned to larger LMUL, we can eliminate some copyings.
453 auto [LMulCopied, RegClass, Opc, VVOpc, VIOpc] =
454 GetCopyInfo(SrcEncoding, DstEncoding);
455 auto [NumCopied, _] = RISCVVType::decodeVLMUL(LMulCopied);
456
458 if (LMul == LMulCopied &&
459 isConvertibleToVMV_V_V(STI, MBB, MBBI, DefMBBI, LMul)) {
460 Opc = VVOpc;
461 if (DefMBBI->getOpcode() == VIOpc)
462 Opc = VIOpc;
463 }
464
465 // Emit actual copying.
466 // For reversed copying, the encoding should be decreased.
467 MCRegister ActualSrcReg = TRI->findVRegWithEncoding(
468 RegClass, ReversedCopy ? (SrcEncoding - NumCopied + 1) : SrcEncoding);
469 MCRegister ActualDstReg = TRI->findVRegWithEncoding(
470 RegClass, ReversedCopy ? (DstEncoding - NumCopied + 1) : DstEncoding);
471
472 auto MIB = BuildMI(MBB, MBBI, DL, get(Opc), ActualDstReg);
473 bool UseVMV_V_I = RISCV::getRVVMCOpcode(Opc) == RISCV::VMV_V_I;
474 bool UseVMV = UseVMV_V_I || RISCV::getRVVMCOpcode(Opc) == RISCV::VMV_V_V;
475 if (UseVMV)
476 MIB.addReg(ActualDstReg, RegState::Undef);
477 if (UseVMV_V_I)
478 MIB = MIB.add(DefMBBI->getOperand(2));
479 else
480 MIB = MIB.addReg(ActualSrcReg, getKillRegState(KillSrc));
481 if (UseVMV) {
482 const MCInstrDesc &Desc = DefMBBI->getDesc();
483 MIB.add(DefMBBI->getOperand(RISCVII::getVLOpNum(Desc))); // AVL
484 unsigned Log2SEW =
485 DefMBBI->getOperand(RISCVII::getSEWOpNum(Desc)).getImm();
486 MIB.addImm(Log2SEW ? Log2SEW : 3); // SEW
487 MIB.addImm(0); // tu, mu
488 MIB.addReg(RISCV::VL, RegState::Implicit);
489 MIB.addReg(RISCV::VTYPE, RegState::Implicit);
490 }
491 // Add an implicit read of the original source to silence the verifier
492 // in the cases where some of the smaller VRs we're copying from might be
493 // undef, caused by the fact that the original, larger source VR might not
494 // be fully initialized at the time this COPY happens.
495 MIB.addReg(SrcReg, RegState::Implicit);
496
497 // If we are copying reversely, we should decrease the encoding.
498 SrcEncoding += (ReversedCopy ? -NumCopied : NumCopied);
499 DstEncoding += (ReversedCopy ? -NumCopied : NumCopied);
500 I += NumCopied;
501 }
502}
503
506 const DebugLoc &DL, Register DstReg,
507 Register SrcReg, bool KillSrc,
508 bool RenamableDest, bool RenamableSrc) const {
509 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
510 unsigned KillFlag = getKillRegState(KillSrc);
511
512 if (RISCV::GPRRegClass.contains(DstReg, SrcReg)) {
513 BuildMI(MBB, MBBI, DL, get(RISCV::ADDI), DstReg)
514 .addReg(SrcReg, KillFlag | getRenamableRegState(RenamableSrc))
515 .addImm(0);
516 return;
517 }
518
519 if (RISCV::GPRF16RegClass.contains(DstReg, SrcReg)) {
520 BuildMI(MBB, MBBI, DL, get(RISCV::PseudoMV_FPR16INX), DstReg)
521 .addReg(SrcReg, KillFlag | getRenamableRegState(RenamableSrc));
522 return;
523 }
524
525 if (RISCV::GPRF32RegClass.contains(DstReg, SrcReg)) {
526 BuildMI(MBB, MBBI, DL, get(RISCV::PseudoMV_FPR32INX), DstReg)
527 .addReg(SrcReg, KillFlag | getRenamableRegState(RenamableSrc));
528 return;
529 }
530
531 if (RISCV::GPRPairRegClass.contains(DstReg, SrcReg)) {
532 MCRegister EvenReg = TRI->getSubReg(SrcReg, RISCV::sub_gpr_even);
533 MCRegister OddReg = TRI->getSubReg(SrcReg, RISCV::sub_gpr_odd);
534 // We need to correct the odd register of X0_Pair.
535 if (OddReg == RISCV::DUMMY_REG_PAIR_WITH_X0)
536 OddReg = RISCV::X0;
537 assert(DstReg != RISCV::X0_Pair && "Cannot write to X0_Pair");
538
539 // Emit an ADDI for both parts of GPRPair.
540 BuildMI(MBB, MBBI, DL, get(RISCV::ADDI),
541 TRI->getSubReg(DstReg, RISCV::sub_gpr_even))
542 .addReg(EvenReg, KillFlag)
543 .addImm(0);
544 BuildMI(MBB, MBBI, DL, get(RISCV::ADDI),
545 TRI->getSubReg(DstReg, RISCV::sub_gpr_odd))
546 .addReg(OddReg, KillFlag)
547 .addImm(0);
548 return;
549 }
550
551 // Handle copy from csr
552 if (RISCV::VCSRRegClass.contains(SrcReg) &&
553 RISCV::GPRRegClass.contains(DstReg)) {
554 BuildMI(MBB, MBBI, DL, get(RISCV::CSRRS), DstReg)
555 .addImm(RISCVSysReg::lookupSysRegByName(TRI->getName(SrcReg))->Encoding)
556 .addReg(RISCV::X0);
557 return;
558 }
559
560 if (RISCV::FPR16RegClass.contains(DstReg, SrcReg)) {
561 unsigned Opc;
562 if (STI.hasStdExtZfh()) {
563 Opc = RISCV::FSGNJ_H;
564 } else {
565 assert(STI.hasStdExtF() &&
566 (STI.hasStdExtZfhmin() || STI.hasStdExtZfbfmin()) &&
567 "Unexpected extensions");
568 // Zfhmin/Zfbfmin doesn't have FSGNJ_H, replace FSGNJ_H with FSGNJ_S.
569 DstReg = TRI->getMatchingSuperReg(DstReg, RISCV::sub_16,
570 &RISCV::FPR32RegClass);
571 SrcReg = TRI->getMatchingSuperReg(SrcReg, RISCV::sub_16,
572 &RISCV::FPR32RegClass);
573 Opc = RISCV::FSGNJ_S;
574 }
575 BuildMI(MBB, MBBI, DL, get(Opc), DstReg)
576 .addReg(SrcReg, KillFlag)
577 .addReg(SrcReg, KillFlag);
578 return;
579 }
580
581 if (RISCV::FPR32RegClass.contains(DstReg, SrcReg)) {
582 BuildMI(MBB, MBBI, DL, get(RISCV::FSGNJ_S), DstReg)
583 .addReg(SrcReg, KillFlag)
584 .addReg(SrcReg, KillFlag);
585 return;
586 }
587
588 if (RISCV::FPR64RegClass.contains(DstReg, SrcReg)) {
589 BuildMI(MBB, MBBI, DL, get(RISCV::FSGNJ_D), DstReg)
590 .addReg(SrcReg, KillFlag)
591 .addReg(SrcReg, KillFlag);
592 return;
593 }
594
595 if (RISCV::FPR32RegClass.contains(DstReg) &&
596 RISCV::GPRRegClass.contains(SrcReg)) {
597 BuildMI(MBB, MBBI, DL, get(RISCV::FMV_W_X), DstReg)
598 .addReg(SrcReg, KillFlag);
599 return;
600 }
601
602 if (RISCV::GPRRegClass.contains(DstReg) &&
603 RISCV::FPR32RegClass.contains(SrcReg)) {
604 BuildMI(MBB, MBBI, DL, get(RISCV::FMV_X_W), DstReg)
605 .addReg(SrcReg, KillFlag);
606 return;
607 }
608
609 if (RISCV::FPR64RegClass.contains(DstReg) &&
610 RISCV::GPRRegClass.contains(SrcReg)) {
611 assert(STI.getXLen() == 64 && "Unexpected GPR size");
612 BuildMI(MBB, MBBI, DL, get(RISCV::FMV_D_X), DstReg)
613 .addReg(SrcReg, KillFlag);
614 return;
615 }
616
617 if (RISCV::GPRRegClass.contains(DstReg) &&
618 RISCV::FPR64RegClass.contains(SrcReg)) {
619 assert(STI.getXLen() == 64 && "Unexpected GPR size");
620 BuildMI(MBB, MBBI, DL, get(RISCV::FMV_X_D), DstReg)
621 .addReg(SrcReg, KillFlag);
622 return;
623 }
624
625 // VR->VR copies.
626 const TargetRegisterClass *RegClass =
627 TRI->getCommonMinimalPhysRegClass(SrcReg, DstReg);
628 if (RISCVRegisterInfo::isRVVRegClass(RegClass)) {
629 copyPhysRegVector(MBB, MBBI, DL, DstReg, SrcReg, KillSrc, RegClass);
630 return;
631 }
632
633 llvm_unreachable("Impossible reg-to-reg copy");
634}
635
638 Register SrcReg, bool IsKill, int FI,
639 const TargetRegisterClass *RC,
640 const TargetRegisterInfo *TRI,
641 Register VReg,
642 MachineInstr::MIFlag Flags) const {
643 MachineFunction *MF = MBB.getParent();
644 MachineFrameInfo &MFI = MF->getFrameInfo();
645
646 unsigned Opcode;
647 if (RISCV::GPRRegClass.hasSubClassEq(RC)) {
648 Opcode = TRI->getRegSizeInBits(RISCV::GPRRegClass) == 32 ?
649 RISCV::SW : RISCV::SD;
650 } else if (RISCV::GPRF16RegClass.hasSubClassEq(RC)) {
651 Opcode = RISCV::SH_INX;
652 } else if (RISCV::GPRF32RegClass.hasSubClassEq(RC)) {
653 Opcode = RISCV::SW_INX;
654 } else if (RISCV::GPRPairRegClass.hasSubClassEq(RC)) {
655 Opcode = RISCV::PseudoRV32ZdinxSD;
656 } else if (RISCV::FPR16RegClass.hasSubClassEq(RC)) {
657 Opcode = RISCV::FSH;
658 } else if (RISCV::FPR32RegClass.hasSubClassEq(RC)) {
659 Opcode = RISCV::FSW;
660 } else if (RISCV::FPR64RegClass.hasSubClassEq(RC)) {
661 Opcode = RISCV::FSD;
662 } else if (RISCV::VRRegClass.hasSubClassEq(RC)) {
663 Opcode = RISCV::VS1R_V;
664 } else if (RISCV::VRM2RegClass.hasSubClassEq(RC)) {
665 Opcode = RISCV::VS2R_V;
666 } else if (RISCV::VRM4RegClass.hasSubClassEq(RC)) {
667 Opcode = RISCV::VS4R_V;
668 } else if (RISCV::VRM8RegClass.hasSubClassEq(RC)) {
669 Opcode = RISCV::VS8R_V;
670 } else if (RISCV::VRN2M1RegClass.hasSubClassEq(RC))
671 Opcode = RISCV::PseudoVSPILL2_M1;
672 else if (RISCV::VRN2M2RegClass.hasSubClassEq(RC))
673 Opcode = RISCV::PseudoVSPILL2_M2;
674 else if (RISCV::VRN2M4RegClass.hasSubClassEq(RC))
675 Opcode = RISCV::PseudoVSPILL2_M4;
676 else if (RISCV::VRN3M1RegClass.hasSubClassEq(RC))
677 Opcode = RISCV::PseudoVSPILL3_M1;
678 else if (RISCV::VRN3M2RegClass.hasSubClassEq(RC))
679 Opcode = RISCV::PseudoVSPILL3_M2;
680 else if (RISCV::VRN4M1RegClass.hasSubClassEq(RC))
681 Opcode = RISCV::PseudoVSPILL4_M1;
682 else if (RISCV::VRN4M2RegClass.hasSubClassEq(RC))
683 Opcode = RISCV::PseudoVSPILL4_M2;
684 else if (RISCV::VRN5M1RegClass.hasSubClassEq(RC))
685 Opcode = RISCV::PseudoVSPILL5_M1;
686 else if (RISCV::VRN6M1RegClass.hasSubClassEq(RC))
687 Opcode = RISCV::PseudoVSPILL6_M1;
688 else if (RISCV::VRN7M1RegClass.hasSubClassEq(RC))
689 Opcode = RISCV::PseudoVSPILL7_M1;
690 else if (RISCV::VRN8M1RegClass.hasSubClassEq(RC))
691 Opcode = RISCV::PseudoVSPILL8_M1;
692 else
693 llvm_unreachable("Can't store this register to stack slot");
694
699
701 BuildMI(MBB, I, DebugLoc(), get(Opcode))
702 .addReg(SrcReg, getKillRegState(IsKill))
703 .addFrameIndex(FI)
704 .addMemOperand(MMO)
705 .setMIFlag(Flags);
706 NumVRegSpilled += TRI->getRegSizeInBits(*RC) / RISCV::RVVBitsPerBlock;
707 } else {
710 MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
711
712 BuildMI(MBB, I, DebugLoc(), get(Opcode))
713 .addReg(SrcReg, getKillRegState(IsKill))
714 .addFrameIndex(FI)
715 .addImm(0)
716 .addMemOperand(MMO)
717 .setMIFlag(Flags);
718 }
719}
720
723 int FI, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI,
724 Register VReg, MachineInstr::MIFlag Flags) const {
725 MachineFunction *MF = MBB.getParent();
726 MachineFrameInfo &MFI = MF->getFrameInfo();
727 DebugLoc DL =
728 Flags & MachineInstr::FrameDestroy ? MBB.findDebugLoc(I) : DebugLoc();
729
730 unsigned Opcode;
731 if (RISCV::GPRRegClass.hasSubClassEq(RC)) {
732 Opcode = TRI->getRegSizeInBits(RISCV::GPRRegClass) == 32 ?
733 RISCV::LW : RISCV::LD;
734 } else if (RISCV::GPRF16RegClass.hasSubClassEq(RC)) {
735 Opcode = RISCV::LH_INX;
736 } else if (RISCV::GPRF32RegClass.hasSubClassEq(RC)) {
737 Opcode = RISCV::LW_INX;
738 } else if (RISCV::GPRPairRegClass.hasSubClassEq(RC)) {
739 Opcode = RISCV::PseudoRV32ZdinxLD;
740 } else if (RISCV::FPR16RegClass.hasSubClassEq(RC)) {
741 Opcode = RISCV::FLH;
742 } else if (RISCV::FPR32RegClass.hasSubClassEq(RC)) {
743 Opcode = RISCV::FLW;
744 } else if (RISCV::FPR64RegClass.hasSubClassEq(RC)) {
745 Opcode = RISCV::FLD;
746 } else if (RISCV::VRRegClass.hasSubClassEq(RC)) {
747 Opcode = RISCV::VL1RE8_V;
748 } else if (RISCV::VRM2RegClass.hasSubClassEq(RC)) {
749 Opcode = RISCV::VL2RE8_V;
750 } else if (RISCV::VRM4RegClass.hasSubClassEq(RC)) {
751 Opcode = RISCV::VL4RE8_V;
752 } else if (RISCV::VRM8RegClass.hasSubClassEq(RC)) {
753 Opcode = RISCV::VL8RE8_V;
754 } else if (RISCV::VRN2M1RegClass.hasSubClassEq(RC))
755 Opcode = RISCV::PseudoVRELOAD2_M1;
756 else if (RISCV::VRN2M2RegClass.hasSubClassEq(RC))
757 Opcode = RISCV::PseudoVRELOAD2_M2;
758 else if (RISCV::VRN2M4RegClass.hasSubClassEq(RC))
759 Opcode = RISCV::PseudoVRELOAD2_M4;
760 else if (RISCV::VRN3M1RegClass.hasSubClassEq(RC))
761 Opcode = RISCV::PseudoVRELOAD3_M1;
762 else if (RISCV::VRN3M2RegClass.hasSubClassEq(RC))
763 Opcode = RISCV::PseudoVRELOAD3_M2;
764 else if (RISCV::VRN4M1RegClass.hasSubClassEq(RC))
765 Opcode = RISCV::PseudoVRELOAD4_M1;
766 else if (RISCV::VRN4M2RegClass.hasSubClassEq(RC))
767 Opcode = RISCV::PseudoVRELOAD4_M2;
768 else if (RISCV::VRN5M1RegClass.hasSubClassEq(RC))
769 Opcode = RISCV::PseudoVRELOAD5_M1;
770 else if (RISCV::VRN6M1RegClass.hasSubClassEq(RC))
771 Opcode = RISCV::PseudoVRELOAD6_M1;
772 else if (RISCV::VRN7M1RegClass.hasSubClassEq(RC))
773 Opcode = RISCV::PseudoVRELOAD7_M1;
774 else if (RISCV::VRN8M1RegClass.hasSubClassEq(RC))
775 Opcode = RISCV::PseudoVRELOAD8_M1;
776 else
777 llvm_unreachable("Can't load this register from stack slot");
778
783
785 BuildMI(MBB, I, DL, get(Opcode), DstReg)
786 .addFrameIndex(FI)
787 .addMemOperand(MMO)
788 .setMIFlag(Flags);
789 NumVRegReloaded += TRI->getRegSizeInBits(*RC) / RISCV::RVVBitsPerBlock;
790 } else {
793 MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
794
795 BuildMI(MBB, I, DL, get(Opcode), DstReg)
796 .addFrameIndex(FI)
797 .addImm(0)
798 .addMemOperand(MMO)
799 .setMIFlag(Flags);
800 }
801}
802std::optional<unsigned> getFoldedOpcode(MachineFunction &MF, MachineInstr &MI,
804 const RISCVSubtarget &ST) {
805
806 // The below optimizations narrow the load so they are only valid for little
807 // endian.
808 // TODO: Support big endian by adding an offset into the frame object?
809 if (MF.getDataLayout().isBigEndian())
810 return std::nullopt;
811
812 // Fold load from stack followed by sext.b/sext.h/sext.w/zext.b/zext.h/zext.w.
813 if (Ops.size() != 1 || Ops[0] != 1)
814 return std::nullopt;
815
816 switch (MI.getOpcode()) {
817 default:
818 if (RISCVInstrInfo::isSEXT_W(MI))
819 return RISCV::LW;
820 if (RISCVInstrInfo::isZEXT_W(MI))
821 return RISCV::LWU;
822 if (RISCVInstrInfo::isZEXT_B(MI))
823 return RISCV::LBU;
824 break;
825 case RISCV::SEXT_H:
826 return RISCV::LH;
827 case RISCV::SEXT_B:
828 return RISCV::LB;
829 case RISCV::ZEXT_H_RV32:
830 case RISCV::ZEXT_H_RV64:
831 return RISCV::LHU;
832 }
833
834 switch (RISCV::getRVVMCOpcode(MI.getOpcode())) {
835 default:
836 return std::nullopt;
837 case RISCV::VMV_X_S: {
838 unsigned Log2SEW =
839 MI.getOperand(RISCVII::getSEWOpNum(MI.getDesc())).getImm();
840 if (ST.getXLen() < (1U << Log2SEW))
841 return std::nullopt;
842 switch (Log2SEW) {
843 case 3:
844 return RISCV::LB;
845 case 4:
846 return RISCV::LH;
847 case 5:
848 return RISCV::LW;
849 case 6:
850 return RISCV::LD;
851 default:
852 llvm_unreachable("Unexpected SEW");
853 }
854 }
855 case RISCV::VFMV_F_S: {
856 unsigned Log2SEW =
857 MI.getOperand(RISCVII::getSEWOpNum(MI.getDesc())).getImm();
858 switch (Log2SEW) {
859 case 4:
860 return RISCV::FLH;
861 case 5:
862 return RISCV::FLW;
863 case 6:
864 return RISCV::FLD;
865 default:
866 llvm_unreachable("Unexpected SEW");
867 }
868 }
869 }
870}
871
872// This is the version used during inline spilling
875 MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS,
876 VirtRegMap *VRM) const {
877
878 std::optional<unsigned> LoadOpc = getFoldedOpcode(MF, MI, Ops, STI);
879 if (!LoadOpc)
880 return nullptr;
881 Register DstReg = MI.getOperand(0).getReg();
882 return BuildMI(*MI.getParent(), InsertPt, MI.getDebugLoc(), get(*LoadOpc),
883 DstReg)
884 .addFrameIndex(FrameIndex)
885 .addImm(0);
886}
887
890 const DebugLoc &DL, Register DstReg, uint64_t Val,
891 MachineInstr::MIFlag Flag, bool DstRenamable,
892 bool DstIsDead) const {
893 Register SrcReg = RISCV::X0;
894
895 // For RV32, allow a sign or unsigned 32 bit value.
896 if (!STI.is64Bit() && !isInt<32>(Val)) {
897 // If have a uimm32 it will still fit in a register so we can allow it.
898 if (!isUInt<32>(Val))
899 report_fatal_error("Should only materialize 32-bit constants for RV32");
900
901 // Sign extend for generateInstSeq.
902 Val = SignExtend64<32>(Val);
903 }
904
906 assert(!Seq.empty());
907
908 bool SrcRenamable = false;
909 unsigned Num = 0;
910
911 for (const RISCVMatInt::Inst &Inst : Seq) {
912 bool LastItem = ++Num == Seq.size();
913 unsigned DstRegState = getDeadRegState(DstIsDead && LastItem) |
914 getRenamableRegState(DstRenamable);
915 unsigned SrcRegState = getKillRegState(SrcReg != RISCV::X0) |
916 getRenamableRegState(SrcRenamable);
917 switch (Inst.getOpndKind()) {
918 case RISCVMatInt::Imm:
919 BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()))
920 .addReg(DstReg, RegState::Define | DstRegState)
921 .addImm(Inst.getImm())
922 .setMIFlag(Flag);
923 break;
925 BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()))
926 .addReg(DstReg, RegState::Define | DstRegState)
927 .addReg(SrcReg, SrcRegState)
928 .addReg(RISCV::X0)
929 .setMIFlag(Flag);
930 break;
932 BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()))
933 .addReg(DstReg, RegState::Define | DstRegState)
934 .addReg(SrcReg, SrcRegState)
935 .addReg(SrcReg, SrcRegState)
936 .setMIFlag(Flag);
937 break;
939 BuildMI(MBB, MBBI, DL, get(Inst.getOpcode()))
940 .addReg(DstReg, RegState::Define | DstRegState)
941 .addReg(SrcReg, SrcRegState)
942 .addImm(Inst.getImm())
943 .setMIFlag(Flag);
944 break;
945 }
946
947 // Only the first instruction has X0 as its source.
948 SrcReg = DstReg;
949 SrcRenamable = DstRenamable;
950 }
951}
952
954 switch (Opc) {
955 default:
957 case RISCV::BEQ:
958 case RISCV::BEQI:
959 case RISCV::CV_BEQIMM:
960 case RISCV::QC_BEQI:
961 case RISCV::QC_E_BEQI:
962 case RISCV::NDS_BBC:
963 case RISCV::NDS_BEQC:
964 return RISCVCC::COND_EQ;
965 case RISCV::BNE:
966 case RISCV::BNEI:
967 case RISCV::QC_BNEI:
968 case RISCV::QC_E_BNEI:
969 case RISCV::CV_BNEIMM:
970 case RISCV::NDS_BBS:
971 case RISCV::NDS_BNEC:
972 return RISCVCC::COND_NE;
973 case RISCV::BLT:
974 case RISCV::QC_BLTI:
975 case RISCV::QC_E_BLTI:
976 return RISCVCC::COND_LT;
977 case RISCV::BGE:
978 case RISCV::QC_BGEI:
979 case RISCV::QC_E_BGEI:
980 return RISCVCC::COND_GE;
981 case RISCV::BLTU:
982 case RISCV::QC_BLTUI:
983 case RISCV::QC_E_BLTUI:
984 return RISCVCC::COND_LTU;
985 case RISCV::BGEU:
986 case RISCV::QC_BGEUI:
987 case RISCV::QC_E_BGEUI:
988 return RISCVCC::COND_GEU;
989 }
990}
991
993 int64_t C1) {
994 switch (CC) {
995 default:
996 llvm_unreachable("Unexpected CC");
997 case RISCVCC::COND_EQ:
998 return C0 == C1;
999 case RISCVCC::COND_NE:
1000 return C0 != C1;
1001 case RISCVCC::COND_LT:
1002 return C0 < C1;
1003 case RISCVCC::COND_GE:
1004 return C0 >= C1;
1005 case RISCVCC::COND_LTU:
1006 return (uint64_t)C0 < (uint64_t)C1;
1007 case RISCVCC::COND_GEU:
1008 return (uint64_t)C0 >= (uint64_t)C1;
1009 }
1010}
1011
1012// The contents of values added to Cond are not examined outside of
1013// RISCVInstrInfo, giving us flexibility in what to push to it. For RISCV, we
1014// push BranchOpcode, Reg1, Reg2.
1017 // Block ends with fall-through condbranch.
1018 assert(LastInst.getDesc().isConditionalBranch() &&
1019 "Unknown conditional branch");
1020 Target = LastInst.getOperand(2).getMBB();
1021 Cond.push_back(MachineOperand::CreateImm(LastInst.getOpcode()));
1022 Cond.push_back(LastInst.getOperand(0));
1023 Cond.push_back(LastInst.getOperand(1));
1024}
1025
1026unsigned RISCVCC::getBrCond(RISCVCC::CondCode CC, unsigned SelectOpc) {
1027 switch (SelectOpc) {
1028 default:
1029 switch (CC) {
1030 default:
1031 llvm_unreachable("Unexpected condition code!");
1032 case RISCVCC::COND_EQ:
1033 return RISCV::BEQ;
1034 case RISCVCC::COND_NE:
1035 return RISCV::BNE;
1036 case RISCVCC::COND_LT:
1037 return RISCV::BLT;
1038 case RISCVCC::COND_GE:
1039 return RISCV::BGE;
1040 case RISCVCC::COND_LTU:
1041 return RISCV::BLTU;
1042 case RISCVCC::COND_GEU:
1043 return RISCV::BGEU;
1044 }
1045 break;
1046 case RISCV::Select_GPR_Using_CC_Imm5_Zibi:
1047 switch (CC) {
1048 default:
1049 llvm_unreachable("Unexpected condition code!");
1050 case RISCVCC::COND_EQ:
1051 return RISCV::BEQI;
1052 case RISCVCC::COND_NE:
1053 return RISCV::BNEI;
1054 }
1055 break;
1056 case RISCV::Select_GPR_Using_CC_SImm5_CV:
1057 switch (CC) {
1058 default:
1059 llvm_unreachable("Unexpected condition code!");
1060 case RISCVCC::COND_EQ:
1061 return RISCV::CV_BEQIMM;
1062 case RISCVCC::COND_NE:
1063 return RISCV::CV_BNEIMM;
1064 }
1065 break;
1066 case RISCV::Select_GPRNoX0_Using_CC_SImm5NonZero_QC:
1067 switch (CC) {
1068 default:
1069 llvm_unreachable("Unexpected condition code!");
1070 case RISCVCC::COND_EQ:
1071 return RISCV::QC_BEQI;
1072 case RISCVCC::COND_NE:
1073 return RISCV::QC_BNEI;
1074 case RISCVCC::COND_LT:
1075 return RISCV::QC_BLTI;
1076 case RISCVCC::COND_GE:
1077 return RISCV::QC_BGEI;
1078 }
1079 break;
1080 case RISCV::Select_GPRNoX0_Using_CC_UImm5NonZero_QC:
1081 switch (CC) {
1082 default:
1083 llvm_unreachable("Unexpected condition code!");
1084 case RISCVCC::COND_LTU:
1085 return RISCV::QC_BLTUI;
1086 case RISCVCC::COND_GEU:
1087 return RISCV::QC_BGEUI;
1088 }
1089 break;
1090 case RISCV::Select_GPRNoX0_Using_CC_SImm16NonZero_QC:
1091 switch (CC) {
1092 default:
1093 llvm_unreachable("Unexpected condition code!");
1094 case RISCVCC::COND_EQ:
1095 return RISCV::QC_E_BEQI;
1096 case RISCVCC::COND_NE:
1097 return RISCV::QC_E_BNEI;
1098 case RISCVCC::COND_LT:
1099 return RISCV::QC_E_BLTI;
1100 case RISCVCC::COND_GE:
1101 return RISCV::QC_E_BGEI;
1102 }
1103 break;
1104 case RISCV::Select_GPRNoX0_Using_CC_UImm16NonZero_QC:
1105 switch (CC) {
1106 default:
1107 llvm_unreachable("Unexpected condition code!");
1108 case RISCVCC::COND_LTU:
1109 return RISCV::QC_E_BLTUI;
1110 case RISCVCC::COND_GEU:
1111 return RISCV::QC_E_BGEUI;
1112 }
1113 break;
1114 case RISCV::Select_GPR_Using_CC_UImmLog2XLen_NDS:
1115 switch (CC) {
1116 default:
1117 llvm_unreachable("Unexpected condition code!");
1118 case RISCVCC::COND_EQ:
1119 return RISCV::NDS_BBC;
1120 case RISCVCC::COND_NE:
1121 return RISCV::NDS_BBS;
1122 }
1123 break;
1124 case RISCV::Select_GPR_Using_CC_UImm7_NDS:
1125 switch (CC) {
1126 default:
1127 llvm_unreachable("Unexpected condition code!");
1128 case RISCVCC::COND_EQ:
1129 return RISCV::NDS_BEQC;
1130 case RISCVCC::COND_NE:
1131 return RISCV::NDS_BNEC;
1132 }
1133 break;
1134 }
1135}
1136
1138 switch (CC) {
1139 default:
1140 llvm_unreachable("Unrecognized conditional branch");
1141 case RISCVCC::COND_EQ:
1142 return RISCVCC::COND_NE;
1143 case RISCVCC::COND_NE:
1144 return RISCVCC::COND_EQ;
1145 case RISCVCC::COND_LT:
1146 return RISCVCC::COND_GE;
1147 case RISCVCC::COND_GE:
1148 return RISCVCC::COND_LT;
1149 case RISCVCC::COND_LTU:
1150 return RISCVCC::COND_GEU;
1151 case RISCVCC::COND_GEU:
1152 return RISCVCC::COND_LTU;
1153 }
1154}
1155
1158 MachineBasicBlock *&FBB,
1160 bool AllowModify) const {
1161 TBB = FBB = nullptr;
1162 Cond.clear();
1163
1164 // If the block has no terminators, it just falls into the block after it.
1165 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
1166 if (I == MBB.end() || !isUnpredicatedTerminator(*I))
1167 return false;
1168
1169 // Count the number of terminators and find the first unconditional or
1170 // indirect branch.
1171 MachineBasicBlock::iterator FirstUncondOrIndirectBr = MBB.end();
1172 int NumTerminators = 0;
1173 for (auto J = I.getReverse(); J != MBB.rend() && isUnpredicatedTerminator(*J);
1174 J++) {
1175 NumTerminators++;
1176 if (J->getDesc().isUnconditionalBranch() ||
1177 J->getDesc().isIndirectBranch()) {
1178 FirstUncondOrIndirectBr = J.getReverse();
1179 }
1180 }
1181
1182 // If AllowModify is true, we can erase any terminators after
1183 // FirstUncondOrIndirectBR.
1184 if (AllowModify && FirstUncondOrIndirectBr != MBB.end()) {
1185 while (std::next(FirstUncondOrIndirectBr) != MBB.end()) {
1186 std::next(FirstUncondOrIndirectBr)->eraseFromParent();
1187 NumTerminators--;
1188 }
1189 I = FirstUncondOrIndirectBr;
1190 }
1191
1192 // We can't handle blocks that end in an indirect branch.
1193 if (I->getDesc().isIndirectBranch())
1194 return true;
1195
1196 // We can't handle Generic branch opcodes from Global ISel.
1197 if (I->isPreISelOpcode())
1198 return true;
1199
1200 // We can't handle blocks with more than 2 terminators.
1201 if (NumTerminators > 2)
1202 return true;
1203
1204 // Handle a single unconditional branch.
1205 if (NumTerminators == 1 && I->getDesc().isUnconditionalBranch()) {
1207 return false;
1208 }
1209
1210 // Handle a single conditional branch.
1211 if (NumTerminators == 1 && I->getDesc().isConditionalBranch()) {
1213 return false;
1214 }
1215
1216 // Handle a conditional branch followed by an unconditional branch.
1217 if (NumTerminators == 2 && std::prev(I)->getDesc().isConditionalBranch() &&
1218 I->getDesc().isUnconditionalBranch()) {
1219 parseCondBranch(*std::prev(I), TBB, Cond);
1220 FBB = getBranchDestBlock(*I);
1221 return false;
1222 }
1223
1224 // Otherwise, we can't handle this.
1225 return true;
1226}
1227
1229 int *BytesRemoved) const {
1230 if (BytesRemoved)
1231 *BytesRemoved = 0;
1232 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
1233 if (I == MBB.end())
1234 return 0;
1235
1236 if (!I->getDesc().isUnconditionalBranch() &&
1237 !I->getDesc().isConditionalBranch())
1238 return 0;
1239
1240 // Remove the branch.
1241 if (BytesRemoved)
1242 *BytesRemoved += getInstSizeInBytes(*I);
1243 I->eraseFromParent();
1244
1245 I = MBB.end();
1246
1247 if (I == MBB.begin())
1248 return 1;
1249 --I;
1250 if (!I->getDesc().isConditionalBranch())
1251 return 1;
1252
1253 // Remove the branch.
1254 if (BytesRemoved)
1255 *BytesRemoved += getInstSizeInBytes(*I);
1256 I->eraseFromParent();
1257 return 2;
1258}
1259
1260// Inserts a branch into the end of the specific MachineBasicBlock, returning
1261// the number of instructions inserted.
1264 ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
1265 if (BytesAdded)
1266 *BytesAdded = 0;
1267
1268 // Shouldn't be a fall through.
1269 assert(TBB && "insertBranch must not be told to insert a fallthrough");
1270 assert((Cond.size() == 3 || Cond.size() == 0) &&
1271 "RISC-V branch conditions have two components!");
1272
1273 // Unconditional branch.
1274 if (Cond.empty()) {
1275 MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(TBB);
1276 if (BytesAdded)
1277 *BytesAdded += getInstSizeInBytes(MI);
1278 return 1;
1279 }
1280
1281 // Either a one or two-way conditional branch.
1282 MachineInstr &CondMI = *BuildMI(&MBB, DL, get(Cond[0].getImm()))
1283 .add(Cond[1])
1284 .add(Cond[2])
1285 .addMBB(TBB);
1286 if (BytesAdded)
1287 *BytesAdded += getInstSizeInBytes(CondMI);
1288
1289 // One-way conditional branch.
1290 if (!FBB)
1291 return 1;
1292
1293 // Two-way conditional branch.
1294 MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(FBB);
1295 if (BytesAdded)
1296 *BytesAdded += getInstSizeInBytes(MI);
1297 return 2;
1298}
1299
1301 MachineBasicBlock &DestBB,
1302 MachineBasicBlock &RestoreBB,
1303 const DebugLoc &DL, int64_t BrOffset,
1304 RegScavenger *RS) const {
1305 assert(RS && "RegScavenger required for long branching");
1306 assert(MBB.empty() &&
1307 "new block should be inserted for expanding unconditional branch");
1308 assert(MBB.pred_size() == 1);
1309 assert(RestoreBB.empty() &&
1310 "restore block should be inserted for restoring clobbered registers");
1311
1312 MachineFunction *MF = MBB.getParent();
1316
1317 if (!isInt<32>(BrOffset))
1319 "Branch offsets outside of the signed 32-bit range not supported");
1320
1321 // FIXME: A virtual register must be used initially, as the register
1322 // scavenger won't work with empty blocks (SIInstrInfo::insertIndirectBranch
1323 // uses the same workaround).
1324 Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRJALRRegClass);
1325 auto II = MBB.end();
1326 // We may also update the jump target to RestoreBB later.
1327 MachineInstr &MI = *BuildMI(MBB, II, DL, get(RISCV::PseudoJump))
1328 .addReg(ScratchReg, RegState::Define | RegState::Dead)
1329 .addMBB(&DestBB, RISCVII::MO_CALL);
1330
1332 Register TmpGPR =
1333 RS->scavengeRegisterBackwards(RISCV::GPRRegClass, MI.getIterator(),
1334 /*RestoreAfter=*/false, /*SpAdj=*/0,
1335 /*AllowSpill=*/false);
1336 if (TmpGPR != RISCV::NoRegister)
1337 RS->setRegUsed(TmpGPR);
1338 else {
1339 // The case when there is no scavenged register needs special handling.
1340
1341 // Pick s11(or s1 for rve) because it doesn't make a difference.
1342 TmpGPR = STI.hasStdExtE() ? RISCV::X9 : RISCV::X27;
1343
1344 int FrameIndex = RVFI->getBranchRelaxationScratchFrameIndex();
1345 if (FrameIndex == -1)
1346 report_fatal_error("underestimated function size");
1347
1348 storeRegToStackSlot(MBB, MI, TmpGPR, /*IsKill=*/true, FrameIndex,
1349 &RISCV::GPRRegClass, TRI, Register());
1350 TRI->eliminateFrameIndex(std::prev(MI.getIterator()),
1351 /*SpAdj=*/0, /*FIOperandNum=*/1);
1352
1353 MI.getOperand(1).setMBB(&RestoreBB);
1354
1355 loadRegFromStackSlot(RestoreBB, RestoreBB.end(), TmpGPR, FrameIndex,
1356 &RISCV::GPRRegClass, TRI, Register());
1357 TRI->eliminateFrameIndex(RestoreBB.back(),
1358 /*SpAdj=*/0, /*FIOperandNum=*/1);
1359 }
1360
1361 MRI.replaceRegWith(ScratchReg, TmpGPR);
1362 MRI.clearVirtRegs();
1363}
1364
1367 assert((Cond.size() == 3) && "Invalid branch condition!");
1368 switch (Cond[0].getImm()) {
1369 default:
1370 llvm_unreachable("Unknown conditional branch!");
1371 case RISCV::BEQ:
1372 Cond[0].setImm(RISCV::BNE);
1373 break;
1374 case RISCV::BEQI:
1375 Cond[0].setImm(RISCV::BNEI);
1376 break;
1377 case RISCV::BNE:
1378 Cond[0].setImm(RISCV::BEQ);
1379 break;
1380 case RISCV::BNEI:
1381 Cond[0].setImm(RISCV::BEQI);
1382 break;
1383 case RISCV::BLT:
1384 Cond[0].setImm(RISCV::BGE);
1385 break;
1386 case RISCV::BGE:
1387 Cond[0].setImm(RISCV::BLT);
1388 break;
1389 case RISCV::BLTU:
1390 Cond[0].setImm(RISCV::BGEU);
1391 break;
1392 case RISCV::BGEU:
1393 Cond[0].setImm(RISCV::BLTU);
1394 break;
1395 case RISCV::CV_BEQIMM:
1396 Cond[0].setImm(RISCV::CV_BNEIMM);
1397 break;
1398 case RISCV::CV_BNEIMM:
1399 Cond[0].setImm(RISCV::CV_BEQIMM);
1400 break;
1401 case RISCV::QC_BEQI:
1402 Cond[0].setImm(RISCV::QC_BNEI);
1403 break;
1404 case RISCV::QC_BNEI:
1405 Cond[0].setImm(RISCV::QC_BEQI);
1406 break;
1407 case RISCV::QC_BGEI:
1408 Cond[0].setImm(RISCV::QC_BLTI);
1409 break;
1410 case RISCV::QC_BLTI:
1411 Cond[0].setImm(RISCV::QC_BGEI);
1412 break;
1413 case RISCV::QC_BGEUI:
1414 Cond[0].setImm(RISCV::QC_BLTUI);
1415 break;
1416 case RISCV::QC_BLTUI:
1417 Cond[0].setImm(RISCV::QC_BGEUI);
1418 break;
1419 case RISCV::QC_E_BEQI:
1420 Cond[0].setImm(RISCV::QC_E_BNEI);
1421 break;
1422 case RISCV::QC_E_BNEI:
1423 Cond[0].setImm(RISCV::QC_E_BEQI);
1424 break;
1425 case RISCV::QC_E_BGEI:
1426 Cond[0].setImm(RISCV::QC_E_BLTI);
1427 break;
1428 case RISCV::QC_E_BLTI:
1429 Cond[0].setImm(RISCV::QC_E_BGEI);
1430 break;
1431 case RISCV::QC_E_BGEUI:
1432 Cond[0].setImm(RISCV::QC_E_BLTUI);
1433 break;
1434 case RISCV::QC_E_BLTUI:
1435 Cond[0].setImm(RISCV::QC_E_BGEUI);
1436 break;
1437 case RISCV::NDS_BBC:
1438 Cond[0].setImm(RISCV::NDS_BBS);
1439 break;
1440 case RISCV::NDS_BBS:
1441 Cond[0].setImm(RISCV::NDS_BBC);
1442 break;
1443 case RISCV::NDS_BEQC:
1444 Cond[0].setImm(RISCV::NDS_BNEC);
1445 break;
1446 case RISCV::NDS_BNEC:
1447 Cond[0].setImm(RISCV::NDS_BEQC);
1448 break;
1449 }
1450
1451 return false;
1452}
1453
1454// Return true if the instruction is a load immediate instruction (i.e.
1455// ADDI x0, imm).
1456static bool isLoadImm(const MachineInstr *MI, int64_t &Imm) {
1457 if (MI->getOpcode() == RISCV::ADDI && MI->getOperand(1).isReg() &&
1458 MI->getOperand(1).getReg() == RISCV::X0) {
1459 Imm = MI->getOperand(2).getImm();
1460 return true;
1461 }
1462 return false;
1463}
1464
1466 const MachineOperand &Op, int64_t &Imm) {
1467 // Either a load from immediate instruction or X0.
1468 if (!Op.isReg())
1469 return false;
1470
1471 Register Reg = Op.getReg();
1472 if (Reg == RISCV::X0) {
1473 Imm = 0;
1474 return true;
1475 }
1476 return Reg.isVirtual() && isLoadImm(MRI.getVRegDef(Reg), Imm);
1477}
1478
1480 bool IsSigned = false;
1481 bool IsEquality = false;
1482 switch (MI.getOpcode()) {
1483 default:
1484 return false;
1485 case RISCV::BEQ:
1486 case RISCV::BNE:
1487 IsEquality = true;
1488 break;
1489 case RISCV::BGE:
1490 case RISCV::BLT:
1491 IsSigned = true;
1492 break;
1493 case RISCV::BGEU:
1494 case RISCV::BLTU:
1495 break;
1496 }
1497
1498 MachineBasicBlock *MBB = MI.getParent();
1499 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1500
1501 const MachineOperand &LHS = MI.getOperand(0);
1502 const MachineOperand &RHS = MI.getOperand(1);
1503 MachineBasicBlock *TBB = MI.getOperand(2).getMBB();
1504
1505 RISCVCC::CondCode CC = getCondFromBranchOpc(MI.getOpcode());
1507
1508 // Canonicalize conditional branches which can be constant folded into
1509 // beqz or bnez. We can't modify the CFG here.
1510 int64_t C0, C1;
1511 if (isFromLoadImm(MRI, LHS, C0) && isFromLoadImm(MRI, RHS, C1)) {
1512 unsigned NewOpc = evaluateCondBranch(CC, C0, C1) ? RISCV::BEQ : RISCV::BNE;
1513 // Build the new branch and remove the old one.
1514 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc))
1515 .addReg(RISCV::X0)
1516 .addReg(RISCV::X0)
1517 .addMBB(TBB);
1518 MI.eraseFromParent();
1519 return true;
1520 }
1521
1522 if (IsEquality)
1523 return false;
1524
1525 // For two constants C0 and C1 from
1526 // ```
1527 // li Y, C0
1528 // li Z, C1
1529 // ```
1530 // 1. if C1 = C0 + 1
1531 // we can turn:
1532 // (a) blt Y, X -> bge X, Z
1533 // (b) bge Y, X -> blt X, Z
1534 //
1535 // 2. if C1 = C0 - 1
1536 // we can turn:
1537 // (a) blt X, Y -> bge Z, X
1538 // (b) bge X, Y -> blt Z, X
1539 //
1540 // To make sure this optimization is really beneficial, we only
1541 // optimize for cases where Y had only one use (i.e. only used by the branch).
1542 // Try to find the register for constant Z; return
1543 // invalid register otherwise.
1544 auto searchConst = [&](int64_t C1) -> Register {
1546 auto DefC1 = std::find_if(++II, E, [&](const MachineInstr &I) -> bool {
1547 int64_t Imm;
1548 return isLoadImm(&I, Imm) && Imm == C1 &&
1549 I.getOperand(0).getReg().isVirtual();
1550 });
1551 if (DefC1 != E)
1552 return DefC1->getOperand(0).getReg();
1553
1554 return Register();
1555 };
1556
1557 unsigned NewOpc = RISCVCC::getBrCond(getOppositeBranchCondition(CC));
1558
1559 // Might be case 1.
1560 // Don't change 0 to 1 since we can use x0.
1561 // For unsigned cases changing -1U to 0 would be incorrect.
1562 // The incorrect case for signed would be INT_MAX, but isFromLoadImm can't
1563 // return that.
1564 if (isFromLoadImm(MRI, LHS, C0) && C0 != 0 && LHS.getReg().isVirtual() &&
1565 MRI.hasOneUse(LHS.getReg()) && (IsSigned || C0 != -1)) {
1566 assert(isInt<12>(C0) && "Unexpected immediate");
1567 if (Register RegZ = searchConst(C0 + 1)) {
1568 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc))
1569 .add(RHS)
1570 .addReg(RegZ)
1571 .addMBB(TBB);
1572 // We might extend the live range of Z, clear its kill flag to
1573 // account for this.
1574 MRI.clearKillFlags(RegZ);
1575 MI.eraseFromParent();
1576 return true;
1577 }
1578 }
1579
1580 // Might be case 2.
1581 // For signed cases we don't want to change 0 since we can use x0.
1582 // For unsigned cases changing 0 to -1U would be incorrect.
1583 // The incorrect case for signed would be INT_MIN, but isFromLoadImm can't
1584 // return that.
1585 if (isFromLoadImm(MRI, RHS, C0) && C0 != 0 && RHS.getReg().isVirtual() &&
1586 MRI.hasOneUse(RHS.getReg())) {
1587 assert(isInt<12>(C0) && "Unexpected immediate");
1588 if (Register RegZ = searchConst(C0 - 1)) {
1589 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc))
1590 .addReg(RegZ)
1591 .add(LHS)
1592 .addMBB(TBB);
1593 // We might extend the live range of Z, clear its kill flag to
1594 // account for this.
1595 MRI.clearKillFlags(RegZ);
1596 MI.eraseFromParent();
1597 return true;
1598 }
1599 }
1600
1601 return false;
1602}
1603
1606 assert(MI.getDesc().isBranch() && "Unexpected opcode!");
1607 // The branch target is always the last operand.
1608 int NumOp = MI.getNumExplicitOperands();
1609 return MI.getOperand(NumOp - 1).getMBB();
1610}
1611
1613 int64_t BrOffset) const {
1614 unsigned XLen = STI.getXLen();
1615 // Ideally we could determine the supported branch offset from the
1616 // RISCVII::FormMask, but this can't be used for Pseudo instructions like
1617 // PseudoBR.
1618 switch (BranchOp) {
1619 default:
1620 llvm_unreachable("Unexpected opcode!");
1621 case RISCV::NDS_BBC:
1622 case RISCV::NDS_BBS:
1623 case RISCV::NDS_BEQC:
1624 case RISCV::NDS_BNEC:
1625 return isInt<11>(BrOffset);
1626 case RISCV::BEQ:
1627 case RISCV::BNE:
1628 case RISCV::BLT:
1629 case RISCV::BGE:
1630 case RISCV::BLTU:
1631 case RISCV::BGEU:
1632 case RISCV::BEQI:
1633 case RISCV::BNEI:
1634 case RISCV::CV_BEQIMM:
1635 case RISCV::CV_BNEIMM:
1636 case RISCV::QC_BEQI:
1637 case RISCV::QC_BNEI:
1638 case RISCV::QC_BGEI:
1639 case RISCV::QC_BLTI:
1640 case RISCV::QC_BLTUI:
1641 case RISCV::QC_BGEUI:
1642 case RISCV::QC_E_BEQI:
1643 case RISCV::QC_E_BNEI:
1644 case RISCV::QC_E_BGEI:
1645 case RISCV::QC_E_BLTI:
1646 case RISCV::QC_E_BLTUI:
1647 case RISCV::QC_E_BGEUI:
1648 return isInt<13>(BrOffset);
1649 case RISCV::JAL:
1650 case RISCV::PseudoBR:
1651 return isInt<21>(BrOffset);
1652 case RISCV::PseudoJump:
1653 return isInt<32>(SignExtend64(BrOffset + 0x800, XLen));
1654 }
1655}
1656
1657// If the operation has a predicated pseudo instruction, return the pseudo
1658// instruction opcode. Otherwise, return RISCV::INSTRUCTION_LIST_END.
1659// TODO: Support more operations.
1660unsigned getPredicatedOpcode(unsigned Opcode) {
1661 switch (Opcode) {
1662 case RISCV::ADD: return RISCV::PseudoCCADD; break;
1663 case RISCV::SUB: return RISCV::PseudoCCSUB; break;
1664 case RISCV::SLL: return RISCV::PseudoCCSLL; break;
1665 case RISCV::SRL: return RISCV::PseudoCCSRL; break;
1666 case RISCV::SRA: return RISCV::PseudoCCSRA; break;
1667 case RISCV::AND: return RISCV::PseudoCCAND; break;
1668 case RISCV::OR: return RISCV::PseudoCCOR; break;
1669 case RISCV::XOR: return RISCV::PseudoCCXOR; break;
1670
1671 case RISCV::ADDI: return RISCV::PseudoCCADDI; break;
1672 case RISCV::SLLI: return RISCV::PseudoCCSLLI; break;
1673 case RISCV::SRLI: return RISCV::PseudoCCSRLI; break;
1674 case RISCV::SRAI: return RISCV::PseudoCCSRAI; break;
1675 case RISCV::ANDI: return RISCV::PseudoCCANDI; break;
1676 case RISCV::ORI: return RISCV::PseudoCCORI; break;
1677 case RISCV::XORI: return RISCV::PseudoCCXORI; break;
1678
1679 case RISCV::ADDW: return RISCV::PseudoCCADDW; break;
1680 case RISCV::SUBW: return RISCV::PseudoCCSUBW; break;
1681 case RISCV::SLLW: return RISCV::PseudoCCSLLW; break;
1682 case RISCV::SRLW: return RISCV::PseudoCCSRLW; break;
1683 case RISCV::SRAW: return RISCV::PseudoCCSRAW; break;
1684
1685 case RISCV::ADDIW: return RISCV::PseudoCCADDIW; break;
1686 case RISCV::SLLIW: return RISCV::PseudoCCSLLIW; break;
1687 case RISCV::SRLIW: return RISCV::PseudoCCSRLIW; break;
1688 case RISCV::SRAIW: return RISCV::PseudoCCSRAIW; break;
1689
1690 case RISCV::ANDN: return RISCV::PseudoCCANDN; break;
1691 case RISCV::ORN: return RISCV::PseudoCCORN; break;
1692 case RISCV::XNOR: return RISCV::PseudoCCXNOR; break;
1693
1694 case RISCV::NDS_BFOS: return RISCV::PseudoCCNDS_BFOS; break;
1695 case RISCV::NDS_BFOZ: return RISCV::PseudoCCNDS_BFOZ; break;
1696 }
1697
1698 return RISCV::INSTRUCTION_LIST_END;
1699}
1700
1701/// Identify instructions that can be folded into a CCMOV instruction, and
1702/// return the defining instruction.
1704 const MachineRegisterInfo &MRI,
1705 const TargetInstrInfo *TII) {
1706 if (!Reg.isVirtual())
1707 return nullptr;
1708 if (!MRI.hasOneNonDBGUse(Reg))
1709 return nullptr;
1710 MachineInstr *MI = MRI.getVRegDef(Reg);
1711 if (!MI)
1712 return nullptr;
1713 // Check if MI can be predicated and folded into the CCMOV.
1714 if (getPredicatedOpcode(MI->getOpcode()) == RISCV::INSTRUCTION_LIST_END)
1715 return nullptr;
1716 // Don't predicate li idiom.
1717 if (MI->getOpcode() == RISCV::ADDI && MI->getOperand(1).isReg() &&
1718 MI->getOperand(1).getReg() == RISCV::X0)
1719 return nullptr;
1720 // Check if MI has any other defs or physreg uses.
1721 for (const MachineOperand &MO : llvm::drop_begin(MI->operands())) {
1722 // Reject frame index operands, PEI can't handle the predicated pseudos.
1723 if (MO.isFI() || MO.isCPI() || MO.isJTI())
1724 return nullptr;
1725 if (!MO.isReg())
1726 continue;
1727 // MI can't have any tied operands, that would conflict with predication.
1728 if (MO.isTied())
1729 return nullptr;
1730 if (MO.isDef())
1731 return nullptr;
1732 // Allow constant physregs.
1733 if (MO.getReg().isPhysical() && !MRI.isConstantPhysReg(MO.getReg()))
1734 return nullptr;
1735 }
1736 bool DontMoveAcrossStores = true;
1737 if (!MI->isSafeToMove(DontMoveAcrossStores))
1738 return nullptr;
1739 return MI;
1740}
1741
1744 unsigned &TrueOp, unsigned &FalseOp,
1745 bool &Optimizable) const {
1746 assert(MI.getOpcode() == RISCV::PseudoCCMOVGPR &&
1747 "Unknown select instruction");
1748 // CCMOV operands:
1749 // 0: Def.
1750 // 1: LHS of compare.
1751 // 2: RHS of compare.
1752 // 3: Condition code.
1753 // 4: False use.
1754 // 5: True use.
1755 TrueOp = 5;
1756 FalseOp = 4;
1757 Cond.push_back(MI.getOperand(1));
1758 Cond.push_back(MI.getOperand(2));
1759 Cond.push_back(MI.getOperand(3));
1760 // We can only fold when we support short forward branch opt.
1761 Optimizable = STI.hasShortForwardBranchOpt();
1762 return false;
1763}
1764
1768 bool PreferFalse) const {
1769 assert(MI.getOpcode() == RISCV::PseudoCCMOVGPR &&
1770 "Unknown select instruction");
1771 if (!STI.hasShortForwardBranchOpt())
1772 return nullptr;
1773
1774 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1776 canFoldAsPredicatedOp(MI.getOperand(5).getReg(), MRI, this);
1777 bool Invert = !DefMI;
1778 if (!DefMI)
1779 DefMI = canFoldAsPredicatedOp(MI.getOperand(4).getReg(), MRI, this);
1780 if (!DefMI)
1781 return nullptr;
1782
1783 // Find new register class to use.
1784 MachineOperand FalseReg = MI.getOperand(Invert ? 5 : 4);
1785 Register DestReg = MI.getOperand(0).getReg();
1786 const TargetRegisterClass *PreviousClass = MRI.getRegClass(FalseReg.getReg());
1787 if (!MRI.constrainRegClass(DestReg, PreviousClass))
1788 return nullptr;
1789
1790 unsigned PredOpc = getPredicatedOpcode(DefMI->getOpcode());
1791 assert(PredOpc != RISCV::INSTRUCTION_LIST_END && "Unexpected opcode!");
1792
1793 // Create a new predicated version of DefMI.
1794 MachineInstrBuilder NewMI =
1795 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(PredOpc), DestReg);
1796
1797 // Copy the condition portion.
1798 NewMI.add(MI.getOperand(1));
1799 NewMI.add(MI.getOperand(2));
1800
1801 // Add condition code, inverting if necessary.
1802 auto CC = static_cast<RISCVCC::CondCode>(MI.getOperand(3).getImm());
1803 if (Invert)
1805 NewMI.addImm(CC);
1806
1807 // Copy the false register.
1808 NewMI.add(FalseReg);
1809
1810 // Copy all the DefMI operands.
1811 const MCInstrDesc &DefDesc = DefMI->getDesc();
1812 for (unsigned i = 1, e = DefDesc.getNumOperands(); i != e; ++i)
1813 NewMI.add(DefMI->getOperand(i));
1814
1815 // Update SeenMIs set: register newly created MI and erase removed DefMI.
1816 SeenMIs.insert(NewMI);
1817 SeenMIs.erase(DefMI);
1818
1819 // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
1820 // DefMI would be invalid when transferred inside the loop. Checking for a
1821 // loop is expensive, but at least remove kill flags if they are in different
1822 // BBs.
1823 if (DefMI->getParent() != MI.getParent())
1824 NewMI->clearKillInfo();
1825
1826 // The caller will erase MI, but not DefMI.
1827 DefMI->eraseFromParent();
1828 return NewMI;
1829}
1830
1832 if (MI.isMetaInstruction())
1833 return 0;
1834
1835 unsigned Opcode = MI.getOpcode();
1836
1837 if (Opcode == TargetOpcode::INLINEASM ||
1838 Opcode == TargetOpcode::INLINEASM_BR) {
1839 const MachineFunction &MF = *MI.getParent()->getParent();
1840 return getInlineAsmLength(MI.getOperand(0).getSymbolName(),
1841 *MF.getTarget().getMCAsmInfo());
1842 }
1843
1844 if (!MI.memoperands_empty()) {
1845 MachineMemOperand *MMO = *(MI.memoperands_begin());
1846 if (STI.hasStdExtZihintntl() && MMO->isNonTemporal()) {
1847 if (STI.hasStdExtZca()) {
1848 if (isCompressibleInst(MI, STI))
1849 return 4; // c.ntl.all + c.load/c.store
1850 return 6; // c.ntl.all + load/store
1851 }
1852 return 8; // ntl.all + load/store
1853 }
1854 }
1855
1856 if (Opcode == TargetOpcode::BUNDLE)
1857 return getInstBundleLength(MI);
1858
1859 if (MI.getParent() && MI.getParent()->getParent()) {
1860 if (isCompressibleInst(MI, STI))
1861 return 2;
1862 }
1863
1864 switch (Opcode) {
1865 case RISCV::PseudoMV_FPR16INX:
1866 case RISCV::PseudoMV_FPR32INX:
1867 // MV is always compressible to either c.mv or c.li rd, 0.
1868 return STI.hasStdExtZca() ? 2 : 4;
1869 case TargetOpcode::STACKMAP:
1870 // The upper bound for a stackmap intrinsic is the full length of its shadow
1872 case TargetOpcode::PATCHPOINT:
1873 // The size of the patchpoint intrinsic is the number of bytes requested
1875 case TargetOpcode::STATEPOINT: {
1876 // The size of the statepoint intrinsic is the number of bytes requested
1877 unsigned NumBytes = StatepointOpers(&MI).getNumPatchBytes();
1878 // No patch bytes means at most a PseudoCall is emitted
1879 return std::max(NumBytes, 8U);
1880 }
1881 case TargetOpcode::PATCHABLE_FUNCTION_ENTER:
1882 case TargetOpcode::PATCHABLE_FUNCTION_EXIT:
1883 case TargetOpcode::PATCHABLE_TAIL_CALL: {
1884 const MachineFunction &MF = *MI.getParent()->getParent();
1885 const Function &F = MF.getFunction();
1886 if (Opcode == TargetOpcode::PATCHABLE_FUNCTION_ENTER &&
1887 F.hasFnAttribute("patchable-function-entry")) {
1888 unsigned Num;
1889 if (F.getFnAttribute("patchable-function-entry")
1890 .getValueAsString()
1891 .getAsInteger(10, Num))
1892 return get(Opcode).getSize();
1893
1894 // Number of C.NOP or NOP
1895 return (STI.hasStdExtZca() ? 2 : 4) * Num;
1896 }
1897 // XRay uses C.JAL + 21 or 33 C.NOP for each sled in RV32 and RV64,
1898 // respectively.
1899 return STI.is64Bit() ? 68 : 44;
1900 }
1901 default:
1902 return get(Opcode).getSize();
1903 }
1904}
1905
1906unsigned RISCVInstrInfo::getInstBundleLength(const MachineInstr &MI) const {
1907 unsigned Size = 0;
1909 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
1910 while (++I != E && I->isInsideBundle()) {
1911 assert(!I->isBundle() && "No nested bundle!");
1913 }
1914 return Size;
1915}
1916
1918 const unsigned Opcode = MI.getOpcode();
1919 switch (Opcode) {
1920 default:
1921 break;
1922 case RISCV::FSGNJ_D:
1923 case RISCV::FSGNJ_S:
1924 case RISCV::FSGNJ_H:
1925 case RISCV::FSGNJ_D_INX:
1926 case RISCV::FSGNJ_D_IN32X:
1927 case RISCV::FSGNJ_S_INX:
1928 case RISCV::FSGNJ_H_INX:
1929 // The canonical floating-point move is fsgnj rd, rs, rs.
1930 return MI.getOperand(1).isReg() && MI.getOperand(2).isReg() &&
1931 MI.getOperand(1).getReg() == MI.getOperand(2).getReg();
1932 case RISCV::ADDI:
1933 case RISCV::ORI:
1934 case RISCV::XORI:
1935 return (MI.getOperand(1).isReg() &&
1936 MI.getOperand(1).getReg() == RISCV::X0) ||
1937 (MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0);
1938 }
1939 return MI.isAsCheapAsAMove();
1940}
1941
1942std::optional<DestSourcePair>
1944 if (MI.isMoveReg())
1945 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
1946 switch (MI.getOpcode()) {
1947 default:
1948 break;
1949 case RISCV::ADD:
1950 case RISCV::OR:
1951 case RISCV::XOR:
1952 if (MI.getOperand(1).isReg() && MI.getOperand(1).getReg() == RISCV::X0 &&
1953 MI.getOperand(2).isReg())
1954 return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
1955 if (MI.getOperand(2).isReg() && MI.getOperand(2).getReg() == RISCV::X0 &&
1956 MI.getOperand(1).isReg())
1957 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
1958 break;
1959 case RISCV::ADDI:
1960 // Operand 1 can be a frameindex but callers expect registers
1961 if (MI.getOperand(1).isReg() && MI.getOperand(2).isImm() &&
1962 MI.getOperand(2).getImm() == 0)
1963 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
1964 break;
1965 case RISCV::SUB:
1966 if (MI.getOperand(2).isReg() && MI.getOperand(2).getReg() == RISCV::X0 &&
1967 MI.getOperand(1).isReg())
1968 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
1969 break;
1970 case RISCV::SH1ADD:
1971 case RISCV::SH1ADD_UW:
1972 case RISCV::SH2ADD:
1973 case RISCV::SH2ADD_UW:
1974 case RISCV::SH3ADD:
1975 case RISCV::SH3ADD_UW:
1976 if (MI.getOperand(1).isReg() && MI.getOperand(1).getReg() == RISCV::X0 &&
1977 MI.getOperand(2).isReg())
1978 return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
1979 break;
1980 case RISCV::FSGNJ_D:
1981 case RISCV::FSGNJ_S:
1982 case RISCV::FSGNJ_H:
1983 case RISCV::FSGNJ_D_INX:
1984 case RISCV::FSGNJ_D_IN32X:
1985 case RISCV::FSGNJ_S_INX:
1986 case RISCV::FSGNJ_H_INX:
1987 // The canonical floating-point move is fsgnj rd, rs, rs.
1988 if (MI.getOperand(1).isReg() && MI.getOperand(2).isReg() &&
1989 MI.getOperand(1).getReg() == MI.getOperand(2).getReg())
1990 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
1991 break;
1992 }
1993 return std::nullopt;
1994}
1995
1997 if (ForceMachineCombinerStrategy.getNumOccurrences() == 0) {
1998 // The option is unused. Choose Local strategy only for in-order cores. When
1999 // scheduling model is unspecified, use MinInstrCount strategy as more
2000 // generic one.
2001 const auto &SchedModel = STI.getSchedModel();
2002 return (!SchedModel.hasInstrSchedModel() || SchedModel.isOutOfOrder())
2005 }
2006 // The strategy was forced by the option.
2008}
2009
2011 MachineInstr &Root, unsigned &Pattern,
2012 SmallVectorImpl<MachineInstr *> &InsInstrs) const {
2013 int16_t FrmOpIdx =
2014 RISCV::getNamedOperandIdx(Root.getOpcode(), RISCV::OpName::frm);
2015 if (FrmOpIdx < 0) {
2016 assert(all_of(InsInstrs,
2017 [](MachineInstr *MI) {
2018 return RISCV::getNamedOperandIdx(MI->getOpcode(),
2019 RISCV::OpName::frm) < 0;
2020 }) &&
2021 "New instructions require FRM whereas the old one does not have it");
2022 return;
2023 }
2024
2025 const MachineOperand &FRM = Root.getOperand(FrmOpIdx);
2026 MachineFunction &MF = *Root.getMF();
2027
2028 for (auto *NewMI : InsInstrs) {
2029 // We'd already added the FRM operand.
2030 if (static_cast<unsigned>(RISCV::getNamedOperandIdx(
2031 NewMI->getOpcode(), RISCV::OpName::frm)) != NewMI->getNumOperands())
2032 continue;
2033 MachineInstrBuilder MIB(MF, NewMI);
2034 MIB.add(FRM);
2035 if (FRM.getImm() == RISCVFPRndMode::DYN)
2036 MIB.addUse(RISCV::FRM, RegState::Implicit);
2037 }
2038}
2039
2040static bool isFADD(unsigned Opc) {
2041 switch (Opc) {
2042 default:
2043 return false;
2044 case RISCV::FADD_H:
2045 case RISCV::FADD_S:
2046 case RISCV::FADD_D:
2047 return true;
2048 }
2049}
2050
2051static bool isFSUB(unsigned Opc) {
2052 switch (Opc) {
2053 default:
2054 return false;
2055 case RISCV::FSUB_H:
2056 case RISCV::FSUB_S:
2057 case RISCV::FSUB_D:
2058 return true;
2059 }
2060}
2061
2062static bool isFMUL(unsigned Opc) {
2063 switch (Opc) {
2064 default:
2065 return false;
2066 case RISCV::FMUL_H:
2067 case RISCV::FMUL_S:
2068 case RISCV::FMUL_D:
2069 return true;
2070 }
2071}
2072
2073bool RISCVInstrInfo::isVectorAssociativeAndCommutative(const MachineInstr &Inst,
2074 bool Invert) const {
2075#define OPCODE_LMUL_CASE(OPC) \
2076 case RISCV::OPC##_M1: \
2077 case RISCV::OPC##_M2: \
2078 case RISCV::OPC##_M4: \
2079 case RISCV::OPC##_M8: \
2080 case RISCV::OPC##_MF2: \
2081 case RISCV::OPC##_MF4: \
2082 case RISCV::OPC##_MF8
2083
2084#define OPCODE_LMUL_MASK_CASE(OPC) \
2085 case RISCV::OPC##_M1_MASK: \
2086 case RISCV::OPC##_M2_MASK: \
2087 case RISCV::OPC##_M4_MASK: \
2088 case RISCV::OPC##_M8_MASK: \
2089 case RISCV::OPC##_MF2_MASK: \
2090 case RISCV::OPC##_MF4_MASK: \
2091 case RISCV::OPC##_MF8_MASK
2092
2093 unsigned Opcode = Inst.getOpcode();
2094 if (Invert) {
2095 if (auto InvOpcode = getInverseOpcode(Opcode))
2096 Opcode = *InvOpcode;
2097 else
2098 return false;
2099 }
2100
2101 // clang-format off
2102 switch (Opcode) {
2103 default:
2104 return false;
2105 OPCODE_LMUL_CASE(PseudoVADD_VV):
2106 OPCODE_LMUL_MASK_CASE(PseudoVADD_VV):
2107 OPCODE_LMUL_CASE(PseudoVMUL_VV):
2108 OPCODE_LMUL_MASK_CASE(PseudoVMUL_VV):
2109 return true;
2110 }
2111 // clang-format on
2112
2113#undef OPCODE_LMUL_MASK_CASE
2114#undef OPCODE_LMUL_CASE
2115}
2116
2117bool RISCVInstrInfo::areRVVInstsReassociable(const MachineInstr &Root,
2118 const MachineInstr &Prev) const {
2119 if (!areOpcodesEqualOrInverse(Root.getOpcode(), Prev.getOpcode()))
2120 return false;
2121
2122 assert(Root.getMF() == Prev.getMF());
2123 const MachineRegisterInfo *MRI = &Root.getMF()->getRegInfo();
2124 const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
2125
2126 // Make sure vtype operands are also the same.
2127 const MCInstrDesc &Desc = get(Root.getOpcode());
2128 const uint64_t TSFlags = Desc.TSFlags;
2129
2130 auto checkImmOperand = [&](unsigned OpIdx) {
2131 return Root.getOperand(OpIdx).getImm() == Prev.getOperand(OpIdx).getImm();
2132 };
2133
2134 auto checkRegOperand = [&](unsigned OpIdx) {
2135 return Root.getOperand(OpIdx).getReg() == Prev.getOperand(OpIdx).getReg();
2136 };
2137
2138 // PassThru
2139 // TODO: Potentially we can loosen the condition to consider Root to be
2140 // associable with Prev if Root has NoReg as passthru. In which case we
2141 // also need to loosen the condition on vector policies between these.
2142 if (!checkRegOperand(1))
2143 return false;
2144
2145 // SEW
2146 if (RISCVII::hasSEWOp(TSFlags) &&
2147 !checkImmOperand(RISCVII::getSEWOpNum(Desc)))
2148 return false;
2149
2150 // Mask
2151 if (RISCVII::usesMaskPolicy(TSFlags)) {
2152 const MachineBasicBlock *MBB = Root.getParent();
2155 Register MI1VReg;
2156
2157 bool SeenMI2 = false;
2158 for (auto End = MBB->rend(), It = It1; It != End; ++It) {
2159 if (It == It2) {
2160 SeenMI2 = true;
2161 if (!MI1VReg.isValid())
2162 // There is no V0 def between Root and Prev; they're sharing the
2163 // same V0.
2164 break;
2165 }
2166
2167 if (It->modifiesRegister(RISCV::V0, TRI)) {
2168 Register SrcReg = It->getOperand(1).getReg();
2169 // If it's not VReg it'll be more difficult to track its defs, so
2170 // bailing out here just to be safe.
2171 if (!SrcReg.isVirtual())
2172 return false;
2173
2174 if (!MI1VReg.isValid()) {
2175 // This is the V0 def for Root.
2176 MI1VReg = SrcReg;
2177 continue;
2178 }
2179
2180 // Some random mask updates.
2181 if (!SeenMI2)
2182 continue;
2183
2184 // This is the V0 def for Prev; check if it's the same as that of
2185 // Root.
2186 if (MI1VReg != SrcReg)
2187 return false;
2188 else
2189 break;
2190 }
2191 }
2192
2193 // If we haven't encountered Prev, it's likely that this function was
2194 // called in a wrong way (e.g. Root is before Prev).
2195 assert(SeenMI2 && "Prev is expected to appear before Root");
2196 }
2197
2198 // Tail / Mask policies
2199 if (RISCVII::hasVecPolicyOp(TSFlags) &&
2200 !checkImmOperand(RISCVII::getVecPolicyOpNum(Desc)))
2201 return false;
2202
2203 // VL
2204 if (RISCVII::hasVLOp(TSFlags)) {
2205 unsigned OpIdx = RISCVII::getVLOpNum(Desc);
2206 const MachineOperand &Op1 = Root.getOperand(OpIdx);
2207 const MachineOperand &Op2 = Prev.getOperand(OpIdx);
2208 if (Op1.getType() != Op2.getType())
2209 return false;
2210 switch (Op1.getType()) {
2212 if (Op1.getReg() != Op2.getReg())
2213 return false;
2214 break;
2216 if (Op1.getImm() != Op2.getImm())
2217 return false;
2218 break;
2219 default:
2220 llvm_unreachable("Unrecognized VL operand type");
2221 }
2222 }
2223
2224 // Rounding modes
2225 if (RISCVII::hasRoundModeOp(TSFlags) &&
2226 !checkImmOperand(RISCVII::getVLOpNum(Desc) - 1))
2227 return false;
2228
2229 return true;
2230}
2231
2232// Most of our RVV pseudos have passthru operand, so the real operands
2233// start from index = 2.
2234bool RISCVInstrInfo::hasReassociableVectorSibling(const MachineInstr &Inst,
2235 bool &Commuted) const {
2236 const MachineBasicBlock *MBB = Inst.getParent();
2237 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
2239 "Expect the present of passthrough operand.");
2240 MachineInstr *MI1 = MRI.getUniqueVRegDef(Inst.getOperand(2).getReg());
2241 MachineInstr *MI2 = MRI.getUniqueVRegDef(Inst.getOperand(3).getReg());
2242
2243 // If only one operand has the same or inverse opcode and it's the second
2244 // source operand, the operands must be commuted.
2245 Commuted = !areRVVInstsReassociable(Inst, *MI1) &&
2246 areRVVInstsReassociable(Inst, *MI2);
2247 if (Commuted)
2248 std::swap(MI1, MI2);
2249
2250 return areRVVInstsReassociable(Inst, *MI1) &&
2251 (isVectorAssociativeAndCommutative(*MI1) ||
2252 isVectorAssociativeAndCommutative(*MI1, /* Invert */ true)) &&
2254 MRI.hasOneNonDBGUse(MI1->getOperand(0).getReg());
2255}
2256
2258 const MachineInstr &Inst, const MachineBasicBlock *MBB) const {
2259 if (!isVectorAssociativeAndCommutative(Inst) &&
2260 !isVectorAssociativeAndCommutative(Inst, /*Invert=*/true))
2262
2263 const MachineOperand &Op1 = Inst.getOperand(2);
2264 const MachineOperand &Op2 = Inst.getOperand(3);
2265 const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
2266
2267 // We need virtual register definitions for the operands that we will
2268 // reassociate.
2269 MachineInstr *MI1 = nullptr;
2270 MachineInstr *MI2 = nullptr;
2271 if (Op1.isReg() && Op1.getReg().isVirtual())
2272 MI1 = MRI.getUniqueVRegDef(Op1.getReg());
2273 if (Op2.isReg() && Op2.getReg().isVirtual())
2274 MI2 = MRI.getUniqueVRegDef(Op2.getReg());
2275
2276 // And at least one operand must be defined in MBB.
2277 return MI1 && MI2 && (MI1->getParent() == MBB || MI2->getParent() == MBB);
2278}
2279
2281 const MachineInstr &Root, unsigned Pattern,
2282 std::array<unsigned, 5> &OperandIndices) const {
2284 if (RISCV::getRVVMCOpcode(Root.getOpcode())) {
2285 // Skip the passthrough operand, so increment all indices by one.
2286 for (unsigned I = 0; I < 5; ++I)
2287 ++OperandIndices[I];
2288 }
2289}
2290
2292 bool &Commuted) const {
2293 if (isVectorAssociativeAndCommutative(Inst) ||
2294 isVectorAssociativeAndCommutative(Inst, /*Invert=*/true))
2295 return hasReassociableVectorSibling(Inst, Commuted);
2296
2297 if (!TargetInstrInfo::hasReassociableSibling(Inst, Commuted))
2298 return false;
2299
2300 const MachineRegisterInfo &MRI = Inst.getMF()->getRegInfo();
2301 unsigned OperandIdx = Commuted ? 2 : 1;
2302 const MachineInstr &Sibling =
2303 *MRI.getVRegDef(Inst.getOperand(OperandIdx).getReg());
2304
2305 int16_t InstFrmOpIdx =
2306 RISCV::getNamedOperandIdx(Inst.getOpcode(), RISCV::OpName::frm);
2307 int16_t SiblingFrmOpIdx =
2308 RISCV::getNamedOperandIdx(Sibling.getOpcode(), RISCV::OpName::frm);
2309
2310 return (InstFrmOpIdx < 0 && SiblingFrmOpIdx < 0) ||
2311 RISCV::hasEqualFRM(Inst, Sibling);
2312}
2313
2315 bool Invert) const {
2316 if (isVectorAssociativeAndCommutative(Inst, Invert))
2317 return true;
2318
2319 unsigned Opc = Inst.getOpcode();
2320 if (Invert) {
2321 auto InverseOpcode = getInverseOpcode(Opc);
2322 if (!InverseOpcode)
2323 return false;
2324 Opc = *InverseOpcode;
2325 }
2326
2327 if (isFADD(Opc) || isFMUL(Opc))
2330
2331 switch (Opc) {
2332 default:
2333 return false;
2334 case RISCV::ADD:
2335 case RISCV::ADDW:
2336 case RISCV::AND:
2337 case RISCV::OR:
2338 case RISCV::XOR:
2339 // From RISC-V ISA spec, if both the high and low bits of the same product
2340 // are required, then the recommended code sequence is:
2341 //
2342 // MULH[[S]U] rdh, rs1, rs2
2343 // MUL rdl, rs1, rs2
2344 // (source register specifiers must be in same order and rdh cannot be the
2345 // same as rs1 or rs2)
2346 //
2347 // Microarchitectures can then fuse these into a single multiply operation
2348 // instead of performing two separate multiplies.
2349 // MachineCombiner may reassociate MUL operands and lose the fusion
2350 // opportunity.
2351 case RISCV::MUL:
2352 case RISCV::MULW:
2353 case RISCV::MIN:
2354 case RISCV::MINU:
2355 case RISCV::MAX:
2356 case RISCV::MAXU:
2357 case RISCV::FMIN_H:
2358 case RISCV::FMIN_S:
2359 case RISCV::FMIN_D:
2360 case RISCV::FMAX_H:
2361 case RISCV::FMAX_S:
2362 case RISCV::FMAX_D:
2363 return true;
2364 }
2365
2366 return false;
2367}
2368
2369std::optional<unsigned>
2370RISCVInstrInfo::getInverseOpcode(unsigned Opcode) const {
2371#define RVV_OPC_LMUL_CASE(OPC, INV) \
2372 case RISCV::OPC##_M1: \
2373 return RISCV::INV##_M1; \
2374 case RISCV::OPC##_M2: \
2375 return RISCV::INV##_M2; \
2376 case RISCV::OPC##_M4: \
2377 return RISCV::INV##_M4; \
2378 case RISCV::OPC##_M8: \
2379 return RISCV::INV##_M8; \
2380 case RISCV::OPC##_MF2: \
2381 return RISCV::INV##_MF2; \
2382 case RISCV::OPC##_MF4: \
2383 return RISCV::INV##_MF4; \
2384 case RISCV::OPC##_MF8: \
2385 return RISCV::INV##_MF8
2386
2387#define RVV_OPC_LMUL_MASK_CASE(OPC, INV) \
2388 case RISCV::OPC##_M1_MASK: \
2389 return RISCV::INV##_M1_MASK; \
2390 case RISCV::OPC##_M2_MASK: \
2391 return RISCV::INV##_M2_MASK; \
2392 case RISCV::OPC##_M4_MASK: \
2393 return RISCV::INV##_M4_MASK; \
2394 case RISCV::OPC##_M8_MASK: \
2395 return RISCV::INV##_M8_MASK; \
2396 case RISCV::OPC##_MF2_MASK: \
2397 return RISCV::INV##_MF2_MASK; \
2398 case RISCV::OPC##_MF4_MASK: \
2399 return RISCV::INV##_MF4_MASK; \
2400 case RISCV::OPC##_MF8_MASK: \
2401 return RISCV::INV##_MF8_MASK
2402
2403 switch (Opcode) {
2404 default:
2405 return std::nullopt;
2406 case RISCV::FADD_H:
2407 return RISCV::FSUB_H;
2408 case RISCV::FADD_S:
2409 return RISCV::FSUB_S;
2410 case RISCV::FADD_D:
2411 return RISCV::FSUB_D;
2412 case RISCV::FSUB_H:
2413 return RISCV::FADD_H;
2414 case RISCV::FSUB_S:
2415 return RISCV::FADD_S;
2416 case RISCV::FSUB_D:
2417 return RISCV::FADD_D;
2418 case RISCV::ADD:
2419 return RISCV::SUB;
2420 case RISCV::SUB:
2421 return RISCV::ADD;
2422 case RISCV::ADDW:
2423 return RISCV::SUBW;
2424 case RISCV::SUBW:
2425 return RISCV::ADDW;
2426 // clang-format off
2427 RVV_OPC_LMUL_CASE(PseudoVADD_VV, PseudoVSUB_VV);
2428 RVV_OPC_LMUL_MASK_CASE(PseudoVADD_VV, PseudoVSUB_VV);
2429 RVV_OPC_LMUL_CASE(PseudoVSUB_VV, PseudoVADD_VV);
2430 RVV_OPC_LMUL_MASK_CASE(PseudoVSUB_VV, PseudoVADD_VV);
2431 // clang-format on
2432 }
2433
2434#undef RVV_OPC_LMUL_MASK_CASE
2435#undef RVV_OPC_LMUL_CASE
2436}
2437
2439 const MachineOperand &MO,
2440 bool DoRegPressureReduce) {
2441 if (!MO.isReg() || !MO.getReg().isVirtual())
2442 return false;
2443 const MachineRegisterInfo &MRI = Root.getMF()->getRegInfo();
2444 MachineInstr *MI = MRI.getVRegDef(MO.getReg());
2445 if (!MI || !isFMUL(MI->getOpcode()))
2446 return false;
2447
2450 return false;
2451
2452 // Try combining even if fmul has more than one use as it eliminates
2453 // dependency between fadd(fsub) and fmul. However, it can extend liveranges
2454 // for fmul operands, so reject the transformation in register pressure
2455 // reduction mode.
2456 if (DoRegPressureReduce && !MRI.hasOneNonDBGUse(MI->getOperand(0).getReg()))
2457 return false;
2458
2459 // Do not combine instructions from different basic blocks.
2460 if (Root.getParent() != MI->getParent())
2461 return false;
2462 return RISCV::hasEqualFRM(Root, *MI);
2463}
2464
2466 SmallVectorImpl<unsigned> &Patterns,
2467 bool DoRegPressureReduce) {
2468 unsigned Opc = Root.getOpcode();
2469 bool IsFAdd = isFADD(Opc);
2470 if (!IsFAdd && !isFSUB(Opc))
2471 return false;
2472 bool Added = false;
2473 if (canCombineFPFusedMultiply(Root, Root.getOperand(1),
2474 DoRegPressureReduce)) {
2477 Added = true;
2478 }
2479 if (canCombineFPFusedMultiply(Root, Root.getOperand(2),
2480 DoRegPressureReduce)) {
2483 Added = true;
2484 }
2485 return Added;
2486}
2487
2488static bool getFPPatterns(MachineInstr &Root,
2489 SmallVectorImpl<unsigned> &Patterns,
2490 bool DoRegPressureReduce) {
2491 return getFPFusedMultiplyPatterns(Root, Patterns, DoRegPressureReduce);
2492}
2493
2494/// Utility routine that checks if \param MO is defined by an
2495/// \param CombineOpc instruction in the basic block \param MBB
2497 const MachineOperand &MO,
2498 unsigned CombineOpc) {
2499 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2500 const MachineInstr *MI = nullptr;
2501
2502 if (MO.isReg() && MO.getReg().isVirtual())
2503 MI = MRI.getUniqueVRegDef(MO.getReg());
2504 // And it needs to be in the trace (otherwise, it won't have a depth).
2505 if (!MI || MI->getParent() != &MBB || MI->getOpcode() != CombineOpc)
2506 return nullptr;
2507 // Must only used by the user we combine with.
2508 if (!MRI.hasOneNonDBGUse(MI->getOperand(0).getReg()))
2509 return nullptr;
2510
2511 return MI;
2512}
2513
2514/// Utility routine that checks if \param MO is defined by a SLLI in \param
2515/// MBB that can be combined by splitting across 2 SHXADD instructions. The
2516/// first SHXADD shift amount is given by \param OuterShiftAmt.
2518 const MachineOperand &MO,
2519 unsigned OuterShiftAmt) {
2520 const MachineInstr *ShiftMI = canCombine(MBB, MO, RISCV::SLLI);
2521 if (!ShiftMI)
2522 return false;
2523
2524 unsigned InnerShiftAmt = ShiftMI->getOperand(2).getImm();
2525 if (InnerShiftAmt < OuterShiftAmt || (InnerShiftAmt - OuterShiftAmt) > 3)
2526 return false;
2527
2528 return true;
2529}
2530
2531// Returns the shift amount from a SHXADD instruction. Returns 0 if the
2532// instruction is not a SHXADD.
2533static unsigned getSHXADDShiftAmount(unsigned Opc) {
2534 switch (Opc) {
2535 default:
2536 return 0;
2537 case RISCV::SH1ADD:
2538 return 1;
2539 case RISCV::SH2ADD:
2540 return 2;
2541 case RISCV::SH3ADD:
2542 return 3;
2543 }
2544}
2545
2546// Returns the shift amount from a SHXADD.UW instruction. Returns 0 if the
2547// instruction is not a SHXADD.UW.
2548static unsigned getSHXADDUWShiftAmount(unsigned Opc) {
2549 switch (Opc) {
2550 default:
2551 return 0;
2552 case RISCV::SH1ADD_UW:
2553 return 1;
2554 case RISCV::SH2ADD_UW:
2555 return 2;
2556 case RISCV::SH3ADD_UW:
2557 return 3;
2558 }
2559}
2560
2561// Look for opportunities to combine (sh3add Z, (add X, (slli Y, 5))) into
2562// (sh3add (sh2add Y, Z), X).
2563static bool getSHXADDPatterns(const MachineInstr &Root,
2564 SmallVectorImpl<unsigned> &Patterns) {
2565 unsigned ShiftAmt = getSHXADDShiftAmount(Root.getOpcode());
2566 if (!ShiftAmt)
2567 return false;
2568
2569 const MachineBasicBlock &MBB = *Root.getParent();
2570
2571 const MachineInstr *AddMI = canCombine(MBB, Root.getOperand(2), RISCV::ADD);
2572 if (!AddMI)
2573 return false;
2574
2575 bool Found = false;
2576 if (canCombineShiftIntoShXAdd(MBB, AddMI->getOperand(1), ShiftAmt)) {
2578 Found = true;
2579 }
2580 if (canCombineShiftIntoShXAdd(MBB, AddMI->getOperand(2), ShiftAmt)) {
2582 Found = true;
2583 }
2584
2585 return Found;
2586}
2587
2599
2601 MachineInstr &Root, SmallVectorImpl<unsigned> &Patterns,
2602 bool DoRegPressureReduce) const {
2603
2604 if (getFPPatterns(Root, Patterns, DoRegPressureReduce))
2605 return true;
2606
2607 if (getSHXADDPatterns(Root, Patterns))
2608 return true;
2609
2610 return TargetInstrInfo::getMachineCombinerPatterns(Root, Patterns,
2611 DoRegPressureReduce);
2612}
2613
2614static unsigned getFPFusedMultiplyOpcode(unsigned RootOpc, unsigned Pattern) {
2615 switch (RootOpc) {
2616 default:
2617 llvm_unreachable("Unexpected opcode");
2618 case RISCV::FADD_H:
2619 return RISCV::FMADD_H;
2620 case RISCV::FADD_S:
2621 return RISCV::FMADD_S;
2622 case RISCV::FADD_D:
2623 return RISCV::FMADD_D;
2624 case RISCV::FSUB_H:
2625 return Pattern == RISCVMachineCombinerPattern::FMSUB ? RISCV::FMSUB_H
2626 : RISCV::FNMSUB_H;
2627 case RISCV::FSUB_S:
2628 return Pattern == RISCVMachineCombinerPattern::FMSUB ? RISCV::FMSUB_S
2629 : RISCV::FNMSUB_S;
2630 case RISCV::FSUB_D:
2631 return Pattern == RISCVMachineCombinerPattern::FMSUB ? RISCV::FMSUB_D
2632 : RISCV::FNMSUB_D;
2633 }
2634}
2635
2636static unsigned getAddendOperandIdx(unsigned Pattern) {
2637 switch (Pattern) {
2638 default:
2639 llvm_unreachable("Unexpected pattern");
2642 return 2;
2645 return 1;
2646 }
2647}
2648
2650 unsigned Pattern,
2653 MachineFunction *MF = Root.getMF();
2656
2657 MachineOperand &Mul1 = Prev.getOperand(1);
2658 MachineOperand &Mul2 = Prev.getOperand(2);
2659 MachineOperand &Dst = Root.getOperand(0);
2661
2662 Register DstReg = Dst.getReg();
2663 unsigned FusedOpc = getFPFusedMultiplyOpcode(Root.getOpcode(), Pattern);
2664 uint32_t IntersectedFlags = Root.getFlags() & Prev.getFlags();
2665 DebugLoc MergedLoc =
2667
2668 bool Mul1IsKill = Mul1.isKill();
2669 bool Mul2IsKill = Mul2.isKill();
2670 bool AddendIsKill = Addend.isKill();
2671
2672 // We need to clear kill flags since we may be extending the live range past
2673 // a kill. If the mul had kill flags, we can preserve those since we know
2674 // where the previous range stopped.
2675 MRI.clearKillFlags(Mul1.getReg());
2676 MRI.clearKillFlags(Mul2.getReg());
2677
2679 BuildMI(*MF, MergedLoc, TII->get(FusedOpc), DstReg)
2680 .addReg(Mul1.getReg(), getKillRegState(Mul1IsKill))
2681 .addReg(Mul2.getReg(), getKillRegState(Mul2IsKill))
2682 .addReg(Addend.getReg(), getKillRegState(AddendIsKill))
2683 .setMIFlags(IntersectedFlags);
2684
2685 InsInstrs.push_back(MIB);
2686 if (MRI.hasOneNonDBGUse(Prev.getOperand(0).getReg()))
2687 DelInstrs.push_back(&Prev);
2688 DelInstrs.push_back(&Root);
2689}
2690
2691// Combine patterns like (sh3add Z, (add X, (slli Y, 5))) to
2692// (sh3add (sh2add Y, Z), X) if the shift amount can be split across two
2693// shXadd instructions. The outer shXadd keeps its original opcode.
2694static void
2695genShXAddAddShift(MachineInstr &Root, unsigned AddOpIdx,
2698 DenseMap<Register, unsigned> &InstrIdxForVirtReg) {
2699 MachineFunction *MF = Root.getMF();
2702
2703 unsigned OuterShiftAmt = getSHXADDShiftAmount(Root.getOpcode());
2704 assert(OuterShiftAmt != 0 && "Unexpected opcode");
2705
2706 MachineInstr *AddMI = MRI.getUniqueVRegDef(Root.getOperand(2).getReg());
2707 MachineInstr *ShiftMI =
2708 MRI.getUniqueVRegDef(AddMI->getOperand(AddOpIdx).getReg());
2709
2710 unsigned InnerShiftAmt = ShiftMI->getOperand(2).getImm();
2711 assert(InnerShiftAmt >= OuterShiftAmt && "Unexpected shift amount");
2712
2713 unsigned InnerOpc;
2714 switch (InnerShiftAmt - OuterShiftAmt) {
2715 default:
2716 llvm_unreachable("Unexpected shift amount");
2717 case 0:
2718 InnerOpc = RISCV::ADD;
2719 break;
2720 case 1:
2721 InnerOpc = RISCV::SH1ADD;
2722 break;
2723 case 2:
2724 InnerOpc = RISCV::SH2ADD;
2725 break;
2726 case 3:
2727 InnerOpc = RISCV::SH3ADD;
2728 break;
2729 }
2730
2731 const MachineOperand &X = AddMI->getOperand(3 - AddOpIdx);
2732 const MachineOperand &Y = ShiftMI->getOperand(1);
2733 const MachineOperand &Z = Root.getOperand(1);
2734
2735 Register NewVR = MRI.createVirtualRegister(&RISCV::GPRRegClass);
2736
2737 auto MIB1 = BuildMI(*MF, MIMetadata(Root), TII->get(InnerOpc), NewVR)
2738 .addReg(Y.getReg(), getKillRegState(Y.isKill()))
2739 .addReg(Z.getReg(), getKillRegState(Z.isKill()));
2740 auto MIB2 = BuildMI(*MF, MIMetadata(Root), TII->get(Root.getOpcode()),
2741 Root.getOperand(0).getReg())
2742 .addReg(NewVR, RegState::Kill)
2743 .addReg(X.getReg(), getKillRegState(X.isKill()));
2744
2745 InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
2746 InsInstrs.push_back(MIB1);
2747 InsInstrs.push_back(MIB2);
2748 DelInstrs.push_back(ShiftMI);
2749 DelInstrs.push_back(AddMI);
2750 DelInstrs.push_back(&Root);
2751}
2752
2754 MachineInstr &Root, unsigned Pattern,
2757 DenseMap<Register, unsigned> &InstrIdxForVirtReg) const {
2759 switch (Pattern) {
2760 default:
2762 DelInstrs, InstrIdxForVirtReg);
2763 return;
2766 MachineInstr &Prev = *MRI.getVRegDef(Root.getOperand(1).getReg());
2767 combineFPFusedMultiply(Root, Prev, Pattern, InsInstrs, DelInstrs);
2768 return;
2769 }
2772 MachineInstr &Prev = *MRI.getVRegDef(Root.getOperand(2).getReg());
2773 combineFPFusedMultiply(Root, Prev, Pattern, InsInstrs, DelInstrs);
2774 return;
2775 }
2777 genShXAddAddShift(Root, 1, InsInstrs, DelInstrs, InstrIdxForVirtReg);
2778 return;
2780 genShXAddAddShift(Root, 2, InsInstrs, DelInstrs, InstrIdxForVirtReg);
2781 return;
2782 }
2783}
2784
2786 StringRef &ErrInfo) const {
2787 MCInstrDesc const &Desc = MI.getDesc();
2788
2789 for (const auto &[Index, Operand] : enumerate(Desc.operands())) {
2790 unsigned OpType = Operand.OperandType;
2791 if (OpType >= RISCVOp::OPERAND_FIRST_RISCV_IMM &&
2793 const MachineOperand &MO = MI.getOperand(Index);
2794 if (MO.isReg()) {
2795 ErrInfo = "Expected a non-register operand.";
2796 return false;
2797 }
2798 if (MO.isImm()) {
2799 int64_t Imm = MO.getImm();
2800 bool Ok;
2801 switch (OpType) {
2802 default:
2803 llvm_unreachable("Unexpected operand type");
2804
2805 // clang-format off
2806#define CASE_OPERAND_UIMM(NUM) \
2807 case RISCVOp::OPERAND_UIMM##NUM: \
2808 Ok = isUInt<NUM>(Imm); \
2809 break;
2810#define CASE_OPERAND_SIMM(NUM) \
2811 case RISCVOp::OPERAND_SIMM##NUM: \
2812 Ok = isInt<NUM>(Imm); \
2813 break;
2830 // clang-format on
2832 Ok = isShiftedUInt<1, 1>(Imm);
2833 break;
2835 Ok = isShiftedUInt<4, 1>(Imm);
2836 break;
2838 Ok = isUInt<5>(Imm) && (Imm != 0);
2839 break;
2841 Ok = isUInt<5>(Imm) && (Imm > 3);
2842 break;
2844 Ok = (isUInt<5>(Imm) && (Imm != 0)) || (Imm == 32);
2845 break;
2847 Ok = isShiftedUInt<5, 1>(Imm);
2848 break;
2850 Ok = isShiftedUInt<5, 2>(Imm);
2851 break;
2853 Ok = isShiftedUInt<4, 3>(Imm);
2854 break;
2856 Ok = isShiftedUInt<6, 2>(Imm);
2857 break;
2859 Ok = isShiftedUInt<5, 3>(Imm);
2860 break;
2862 Ok = isUInt<8>(Imm) && Imm >= 32;
2863 break;
2865 Ok = isShiftedUInt<6, 3>(Imm);
2866 break;
2868 Ok = isShiftedInt<6, 4>(Imm) && (Imm != 0);
2869 break;
2871 Ok = isShiftedUInt<8, 2>(Imm) && (Imm != 0);
2872 break;
2874 Ok = isUInt<16>(Imm) && (Imm != 0);
2875 break;
2877 Ok = Imm == 3;
2878 break;
2880 Ok = Imm == 4;
2881 break;
2883 Ok = (isUInt<5>(Imm) && Imm != 0) || Imm == -1;
2884 break;
2885 // clang-format off
2891 // clang-format on
2893 Ok = (isInt<5>(Imm) && Imm != -16) || Imm == 16;
2894 break;
2896 Ok = isInt<5>(Imm) && (Imm != 0);
2897 break;
2899 Ok = Imm != 0 && isInt<6>(Imm);
2900 break;
2902 Ok = isUInt<10>(Imm);
2903 break;
2905 Ok = isUInt<11>(Imm);
2906 break;
2908 Ok = isShiftedInt<7, 5>(Imm);
2909 break;
2911 Ok = isInt<16>(Imm) && (Imm != 0);
2912 break;
2914 Ok = isInt<20>(Imm);
2915 break;
2917 Ok = isInt<32>(Imm);
2918 break;
2920 Ok = STI.is64Bit() ? isUInt<6>(Imm) : isUInt<5>(Imm);
2921 break;
2923 Ok = STI.is64Bit() ? isUInt<6>(Imm) : isUInt<5>(Imm);
2924 Ok = Ok && Imm != 0;
2925 break;
2927 Ok = (isUInt<5>(Imm) && Imm != 0) ||
2928 (Imm >= 0xfffe0 && Imm <= 0xfffff);
2929 break;
2931 Ok = Imm >= 0 && Imm <= 10;
2932 break;
2934 Ok = Imm >= 0 && Imm <= 7;
2935 break;
2937 Ok = Imm >= 1 && Imm <= 10;
2938 break;
2940 Ok = Imm >= 2 && Imm <= 14;
2941 break;
2943 Ok = Imm >= RISCVZC::RA && Imm <= RISCVZC::RA_S0_S11;
2944 break;
2946 Ok = Imm >= RISCVZC::RA_S0 && Imm <= RISCVZC::RA_S0_S11;
2947 break;
2949 Ok = Imm >= 0 && Imm <= 48 && Imm % 16 == 0;
2950 break;
2953 break;
2955 Ok = Imm == RISCVFPRndMode::RTZ;
2956 break;
2958 Ok = Imm >= 0 && Imm < RISCVCC::COND_INVALID;
2959 break;
2961 Ok = (Imm &
2963 break;
2965 Ok = (isUInt<5>(Imm) && RISCVVType::isValidSEW(1 << Imm));
2966 break;
2968 Ok = Imm == 0;
2969 break;
2972 if (RISCVII::usesVXRM(Desc.TSFlags))
2973 Ok = isUInt<2>(Imm);
2974 else
2976 break;
2977 }
2978 if (!Ok) {
2979 ErrInfo = "Invalid immediate";
2980 return false;
2981 }
2982 }
2983 }
2984 }
2985
2986 const uint64_t TSFlags = Desc.TSFlags;
2987 if (RISCVII::hasVLOp(TSFlags)) {
2988 const MachineOperand &Op = MI.getOperand(RISCVII::getVLOpNum(Desc));
2989 if (!Op.isImm() && !Op.isReg()) {
2990 ErrInfo = "Invalid operand type for VL operand";
2991 return false;
2992 }
2993 if (Op.isReg() && Op.getReg() != RISCV::NoRegister) {
2994 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2995 auto *RC = MRI.getRegClass(Op.getReg());
2996 if (!RISCV::GPRRegClass.hasSubClassEq(RC)) {
2997 ErrInfo = "Invalid register class for VL operand";
2998 return false;
2999 }
3000 }
3001 if (!RISCVII::hasSEWOp(TSFlags)) {
3002 ErrInfo = "VL operand w/o SEW operand?";
3003 return false;
3004 }
3005 }
3006 if (RISCVII::hasSEWOp(TSFlags)) {
3007 unsigned OpIdx = RISCVII::getSEWOpNum(Desc);
3008 if (!MI.getOperand(OpIdx).isImm()) {
3009 ErrInfo = "SEW value expected to be an immediate";
3010 return false;
3011 }
3012 uint64_t Log2SEW = MI.getOperand(OpIdx).getImm();
3013 if (Log2SEW > 31) {
3014 ErrInfo = "Unexpected SEW value";
3015 return false;
3016 }
3017 unsigned SEW = Log2SEW ? 1 << Log2SEW : 8;
3018 if (!RISCVVType::isValidSEW(SEW)) {
3019 ErrInfo = "Unexpected SEW value";
3020 return false;
3021 }
3022 }
3023 if (RISCVII::hasVecPolicyOp(TSFlags)) {
3025 if (!MI.getOperand(OpIdx).isImm()) {
3026 ErrInfo = "Policy operand expected to be an immediate";
3027 return false;
3028 }
3029 uint64_t Policy = MI.getOperand(OpIdx).getImm();
3031 ErrInfo = "Invalid Policy Value";
3032 return false;
3033 }
3034 if (!RISCVII::hasVLOp(TSFlags)) {
3035 ErrInfo = "policy operand w/o VL operand?";
3036 return false;
3037 }
3038
3039 // VecPolicy operands can only exist on instructions with passthru/merge
3040 // arguments. Note that not all arguments with passthru have vec policy
3041 // operands- some instructions have implicit policies.
3042 unsigned UseOpIdx;
3043 if (!MI.isRegTiedToUseOperand(0, &UseOpIdx)) {
3044 ErrInfo = "policy operand w/o tied operand?";
3045 return false;
3046 }
3047 }
3048
3049 if (int Idx = RISCVII::getFRMOpNum(Desc);
3050 Idx >= 0 && MI.getOperand(Idx).getImm() == RISCVFPRndMode::DYN &&
3051 !MI.readsRegister(RISCV::FRM, /*TRI=*/nullptr)) {
3052 ErrInfo = "dynamic rounding mode should read FRM";
3053 return false;
3054 }
3055
3056 return true;
3057}
3058
3060 const MachineInstr &AddrI,
3061 ExtAddrMode &AM) const {
3062 switch (MemI.getOpcode()) {
3063 default:
3064 return false;
3065 case RISCV::LB:
3066 case RISCV::LBU:
3067 case RISCV::LH:
3068 case RISCV::LH_INX:
3069 case RISCV::LHU:
3070 case RISCV::LW:
3071 case RISCV::LW_INX:
3072 case RISCV::LWU:
3073 case RISCV::LD:
3074 case RISCV::LD_RV32:
3075 case RISCV::FLH:
3076 case RISCV::FLW:
3077 case RISCV::FLD:
3078 case RISCV::SB:
3079 case RISCV::SH:
3080 case RISCV::SH_INX:
3081 case RISCV::SW:
3082 case RISCV::SW_INX:
3083 case RISCV::SD:
3084 case RISCV::SD_RV32:
3085 case RISCV::FSH:
3086 case RISCV::FSW:
3087 case RISCV::FSD:
3088 break;
3089 }
3090
3091 if (MemI.getOperand(0).getReg() == Reg)
3092 return false;
3093
3094 if (AddrI.getOpcode() != RISCV::ADDI || !AddrI.getOperand(1).isReg() ||
3095 !AddrI.getOperand(2).isImm())
3096 return false;
3097
3098 int64_t OldOffset = MemI.getOperand(2).getImm();
3099 int64_t Disp = AddrI.getOperand(2).getImm();
3100 int64_t NewOffset = OldOffset + Disp;
3101 if (!STI.is64Bit())
3102 NewOffset = SignExtend64<32>(NewOffset);
3103
3104 if (!isInt<12>(NewOffset))
3105 return false;
3106
3107 AM.BaseReg = AddrI.getOperand(1).getReg();
3108 AM.ScaledReg = 0;
3109 AM.Scale = 0;
3110 AM.Displacement = NewOffset;
3112 return true;
3113}
3114
3116 const ExtAddrMode &AM) const {
3117
3118 const DebugLoc &DL = MemI.getDebugLoc();
3119 MachineBasicBlock &MBB = *MemI.getParent();
3120
3121 assert(AM.ScaledReg == 0 && AM.Scale == 0 &&
3122 "Addressing mode not supported for folding");
3123
3124 return BuildMI(MBB, MemI, DL, get(MemI.getOpcode()))
3125 .addReg(MemI.getOperand(0).getReg(),
3126 MemI.mayLoad() ? RegState::Define : 0)
3127 .addReg(AM.BaseReg)
3128 .addImm(AM.Displacement)
3129 .setMemRefs(MemI.memoperands())
3130 .setMIFlags(MemI.getFlags());
3131}
3132
3133// TODO: At the moment, MIPS introduced paring of instructions operating with
3134// word or double word. This should be extended with more instructions when more
3135// vendors support load/store pairing.
3137 switch (Opc) {
3138 default:
3139 return false;
3140 case RISCV::SW:
3141 case RISCV::SD:
3142 case RISCV::LD:
3143 case RISCV::LW:
3144 return true;
3145 }
3146}
3147
3149 const TargetRegisterInfo *TRI) {
3150 // If this is a volatile load/store, don't mess with it.
3151 if (LdSt.hasOrderedMemoryRef() || LdSt.getNumExplicitOperands() != 3)
3152 return false;
3153
3154 if (LdSt.getOperand(1).isFI())
3155 return true;
3156
3157 assert(LdSt.getOperand(1).isReg() && "Expected a reg operand.");
3158 // Can't cluster if the instruction modifies the base register
3159 // or it is update form. e.g. ld x5,8(x5)
3160 if (LdSt.modifiesRegister(LdSt.getOperand(1).getReg(), TRI))
3161 return false;
3162
3163 if (!LdSt.getOperand(2).isImm())
3164 return false;
3165
3166 return true;
3167}
3168
3171 int64_t &Offset, bool &OffsetIsScalable, LocationSize &Width,
3172 const TargetRegisterInfo *TRI) const {
3173 if (!LdSt.mayLoadOrStore())
3174 return false;
3175
3176 // Conservatively, only handle scalar loads/stores for now.
3177 switch (LdSt.getOpcode()) {
3178 case RISCV::LB:
3179 case RISCV::LBU:
3180 case RISCV::SB:
3181 case RISCV::LH:
3182 case RISCV::LH_INX:
3183 case RISCV::LHU:
3184 case RISCV::FLH:
3185 case RISCV::SH:
3186 case RISCV::SH_INX:
3187 case RISCV::FSH:
3188 case RISCV::LW:
3189 case RISCV::LW_INX:
3190 case RISCV::LWU:
3191 case RISCV::FLW:
3192 case RISCV::SW:
3193 case RISCV::SW_INX:
3194 case RISCV::FSW:
3195 case RISCV::LD:
3196 case RISCV::LD_RV32:
3197 case RISCV::FLD:
3198 case RISCV::SD:
3199 case RISCV::SD_RV32:
3200 case RISCV::FSD:
3201 break;
3202 default:
3203 return false;
3204 }
3205 const MachineOperand *BaseOp;
3206 OffsetIsScalable = false;
3207 if (!getMemOperandWithOffsetWidth(LdSt, BaseOp, Offset, Width, TRI))
3208 return false;
3209 BaseOps.push_back(BaseOp);
3210 return true;
3211}
3212
3213// TODO: This was copied from SIInstrInfo. Could it be lifted to a common
3214// helper?
3217 const MachineInstr &MI2,
3219 // Only examine the first "base" operand of each instruction, on the
3220 // assumption that it represents the real base address of the memory access.
3221 // Other operands are typically offsets or indices from this base address.
3222 if (BaseOps1.front()->isIdenticalTo(*BaseOps2.front()))
3223 return true;
3224
3225 if (!MI1.hasOneMemOperand() || !MI2.hasOneMemOperand())
3226 return false;
3227
3228 auto MO1 = *MI1.memoperands_begin();
3229 auto MO2 = *MI2.memoperands_begin();
3230 if (MO1->getAddrSpace() != MO2->getAddrSpace())
3231 return false;
3232
3233 auto Base1 = MO1->getValue();
3234 auto Base2 = MO2->getValue();
3235 if (!Base1 || !Base2)
3236 return false;
3237 Base1 = getUnderlyingObject(Base1);
3238 Base2 = getUnderlyingObject(Base2);
3239
3240 if (isa<UndefValue>(Base1) || isa<UndefValue>(Base2))
3241 return false;
3242
3243 return Base1 == Base2;
3244}
3245
3247 ArrayRef<const MachineOperand *> BaseOps1, int64_t Offset1,
3248 bool OffsetIsScalable1, ArrayRef<const MachineOperand *> BaseOps2,
3249 int64_t Offset2, bool OffsetIsScalable2, unsigned ClusterSize,
3250 unsigned NumBytes) const {
3251 // If the mem ops (to be clustered) do not have the same base ptr, then they
3252 // should not be clustered
3253 if (!BaseOps1.empty() && !BaseOps2.empty()) {
3254 const MachineInstr &FirstLdSt = *BaseOps1.front()->getParent();
3255 const MachineInstr &SecondLdSt = *BaseOps2.front()->getParent();
3256 if (!memOpsHaveSameBasePtr(FirstLdSt, BaseOps1, SecondLdSt, BaseOps2))
3257 return false;
3258 } else if (!BaseOps1.empty() || !BaseOps2.empty()) {
3259 // If only one base op is empty, they do not have the same base ptr
3260 return false;
3261 }
3262
3263 unsigned CacheLineSize =
3264 BaseOps1.front()->getParent()->getMF()->getSubtarget().getCacheLineSize();
3265 // Assume a cache line size of 64 bytes if no size is set in RISCVSubtarget.
3267 // Cluster if the memory operations are on the same or a neighbouring cache
3268 // line, but limit the maximum ClusterSize to avoid creating too much
3269 // additional register pressure.
3270 return ClusterSize <= 4 && std::abs(Offset1 - Offset2) < CacheLineSize;
3271}
3272
3273// Set BaseReg (the base register operand), Offset (the byte offset being
3274// accessed) and the access Width of the passed instruction that reads/writes
3275// memory. Returns false if the instruction does not read/write memory or the
3276// BaseReg/Offset/Width can't be determined. Is not guaranteed to always
3277// recognise base operands and offsets in all cases.
3278// TODO: Add an IsScalable bool ref argument (like the equivalent AArch64
3279// function) and set it as appropriate.
3281 const MachineInstr &LdSt, const MachineOperand *&BaseReg, int64_t &Offset,
3282 LocationSize &Width, const TargetRegisterInfo *TRI) const {
3283 if (!LdSt.mayLoadOrStore())
3284 return false;
3285
3286 // Here we assume the standard RISC-V ISA, which uses a base+offset
3287 // addressing mode. You'll need to relax these conditions to support custom
3288 // load/store instructions.
3289 if (LdSt.getNumExplicitOperands() != 3)
3290 return false;
3291 if ((!LdSt.getOperand(1).isReg() && !LdSt.getOperand(1).isFI()) ||
3292 !LdSt.getOperand(2).isImm())
3293 return false;
3294
3295 if (!LdSt.hasOneMemOperand())
3296 return false;
3297
3298 Width = (*LdSt.memoperands_begin())->getSize();
3299 BaseReg = &LdSt.getOperand(1);
3300 Offset = LdSt.getOperand(2).getImm();
3301 return true;
3302}
3303
3305 const MachineInstr &MIa, const MachineInstr &MIb) const {
3306 assert(MIa.mayLoadOrStore() && "MIa must be a load or store.");
3307 assert(MIb.mayLoadOrStore() && "MIb must be a load or store.");
3308
3311 return false;
3312
3313 // Retrieve the base register, offset from the base register and width. Width
3314 // is the size of memory that is being loaded/stored (e.g. 1, 2, 4). If
3315 // base registers are identical, and the offset of a lower memory access +
3316 // the width doesn't overlap the offset of a higher memory access,
3317 // then the memory accesses are different.
3318 const TargetRegisterInfo *TRI = STI.getRegisterInfo();
3319 const MachineOperand *BaseOpA = nullptr, *BaseOpB = nullptr;
3320 int64_t OffsetA = 0, OffsetB = 0;
3322 WidthB = LocationSize::precise(0);
3323 if (getMemOperandWithOffsetWidth(MIa, BaseOpA, OffsetA, WidthA, TRI) &&
3324 getMemOperandWithOffsetWidth(MIb, BaseOpB, OffsetB, WidthB, TRI)) {
3325 if (BaseOpA->isIdenticalTo(*BaseOpB)) {
3326 int LowOffset = std::min(OffsetA, OffsetB);
3327 int HighOffset = std::max(OffsetA, OffsetB);
3328 LocationSize LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
3329 if (LowWidth.hasValue() &&
3330 LowOffset + (int)LowWidth.getValue() <= HighOffset)
3331 return true;
3332 }
3333 }
3334 return false;
3335}
3336
3337std::pair<unsigned, unsigned>
3339 const unsigned Mask = RISCVII::MO_DIRECT_FLAG_MASK;
3340 return std::make_pair(TF & Mask, TF & ~Mask);
3341}
3342
3345 using namespace RISCVII;
3346 static const std::pair<unsigned, const char *> TargetFlags[] = {
3347 {MO_CALL, "riscv-call"},
3348 {MO_LO, "riscv-lo"},
3349 {MO_HI, "riscv-hi"},
3350 {MO_PCREL_LO, "riscv-pcrel-lo"},
3351 {MO_PCREL_HI, "riscv-pcrel-hi"},
3352 {MO_GOT_HI, "riscv-got-hi"},
3353 {MO_TPREL_LO, "riscv-tprel-lo"},
3354 {MO_TPREL_HI, "riscv-tprel-hi"},
3355 {MO_TPREL_ADD, "riscv-tprel-add"},
3356 {MO_TLS_GOT_HI, "riscv-tls-got-hi"},
3357 {MO_TLS_GD_HI, "riscv-tls-gd-hi"},
3358 {MO_TLSDESC_HI, "riscv-tlsdesc-hi"},
3359 {MO_TLSDESC_LOAD_LO, "riscv-tlsdesc-load-lo"},
3360 {MO_TLSDESC_ADD_LO, "riscv-tlsdesc-add-lo"},
3361 {MO_TLSDESC_CALL, "riscv-tlsdesc-call"}};
3362 return ArrayRef(TargetFlags);
3363}
3365 MachineFunction &MF, bool OutlineFromLinkOnceODRs) const {
3366 const Function &F = MF.getFunction();
3367
3368 // Can F be deduplicated by the linker? If it can, don't outline from it.
3369 if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage())
3370 return false;
3371
3372 // Don't outline from functions with section markings; the program could
3373 // expect that all the code is in the named section.
3374 if (F.hasSection())
3375 return false;
3376
3377 // It's safe to outline from MF.
3378 return true;
3379}
3380
3382 unsigned &Flags) const {
3383 // More accurate safety checking is done in getOutliningCandidateInfo.
3385}
3386
3387// Enum values indicating how an outlined call should be constructed.
3392
3397
3399 const MachineFunction *MF = MBB.getParent();
3400 const Function &F = MF->getFunction();
3401 return F.getFnAttribute("fentry-call").getValueAsBool() ||
3402 F.hasFnAttribute("patchable-function-entry");
3403}
3404
3406 MCRegister RegNo) {
3407 return MI.readsRegister(RegNo, TRI) ||
3408 MI.getDesc().hasImplicitUseOfPhysReg(RegNo);
3409}
3410
3412 const TargetRegisterInfo *TRI, MCRegister RegNo) {
3413 return MI.modifiesRegister(RegNo, TRI) ||
3414 MI.getDesc().hasImplicitDefOfPhysReg(RegNo);
3415}
3416
3418 if (!MBB.back().isReturn())
3419 return true;
3421 return true;
3422
3423 // If the candidate reads the pre-set register
3424 // that can be used for expanding PseudoTAIL instruction,
3425 // then we cannot insert tail call.
3426 const TargetSubtargetInfo &STI = MBB.getParent()->getSubtarget();
3427 MCRegister TailExpandUseRegNo =
3429 for (const MachineInstr &MI : MBB) {
3430 if (isMIReadsReg(MI, STI.getRegisterInfo(), TailExpandUseRegNo))
3431 return true;
3432 if (isMIModifiesReg(MI, STI.getRegisterInfo(), TailExpandUseRegNo))
3433 break;
3434 }
3435 return false;
3436}
3437
3439 // If last instruction is return then we can rely on
3440 // the verification already performed in the getOutliningTypeImpl.
3441 if (C.back().isReturn()) {
3442 assert(!cannotInsertTailCall(*C.getMBB()) &&
3443 "The candidate who uses return instruction must be outlined "
3444 "using tail call");
3445 return false;
3446 }
3447
3448 // Filter out candidates where the X5 register (t0) can't be used to setup
3449 // the function call.
3450 const TargetRegisterInfo *TRI = C.getMF()->getSubtarget().getRegisterInfo();
3451 if (llvm::any_of(C, [TRI](const MachineInstr &MI) {
3452 return isMIModifiesReg(MI, TRI, RISCV::X5);
3453 }))
3454 return true;
3455
3456 return !C.isAvailableAcrossAndOutOfSeq(RISCV::X5, *TRI);
3457}
3458
3459std::optional<std::unique_ptr<outliner::OutlinedFunction>>
3461 const MachineModuleInfo &MMI,
3462 std::vector<outliner::Candidate> &RepeatedSequenceLocs,
3463 unsigned MinRepeats) const {
3464
3465 // Analyze each candidate and erase the ones that are not viable.
3466 llvm::erase_if(RepeatedSequenceLocs, analyzeCandidate);
3467
3468 // If the sequence doesn't have enough candidates left, then we're done.
3469 if (RepeatedSequenceLocs.size() < MinRepeats)
3470 return std::nullopt;
3471
3472 // Each RepeatedSequenceLoc is identical.
3473 outliner::Candidate &Candidate = RepeatedSequenceLocs[0];
3474 unsigned InstrSizeCExt =
3475 Candidate.getMF()->getSubtarget<RISCVSubtarget>().hasStdExtZca() ? 2 : 4;
3476 unsigned CallOverhead = 0, FrameOverhead = 0;
3477
3479 if (Candidate.back().isReturn()) {
3481 // tail call = auipc + jalr in the worst case without linker relaxation.
3482 // FIXME: This code suggests the JALR can be compressed - how?
3483 CallOverhead = 4 + InstrSizeCExt;
3484 // Using tail call we move ret instruction from caller to callee.
3485 FrameOverhead = 0;
3486 } else {
3487 // call t0, function = 8 bytes.
3488 CallOverhead = 8;
3489 // jr t0 = 4 bytes, 2 bytes if compressed instructions are enabled.
3490 FrameOverhead = InstrSizeCExt;
3491 }
3492
3493 for (auto &C : RepeatedSequenceLocs)
3494 C.setCallInfo(MOCI, CallOverhead);
3495
3496 unsigned SequenceSize = 0;
3497 for (auto &MI : Candidate)
3498 SequenceSize += getInstSizeInBytes(MI);
3499
3500 return std::make_unique<outliner::OutlinedFunction>(
3501 RepeatedSequenceLocs, SequenceSize, FrameOverhead, MOCI);
3502}
3503
3507 unsigned Flags) const {
3508 MachineInstr &MI = *MBBI;
3509 MachineBasicBlock *MBB = MI.getParent();
3510 const TargetRegisterInfo *TRI =
3511 MBB->getParent()->getSubtarget().getRegisterInfo();
3512 const auto &F = MI.getMF()->getFunction();
3513
3514 // We can manually strip out CFI instructions later.
3515 if (MI.isCFIInstruction())
3516 // If current function has exception handling code, we can't outline &
3517 // strip these CFI instructions since it may break .eh_frame section
3518 // needed in unwinding.
3519 return F.needsUnwindTableEntry() ? outliner::InstrType::Illegal
3521
3522 if (cannotInsertTailCall(*MBB) &&
3523 (MI.isReturn() || isMIModifiesReg(MI, TRI, RISCV::X5)))
3525
3526 // Make sure the operands don't reference something unsafe.
3527 for (const auto &MO : MI.operands()) {
3528
3529 // pcrel-hi and pcrel-lo can't put in separate sections, filter that out
3530 // if any possible.
3531 if (MO.getTargetFlags() == RISCVII::MO_PCREL_LO &&
3532 (MI.getMF()->getTarget().getFunctionSections() || F.hasComdat() ||
3533 F.hasSection() || F.getSectionPrefix()))
3535 }
3536
3537 if (isLPAD(MI))
3539
3541}
3542
3545 const outliner::OutlinedFunction &OF) const {
3546
3547 // Strip out any CFI instructions
3548 bool Changed = true;
3549 while (Changed) {
3550 Changed = false;
3551 auto I = MBB.begin();
3552 auto E = MBB.end();
3553 for (; I != E; ++I) {
3554 if (I->isCFIInstruction()) {
3555 I->removeFromParent();
3556 Changed = true;
3557 break;
3558 }
3559 }
3560 }
3561
3562 if (OF.FrameConstructionID == MachineOutlinerTailCall)
3563 return;
3564
3565 MBB.addLiveIn(RISCV::X5);
3566
3567 // Add in a return instruction to the end of the outlined frame.
3568 MBB.insert(MBB.end(), BuildMI(MF, DebugLoc(), get(RISCV::JALR))
3569 .addReg(RISCV::X0, RegState::Define)
3570 .addReg(RISCV::X5)
3571 .addImm(0));
3572}
3573
3577
3578 if (C.CallConstructionID == MachineOutlinerTailCall) {
3579 It = MBB.insert(It, BuildMI(MF, DebugLoc(), get(RISCV::PseudoTAIL))
3580 .addGlobalAddress(M.getNamedValue(MF.getName()),
3581 /*Offset=*/0, RISCVII::MO_CALL));
3582 return It;
3583 }
3584
3585 // Add in a call instruction to the outlined function at the given location.
3586 It = MBB.insert(It,
3587 BuildMI(MF, DebugLoc(), get(RISCV::PseudoCALLReg), RISCV::X5)
3588 .addGlobalAddress(M.getNamedValue(MF.getName()), 0,
3590 return It;
3591}
3592
3593std::optional<RegImmPair> RISCVInstrInfo::isAddImmediate(const MachineInstr &MI,
3594 Register Reg) const {
3595 // TODO: Handle cases where Reg is a super- or sub-register of the
3596 // destination register.
3597 const MachineOperand &Op0 = MI.getOperand(0);
3598 if (!Op0.isReg() || Reg != Op0.getReg())
3599 return std::nullopt;
3600
3601 // Don't consider ADDIW as a candidate because the caller may not be aware
3602 // of its sign extension behaviour.
3603 if (MI.getOpcode() == RISCV::ADDI && MI.getOperand(1).isReg() &&
3604 MI.getOperand(2).isImm())
3605 return RegImmPair{MI.getOperand(1).getReg(), MI.getOperand(2).getImm()};
3606
3607 return std::nullopt;
3608}
3609
3610// MIR printer helper function to annotate Operands with a comment.
3612 const MachineInstr &MI, const MachineOperand &Op, unsigned OpIdx,
3613 const TargetRegisterInfo *TRI) const {
3614 // Print a generic comment for this operand if there is one.
3615 std::string GenericComment =
3617 if (!GenericComment.empty())
3618 return GenericComment;
3619
3620 // If not, we must have an immediate operand.
3621 if (!Op.isImm())
3622 return std::string();
3623
3624 const MCInstrDesc &Desc = MI.getDesc();
3625 if (OpIdx >= Desc.getNumOperands())
3626 return std::string();
3627
3628 std::string Comment;
3629 raw_string_ostream OS(Comment);
3630
3631 const MCOperandInfo &OpInfo = Desc.operands()[OpIdx];
3632
3633 // Print the full VType operand of vsetvli/vsetivli instructions, and the SEW
3634 // operand of vector codegen pseudos.
3635 switch (OpInfo.OperandType) {
3638 unsigned Imm = Op.getImm();
3639 RISCVVType::printVType(Imm, OS);
3640 break;
3641 }
3644 unsigned Log2SEW = Op.getImm();
3645 unsigned SEW = Log2SEW ? 1 << Log2SEW : 8;
3646 assert(RISCVVType::isValidSEW(SEW) && "Unexpected SEW");
3647 OS << "e" << SEW;
3648 break;
3649 }
3651 unsigned Policy = Op.getImm();
3653 "Invalid Policy Value");
3654 OS << (Policy & RISCVVType::TAIL_AGNOSTIC ? "ta" : "tu") << ", "
3655 << (Policy & RISCVVType::MASK_AGNOSTIC ? "ma" : "mu");
3656 break;
3657 }
3658
3659 return Comment;
3660}
3661
3662// clang-format off
3663#define CASE_RVV_OPCODE_UNMASK_LMUL(OP, LMUL) \
3664 RISCV::Pseudo##OP##_##LMUL
3665
3666#define CASE_RVV_OPCODE_MASK_LMUL(OP, LMUL) \
3667 RISCV::Pseudo##OP##_##LMUL##_MASK
3668
3669#define CASE_RVV_OPCODE_LMUL(OP, LMUL) \
3670 CASE_RVV_OPCODE_UNMASK_LMUL(OP, LMUL): \
3671 case CASE_RVV_OPCODE_MASK_LMUL(OP, LMUL)
3672
3673#define CASE_RVV_OPCODE_UNMASK_WIDEN(OP) \
3674 CASE_RVV_OPCODE_UNMASK_LMUL(OP, MF8): \
3675 case CASE_RVV_OPCODE_UNMASK_LMUL(OP, MF4): \
3676 case CASE_RVV_OPCODE_UNMASK_LMUL(OP, MF2): \
3677 case CASE_RVV_OPCODE_UNMASK_LMUL(OP, M1): \
3678 case CASE_RVV_OPCODE_UNMASK_LMUL(OP, M2): \
3679 case CASE_RVV_OPCODE_UNMASK_LMUL(OP, M4)
3680
3681#define CASE_RVV_OPCODE_UNMASK(OP) \
3682 CASE_RVV_OPCODE_UNMASK_WIDEN(OP): \
3683 case CASE_RVV_OPCODE_UNMASK_LMUL(OP, M8)
3684
3685#define CASE_RVV_OPCODE_MASK_WIDEN(OP) \
3686 CASE_RVV_OPCODE_MASK_LMUL(OP, MF8): \
3687 case CASE_RVV_OPCODE_MASK_LMUL(OP, MF4): \
3688 case CASE_RVV_OPCODE_MASK_LMUL(OP, MF2): \
3689 case CASE_RVV_OPCODE_MASK_LMUL(OP, M1): \
3690 case CASE_RVV_OPCODE_MASK_LMUL(OP, M2): \
3691 case CASE_RVV_OPCODE_MASK_LMUL(OP, M4)
3692
3693#define CASE_RVV_OPCODE_MASK(OP) \
3694 CASE_RVV_OPCODE_MASK_WIDEN(OP): \
3695 case CASE_RVV_OPCODE_MASK_LMUL(OP, M8)
3696
3697#define CASE_RVV_OPCODE_WIDEN(OP) \
3698 CASE_RVV_OPCODE_UNMASK_WIDEN(OP): \
3699 case CASE_RVV_OPCODE_MASK_WIDEN(OP)
3700
3701#define CASE_RVV_OPCODE(OP) \
3702 CASE_RVV_OPCODE_UNMASK(OP): \
3703 case CASE_RVV_OPCODE_MASK(OP)
3704// clang-format on
3705
3706// clang-format off
3707#define CASE_VMA_OPCODE_COMMON(OP, TYPE, LMUL) \
3708 RISCV::PseudoV##OP##_##TYPE##_##LMUL
3709
3710#define CASE_VMA_OPCODE_LMULS(OP, TYPE) \
3711 CASE_VMA_OPCODE_COMMON(OP, TYPE, MF8): \
3712 case CASE_VMA_OPCODE_COMMON(OP, TYPE, MF4): \
3713 case CASE_VMA_OPCODE_COMMON(OP, TYPE, MF2): \
3714 case CASE_VMA_OPCODE_COMMON(OP, TYPE, M1): \
3715 case CASE_VMA_OPCODE_COMMON(OP, TYPE, M2): \
3716 case CASE_VMA_OPCODE_COMMON(OP, TYPE, M4): \
3717 case CASE_VMA_OPCODE_COMMON(OP, TYPE, M8)
3718
3719// VFMA instructions are SEW specific.
3720#define CASE_VFMA_OPCODE_COMMON(OP, TYPE, LMUL, SEW) \
3721 RISCV::PseudoV##OP##_##TYPE##_##LMUL##_##SEW
3722
3723#define CASE_VFMA_OPCODE_LMULS_M1(OP, TYPE, SEW) \
3724 CASE_VFMA_OPCODE_COMMON(OP, TYPE, M1, SEW): \
3725 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M2, SEW): \
3726 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M4, SEW): \
3727 case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M8, SEW)
3728
3729#define CASE_VFMA_OPCODE_LMULS_MF2(OP, TYPE, SEW) \
3730 CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF2, SEW): \
3731 case CASE_VFMA_OPCODE_LMULS_M1(OP, TYPE, SEW)
3732
3733#define CASE_VFMA_OPCODE_LMULS_MF4(OP, TYPE, SEW) \
3734 CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF4, SEW): \
3735 case CASE_VFMA_OPCODE_LMULS_MF2(OP, TYPE, SEW)
3736
3737#define CASE_VFMA_OPCODE_VV(OP) \
3738 CASE_VFMA_OPCODE_LMULS_MF4(OP, VV, E16): \
3739 case CASE_VFMA_OPCODE_LMULS_MF2(OP, VV, E32): \
3740 case CASE_VFMA_OPCODE_LMULS_M1(OP, VV, E64)
3741
3742#define CASE_VFMA_SPLATS(OP) \
3743 CASE_VFMA_OPCODE_LMULS_MF4(OP, VFPR16, E16): \
3744 case CASE_VFMA_OPCODE_LMULS_MF2(OP, VFPR32, E32): \
3745 case CASE_VFMA_OPCODE_LMULS_M1(OP, VFPR64, E64)
3746// clang-format on
3747
3749 unsigned &SrcOpIdx1,
3750 unsigned &SrcOpIdx2) const {
3751 const MCInstrDesc &Desc = MI.getDesc();
3752 if (!Desc.isCommutable())
3753 return false;
3754
3755 switch (MI.getOpcode()) {
3756 case RISCV::TH_MVEQZ:
3757 case RISCV::TH_MVNEZ:
3758 // We can't commute operands if operand 2 (i.e., rs1 in
3759 // mveqz/mvnez rd,rs1,rs2) is the zero-register (as it is
3760 // not valid as the in/out-operand 1).
3761 if (MI.getOperand(2).getReg() == RISCV::X0)
3762 return false;
3763 // Operands 1 and 2 are commutable, if we switch the opcode.
3764 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 1, 2);
3765 case RISCV::TH_MULA:
3766 case RISCV::TH_MULAW:
3767 case RISCV::TH_MULAH:
3768 case RISCV::TH_MULS:
3769 case RISCV::TH_MULSW:
3770 case RISCV::TH_MULSH:
3771 // Operands 2 and 3 are commutable.
3772 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3);
3773 case RISCV::PseudoCCMOVGPRNoX0:
3774 case RISCV::PseudoCCMOVGPR:
3775 // Operands 4 and 5 are commutable.
3776 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 4, 5);
3777 case CASE_RVV_OPCODE(VADD_VV):
3778 case CASE_RVV_OPCODE(VAND_VV):
3779 case CASE_RVV_OPCODE(VOR_VV):
3780 case CASE_RVV_OPCODE(VXOR_VV):
3781 case CASE_RVV_OPCODE_MASK(VMSEQ_VV):
3782 case CASE_RVV_OPCODE_MASK(VMSNE_VV):
3783 case CASE_RVV_OPCODE(VMIN_VV):
3784 case CASE_RVV_OPCODE(VMINU_VV):
3785 case CASE_RVV_OPCODE(VMAX_VV):
3786 case CASE_RVV_OPCODE(VMAXU_VV):
3787 case CASE_RVV_OPCODE(VMUL_VV):
3788 case CASE_RVV_OPCODE(VMULH_VV):
3789 case CASE_RVV_OPCODE(VMULHU_VV):
3790 case CASE_RVV_OPCODE_WIDEN(VWADD_VV):
3791 case CASE_RVV_OPCODE_WIDEN(VWADDU_VV):
3792 case CASE_RVV_OPCODE_WIDEN(VWMUL_VV):
3793 case CASE_RVV_OPCODE_WIDEN(VWMULU_VV):
3794 case CASE_RVV_OPCODE_WIDEN(VWMACC_VV):
3795 case CASE_RVV_OPCODE_WIDEN(VWMACCU_VV):
3796 case CASE_RVV_OPCODE_UNMASK(VADC_VVM):
3797 case CASE_RVV_OPCODE(VSADD_VV):
3798 case CASE_RVV_OPCODE(VSADDU_VV):
3799 case CASE_RVV_OPCODE(VAADD_VV):
3800 case CASE_RVV_OPCODE(VAADDU_VV):
3801 case CASE_RVV_OPCODE(VSMUL_VV):
3802 // Operands 2 and 3 are commutable.
3803 return fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, 2, 3);
3804 case CASE_VFMA_SPLATS(FMADD):
3805 case CASE_VFMA_SPLATS(FMSUB):
3806 case CASE_VFMA_SPLATS(FMACC):
3807 case CASE_VFMA_SPLATS(FMSAC):
3810 case CASE_VFMA_SPLATS(FNMACC):
3811 case CASE_VFMA_SPLATS(FNMSAC):
3812 case CASE_VFMA_OPCODE_VV(FMACC):
3813 case CASE_VFMA_OPCODE_VV(FMSAC):
3814 case CASE_VFMA_OPCODE_VV(FNMACC):
3815 case CASE_VFMA_OPCODE_VV(FNMSAC):
3816 case CASE_VMA_OPCODE_LMULS(MADD, VX):
3817 case CASE_VMA_OPCODE_LMULS(NMSUB, VX):
3818 case CASE_VMA_OPCODE_LMULS(MACC, VX):
3819 case CASE_VMA_OPCODE_LMULS(NMSAC, VX):
3820 case CASE_VMA_OPCODE_LMULS(MACC, VV):
3821 case CASE_VMA_OPCODE_LMULS(NMSAC, VV): {
3822 // If the tail policy is undisturbed we can't commute.
3823 assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags));
3824 if ((MI.getOperand(RISCVII::getVecPolicyOpNum(MI.getDesc())).getImm() &
3825 1) == 0)
3826 return false;
3827
3828 // For these instructions we can only swap operand 1 and operand 3 by
3829 // changing the opcode.
3830 unsigned CommutableOpIdx1 = 1;
3831 unsigned CommutableOpIdx2 = 3;
3832 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1,
3833 CommutableOpIdx2))
3834 return false;
3835 return true;
3836 }
3837 case CASE_VFMA_OPCODE_VV(FMADD):
3841 case CASE_VMA_OPCODE_LMULS(MADD, VV):
3842 case CASE_VMA_OPCODE_LMULS(NMSUB, VV): {
3843 // If the tail policy is undisturbed we can't commute.
3844 assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags));
3845 if ((MI.getOperand(RISCVII::getVecPolicyOpNum(MI.getDesc())).getImm() &
3846 1) == 0)
3847 return false;
3848
3849 // For these instructions we have more freedom. We can commute with the
3850 // other multiplicand or with the addend/subtrahend/minuend.
3851
3852 // Any fixed operand must be from source 1, 2 or 3.
3853 if (SrcOpIdx1 != CommuteAnyOperandIndex && SrcOpIdx1 > 3)
3854 return false;
3855 if (SrcOpIdx2 != CommuteAnyOperandIndex && SrcOpIdx2 > 3)
3856 return false;
3857
3858 // It both ops are fixed one must be the tied source.
3859 if (SrcOpIdx1 != CommuteAnyOperandIndex &&
3860 SrcOpIdx2 != CommuteAnyOperandIndex && SrcOpIdx1 != 1 && SrcOpIdx2 != 1)
3861 return false;
3862
3863 // Look for two different register operands assumed to be commutable
3864 // regardless of the FMA opcode. The FMA opcode is adjusted later if
3865 // needed.
3866 if (SrcOpIdx1 == CommuteAnyOperandIndex ||
3867 SrcOpIdx2 == CommuteAnyOperandIndex) {
3868 // At least one of operands to be commuted is not specified and
3869 // this method is free to choose appropriate commutable operands.
3870 unsigned CommutableOpIdx1 = SrcOpIdx1;
3871 if (SrcOpIdx1 == SrcOpIdx2) {
3872 // Both of operands are not fixed. Set one of commutable
3873 // operands to the tied source.
3874 CommutableOpIdx1 = 1;
3875 } else if (SrcOpIdx1 == CommuteAnyOperandIndex) {
3876 // Only one of the operands is not fixed.
3877 CommutableOpIdx1 = SrcOpIdx2;
3878 }
3879
3880 // CommutableOpIdx1 is well defined now. Let's choose another commutable
3881 // operand and assign its index to CommutableOpIdx2.
3882 unsigned CommutableOpIdx2;
3883 if (CommutableOpIdx1 != 1) {
3884 // If we haven't already used the tied source, we must use it now.
3885 CommutableOpIdx2 = 1;
3886 } else {
3887 Register Op1Reg = MI.getOperand(CommutableOpIdx1).getReg();
3888
3889 // The commuted operands should have different registers.
3890 // Otherwise, the commute transformation does not change anything and
3891 // is useless. We use this as a hint to make our decision.
3892 if (Op1Reg != MI.getOperand(2).getReg())
3893 CommutableOpIdx2 = 2;
3894 else
3895 CommutableOpIdx2 = 3;
3896 }
3897
3898 // Assign the found pair of commutable indices to SrcOpIdx1 and
3899 // SrcOpIdx2 to return those values.
3900 if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1,
3901 CommutableOpIdx2))
3902 return false;
3903 }
3904
3905 return true;
3906 }
3907 }
3908
3909 return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
3910}
3911
3912// clang-format off
3913#define CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, LMUL) \
3914 case RISCV::PseudoV##OLDOP##_##TYPE##_##LMUL: \
3915 Opc = RISCV::PseudoV##NEWOP##_##TYPE##_##LMUL; \
3916 break;
3917
3918#define CASE_VMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, TYPE) \
3919 CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF8) \
3920 CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF4) \
3921 CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF2) \
3922 CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M1) \
3923 CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M2) \
3924 CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M4) \
3925 CASE_VMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M8)
3926
3927// VFMA depends on SEW.
3928#define CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, LMUL, SEW) \
3929 case RISCV::PseudoV##OLDOP##_##TYPE##_##LMUL##_##SEW: \
3930 Opc = RISCV::PseudoV##NEWOP##_##TYPE##_##LMUL##_##SEW; \
3931 break;
3932
3933#define CASE_VFMA_CHANGE_OPCODE_LMULS_M1(OLDOP, NEWOP, TYPE, SEW) \
3934 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M1, SEW) \
3935 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M2, SEW) \
3936 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M4, SEW) \
3937 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M8, SEW)
3938
3939#define CASE_VFMA_CHANGE_OPCODE_LMULS_MF2(OLDOP, NEWOP, TYPE, SEW) \
3940 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF2, SEW) \
3941 CASE_VFMA_CHANGE_OPCODE_LMULS_M1(OLDOP, NEWOP, TYPE, SEW)
3942
3943#define CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(OLDOP, NEWOP, TYPE, SEW) \
3944 CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF4, SEW) \
3945 CASE_VFMA_CHANGE_OPCODE_LMULS_MF2(OLDOP, NEWOP, TYPE, SEW)
3946
3947#define CASE_VFMA_CHANGE_OPCODE_VV(OLDOP, NEWOP) \
3948 CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(OLDOP, NEWOP, VV, E16) \
3949 CASE_VFMA_CHANGE_OPCODE_LMULS_MF2(OLDOP, NEWOP, VV, E32) \
3950 CASE_VFMA_CHANGE_OPCODE_LMULS_M1(OLDOP, NEWOP, VV, E64)
3951
3952#define CASE_VFMA_CHANGE_OPCODE_SPLATS(OLDOP, NEWOP) \
3953 CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(OLDOP, NEWOP, VFPR16, E16) \
3954 CASE_VFMA_CHANGE_OPCODE_LMULS_MF2(OLDOP, NEWOP, VFPR32, E32) \
3955 CASE_VFMA_CHANGE_OPCODE_LMULS_M1(OLDOP, NEWOP, VFPR64, E64)
3956// clang-format on
3957
3959 bool NewMI,
3960 unsigned OpIdx1,
3961 unsigned OpIdx2) const {
3962 auto cloneIfNew = [NewMI](MachineInstr &MI) -> MachineInstr & {
3963 if (NewMI)
3964 return *MI.getParent()->getParent()->CloneMachineInstr(&MI);
3965 return MI;
3966 };
3967
3968 switch (MI.getOpcode()) {
3969 case RISCV::TH_MVEQZ:
3970 case RISCV::TH_MVNEZ: {
3971 auto &WorkingMI = cloneIfNew(MI);
3972 WorkingMI.setDesc(get(MI.getOpcode() == RISCV::TH_MVEQZ ? RISCV::TH_MVNEZ
3973 : RISCV::TH_MVEQZ));
3974 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, false, OpIdx1,
3975 OpIdx2);
3976 }
3977 case RISCV::PseudoCCMOVGPRNoX0:
3978 case RISCV::PseudoCCMOVGPR: {
3979 // CCMOV can be commuted by inverting the condition.
3980 auto CC = static_cast<RISCVCC::CondCode>(MI.getOperand(3).getImm());
3982 auto &WorkingMI = cloneIfNew(MI);
3983 WorkingMI.getOperand(3).setImm(CC);
3984 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI*/ false,
3985 OpIdx1, OpIdx2);
3986 }
3987 case CASE_VFMA_SPLATS(FMACC):
3988 case CASE_VFMA_SPLATS(FMADD):
3989 case CASE_VFMA_SPLATS(FMSAC):
3990 case CASE_VFMA_SPLATS(FMSUB):
3991 case CASE_VFMA_SPLATS(FNMACC):
3993 case CASE_VFMA_SPLATS(FNMSAC):
3995 case CASE_VFMA_OPCODE_VV(FMACC):
3996 case CASE_VFMA_OPCODE_VV(FMSAC):
3997 case CASE_VFMA_OPCODE_VV(FNMACC):
3998 case CASE_VFMA_OPCODE_VV(FNMSAC):
3999 case CASE_VMA_OPCODE_LMULS(MADD, VX):
4000 case CASE_VMA_OPCODE_LMULS(NMSUB, VX):
4001 case CASE_VMA_OPCODE_LMULS(MACC, VX):
4002 case CASE_VMA_OPCODE_LMULS(NMSAC, VX):
4003 case CASE_VMA_OPCODE_LMULS(MACC, VV):
4004 case CASE_VMA_OPCODE_LMULS(NMSAC, VV): {
4005 // It only make sense to toggle these between clobbering the
4006 // addend/subtrahend/minuend one of the multiplicands.
4007 assert((OpIdx1 == 1 || OpIdx2 == 1) && "Unexpected opcode index");
4008 assert((OpIdx1 == 3 || OpIdx2 == 3) && "Unexpected opcode index");
4009 unsigned Opc;
4010 switch (MI.getOpcode()) {
4011 default:
4012 llvm_unreachable("Unexpected opcode");
4013 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMACC, FMADD)
4014 CASE_VFMA_CHANGE_OPCODE_SPLATS(FMADD, FMACC)
4021 CASE_VFMA_CHANGE_OPCODE_VV(FMACC, FMADD)
4025 CASE_VMA_CHANGE_OPCODE_LMULS(MACC, MADD, VX)
4026 CASE_VMA_CHANGE_OPCODE_LMULS(MADD, MACC, VX)
4027 CASE_VMA_CHANGE_OPCODE_LMULS(NMSAC, NMSUB, VX)
4028 CASE_VMA_CHANGE_OPCODE_LMULS(NMSUB, NMSAC, VX)
4029 CASE_VMA_CHANGE_OPCODE_LMULS(MACC, MADD, VV)
4030 CASE_VMA_CHANGE_OPCODE_LMULS(NMSAC, NMSUB, VV)
4031 }
4032
4033 auto &WorkingMI = cloneIfNew(MI);
4034 WorkingMI.setDesc(get(Opc));
4035 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false,
4036 OpIdx1, OpIdx2);
4037 }
4038 case CASE_VFMA_OPCODE_VV(FMADD):
4042 case CASE_VMA_OPCODE_LMULS(MADD, VV):
4043 case CASE_VMA_OPCODE_LMULS(NMSUB, VV): {
4044 assert((OpIdx1 == 1 || OpIdx2 == 1) && "Unexpected opcode index");
4045 // If one of the operands, is the addend we need to change opcode.
4046 // Otherwise we're just swapping 2 of the multiplicands.
4047 if (OpIdx1 == 3 || OpIdx2 == 3) {
4048 unsigned Opc;
4049 switch (MI.getOpcode()) {
4050 default:
4051 llvm_unreachable("Unexpected opcode");
4052 CASE_VFMA_CHANGE_OPCODE_VV(FMADD, FMACC)
4056 CASE_VMA_CHANGE_OPCODE_LMULS(MADD, MACC, VV)
4057 CASE_VMA_CHANGE_OPCODE_LMULS(NMSUB, NMSAC, VV)
4058 }
4059
4060 auto &WorkingMI = cloneIfNew(MI);
4061 WorkingMI.setDesc(get(Opc));
4062 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false,
4063 OpIdx1, OpIdx2);
4064 }
4065 // Let the default code handle it.
4066 break;
4067 }
4068 }
4069
4070 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
4071}
4072
4073#undef CASE_VMA_CHANGE_OPCODE_COMMON
4074#undef CASE_VMA_CHANGE_OPCODE_LMULS
4075#undef CASE_VFMA_CHANGE_OPCODE_COMMON
4076#undef CASE_VFMA_CHANGE_OPCODE_LMULS_M1
4077#undef CASE_VFMA_CHANGE_OPCODE_LMULS_MF2
4078#undef CASE_VFMA_CHANGE_OPCODE_LMULS_MF4
4079#undef CASE_VFMA_CHANGE_OPCODE_VV
4080#undef CASE_VFMA_CHANGE_OPCODE_SPLATS
4081
4082#undef CASE_RVV_OPCODE_UNMASK_LMUL
4083#undef CASE_RVV_OPCODE_MASK_LMUL
4084#undef CASE_RVV_OPCODE_LMUL
4085#undef CASE_RVV_OPCODE_UNMASK_WIDEN
4086#undef CASE_RVV_OPCODE_UNMASK
4087#undef CASE_RVV_OPCODE_MASK_WIDEN
4088#undef CASE_RVV_OPCODE_MASK
4089#undef CASE_RVV_OPCODE_WIDEN
4090#undef CASE_RVV_OPCODE
4091
4092#undef CASE_VMA_OPCODE_COMMON
4093#undef CASE_VMA_OPCODE_LMULS
4094#undef CASE_VFMA_OPCODE_COMMON
4095#undef CASE_VFMA_OPCODE_LMULS_M1
4096#undef CASE_VFMA_OPCODE_LMULS_MF2
4097#undef CASE_VFMA_OPCODE_LMULS_MF4
4098#undef CASE_VFMA_OPCODE_VV
4099#undef CASE_VFMA_SPLATS
4100
4102 switch (MI.getOpcode()) {
4103 default:
4104 break;
4105 case RISCV::ADD:
4106 case RISCV::OR:
4107 case RISCV::XOR:
4108 // Normalize (so we hit the next if clause).
4109 // add/[x]or rd, zero, rs => add/[x]or rd, rs, zero
4110 if (MI.getOperand(1).getReg() == RISCV::X0)
4111 commuteInstruction(MI);
4112 // add/[x]or rd, rs, zero => addi rd, rs, 0
4113 if (MI.getOperand(2).getReg() == RISCV::X0) {
4114 MI.getOperand(2).ChangeToImmediate(0);
4115 MI.setDesc(get(RISCV::ADDI));
4116 return true;
4117 }
4118 // xor rd, rs, rs => addi rd, zero, 0
4119 if (MI.getOpcode() == RISCV::XOR &&
4120 MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
4121 MI.getOperand(1).setReg(RISCV::X0);
4122 MI.getOperand(2).ChangeToImmediate(0);
4123 MI.setDesc(get(RISCV::ADDI));
4124 return true;
4125 }
4126 break;
4127 case RISCV::ORI:
4128 case RISCV::XORI:
4129 // [x]ori rd, zero, N => addi rd, zero, N
4130 if (MI.getOperand(1).getReg() == RISCV::X0) {
4131 MI.setDesc(get(RISCV::ADDI));
4132 return true;
4133 }
4134 break;
4135 case RISCV::SUB:
4136 // sub rd, rs, zero => addi rd, rs, 0
4137 if (MI.getOperand(2).getReg() == RISCV::X0) {
4138 MI.getOperand(2).ChangeToImmediate(0);
4139 MI.setDesc(get(RISCV::ADDI));
4140 return true;
4141 }
4142 break;
4143 case RISCV::SUBW:
4144 // subw rd, rs, zero => addiw rd, rs, 0
4145 if (MI.getOperand(2).getReg() == RISCV::X0) {
4146 MI.getOperand(2).ChangeToImmediate(0);
4147 MI.setDesc(get(RISCV::ADDIW));
4148 return true;
4149 }
4150 break;
4151 case RISCV::ADDW:
4152 // Normalize (so we hit the next if clause).
4153 // addw rd, zero, rs => addw rd, rs, zero
4154 if (MI.getOperand(1).getReg() == RISCV::X0)
4155 commuteInstruction(MI);
4156 // addw rd, rs, zero => addiw rd, rs, 0
4157 if (MI.getOperand(2).getReg() == RISCV::X0) {
4158 MI.getOperand(2).ChangeToImmediate(0);
4159 MI.setDesc(get(RISCV::ADDIW));
4160 return true;
4161 }
4162 break;
4163 case RISCV::SH1ADD:
4164 case RISCV::SH1ADD_UW:
4165 case RISCV::SH2ADD:
4166 case RISCV::SH2ADD_UW:
4167 case RISCV::SH3ADD:
4168 case RISCV::SH3ADD_UW:
4169 // shNadd[.uw] rd, zero, rs => addi rd, rs, 0
4170 if (MI.getOperand(1).getReg() == RISCV::X0) {
4171 MI.removeOperand(1);
4172 MI.addOperand(MachineOperand::CreateImm(0));
4173 MI.setDesc(get(RISCV::ADDI));
4174 return true;
4175 }
4176 // shNadd[.uw] rd, rs, zero => slli[.uw] rd, rs, N
4177 if (MI.getOperand(2).getReg() == RISCV::X0) {
4178 MI.removeOperand(2);
4179 unsigned Opc = MI.getOpcode();
4180 if (Opc == RISCV::SH1ADD_UW || Opc == RISCV::SH2ADD_UW ||
4181 Opc == RISCV::SH3ADD_UW) {
4183 MI.setDesc(get(RISCV::SLLI_UW));
4184 return true;
4185 }
4187 MI.setDesc(get(RISCV::SLLI));
4188 return true;
4189 }
4190 break;
4191 case RISCV::AND:
4192 case RISCV::MUL:
4193 case RISCV::MULH:
4194 case RISCV::MULHSU:
4195 case RISCV::MULHU:
4196 case RISCV::MULW:
4197 // and rd, zero, rs => addi rd, zero, 0
4198 // mul* rd, zero, rs => addi rd, zero, 0
4199 // and rd, rs, zero => addi rd, zero, 0
4200 // mul* rd, rs, zero => addi rd, zero, 0
4201 if (MI.getOperand(1).getReg() == RISCV::X0 ||
4202 MI.getOperand(2).getReg() == RISCV::X0) {
4203 MI.getOperand(1).setReg(RISCV::X0);
4204 MI.getOperand(2).ChangeToImmediate(0);
4205 MI.setDesc(get(RISCV::ADDI));
4206 return true;
4207 }
4208 break;
4209 case RISCV::ANDI:
4210 // andi rd, zero, C => addi rd, zero, 0
4211 if (MI.getOperand(1).getReg() == RISCV::X0) {
4212 MI.getOperand(2).setImm(0);
4213 MI.setDesc(get(RISCV::ADDI));
4214 return true;
4215 }
4216 break;
4217 case RISCV::SLL:
4218 case RISCV::SRL:
4219 case RISCV::SRA:
4220 // shift rd, zero, rs => addi rd, zero, 0
4221 if (MI.getOperand(1).getReg() == RISCV::X0) {
4222 MI.getOperand(2).ChangeToImmediate(0);
4223 MI.setDesc(get(RISCV::ADDI));
4224 return true;
4225 }
4226 // shift rd, rs, zero => addi rd, rs, 0
4227 if (MI.getOperand(2).getReg() == RISCV::X0) {
4228 MI.getOperand(2).ChangeToImmediate(0);
4229 MI.setDesc(get(RISCV::ADDI));
4230 return true;
4231 }
4232 break;
4233 case RISCV::SLLW:
4234 case RISCV::SRLW:
4235 case RISCV::SRAW:
4236 // shiftw rd, zero, rs => addi rd, zero, 0
4237 if (MI.getOperand(1).getReg() == RISCV::X0) {
4238 MI.getOperand(2).ChangeToImmediate(0);
4239 MI.setDesc(get(RISCV::ADDI));
4240 return true;
4241 }
4242 break;
4243 case RISCV::SLLI:
4244 case RISCV::SRLI:
4245 case RISCV::SRAI:
4246 case RISCV::SLLIW:
4247 case RISCV::SRLIW:
4248 case RISCV::SRAIW:
4249 case RISCV::SLLI_UW:
4250 // shiftimm rd, zero, N => addi rd, zero, 0
4251 if (MI.getOperand(1).getReg() == RISCV::X0) {
4252 MI.getOperand(2).setImm(0);
4253 MI.setDesc(get(RISCV::ADDI));
4254 return true;
4255 }
4256 break;
4257 case RISCV::SLTU:
4258 case RISCV::ADD_UW:
4259 // sltu rd, zero, zero => addi rd, zero, 0
4260 // add.uw rd, zero, zero => addi rd, zero, 0
4261 if (MI.getOperand(1).getReg() == RISCV::X0 &&
4262 MI.getOperand(2).getReg() == RISCV::X0) {
4263 MI.getOperand(2).ChangeToImmediate(0);
4264 MI.setDesc(get(RISCV::ADDI));
4265 return true;
4266 }
4267 // add.uw rd, zero, rs => addi rd, rs, 0
4268 if (MI.getOpcode() == RISCV::ADD_UW &&
4269 MI.getOperand(1).getReg() == RISCV::X0) {
4270 MI.removeOperand(1);
4271 MI.addOperand(MachineOperand::CreateImm(0));
4272 MI.setDesc(get(RISCV::ADDI));
4273 }
4274 break;
4275 case RISCV::SLTIU:
4276 // sltiu rd, zero, NZC => addi rd, zero, 1
4277 // sltiu rd, zero, 0 => addi rd, zero, 0
4278 if (MI.getOperand(1).getReg() == RISCV::X0) {
4279 MI.getOperand(2).setImm(MI.getOperand(2).getImm() != 0);
4280 MI.setDesc(get(RISCV::ADDI));
4281 return true;
4282 }
4283 break;
4284 case RISCV::SEXT_H:
4285 case RISCV::SEXT_B:
4286 case RISCV::ZEXT_H_RV32:
4287 case RISCV::ZEXT_H_RV64:
4288 // sext.[hb] rd, zero => addi rd, zero, 0
4289 // zext.h rd, zero => addi rd, zero, 0
4290 if (MI.getOperand(1).getReg() == RISCV::X0) {
4291 MI.addOperand(MachineOperand::CreateImm(0));
4292 MI.setDesc(get(RISCV::ADDI));
4293 return true;
4294 }
4295 break;
4296 case RISCV::MIN:
4297 case RISCV::MINU:
4298 case RISCV::MAX:
4299 case RISCV::MAXU:
4300 // min|max rd, rs, rs => addi rd, rs, 0
4301 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) {
4302 MI.getOperand(2).ChangeToImmediate(0);
4303 MI.setDesc(get(RISCV::ADDI));
4304 return true;
4305 }
4306 break;
4307 case RISCV::BEQ:
4308 case RISCV::BNE:
4309 // b{eq,ne} zero, rs, imm => b{eq,ne} rs, zero, imm
4310 if (MI.getOperand(0).getReg() == RISCV::X0) {
4311 MachineOperand MO0 = MI.getOperand(0);
4312 MI.removeOperand(0);
4313 MI.insert(MI.operands_begin() + 1, {MO0});
4314 }
4315 break;
4316 case RISCV::BLTU:
4317 // bltu zero, rs, imm => bne rs, zero, imm
4318 if (MI.getOperand(0).getReg() == RISCV::X0) {
4319 MachineOperand MO0 = MI.getOperand(0);
4320 MI.removeOperand(0);
4321 MI.insert(MI.operands_begin() + 1, {MO0});
4322 MI.setDesc(get(RISCV::BNE));
4323 }
4324 break;
4325 case RISCV::BGEU:
4326 // bgeu zero, rs, imm => beq rs, zero, imm
4327 if (MI.getOperand(0).getReg() == RISCV::X0) {
4328 MachineOperand MO0 = MI.getOperand(0);
4329 MI.removeOperand(0);
4330 MI.insert(MI.operands_begin() + 1, {MO0});
4331 MI.setDesc(get(RISCV::BEQ));
4332 }
4333 break;
4334 }
4335 return false;
4336}
4337
4338// clang-format off
4339#define CASE_WIDEOP_OPCODE_COMMON(OP, LMUL) \
4340 RISCV::PseudoV##OP##_##LMUL##_TIED
4341
4342#define CASE_WIDEOP_OPCODE_LMULS(OP) \
4343 CASE_WIDEOP_OPCODE_COMMON(OP, MF8): \
4344 case CASE_WIDEOP_OPCODE_COMMON(OP, MF4): \
4345 case CASE_WIDEOP_OPCODE_COMMON(OP, MF2): \
4346 case CASE_WIDEOP_OPCODE_COMMON(OP, M1): \
4347 case CASE_WIDEOP_OPCODE_COMMON(OP, M2): \
4348 case CASE_WIDEOP_OPCODE_COMMON(OP, M4)
4349
4350#define CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, LMUL) \
4351 case RISCV::PseudoV##OP##_##LMUL##_TIED: \
4352 NewOpc = RISCV::PseudoV##OP##_##LMUL; \
4353 break;
4354
4355#define CASE_WIDEOP_CHANGE_OPCODE_LMULS(OP) \
4356 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF8) \
4357 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF4) \
4358 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF2) \
4359 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M1) \
4360 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M2) \
4361 CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M4)
4362
4363// FP Widening Ops may by SEW aware. Create SEW aware cases for these cases.
4364#define CASE_FP_WIDEOP_OPCODE_COMMON(OP, LMUL, SEW) \
4365 RISCV::PseudoV##OP##_##LMUL##_##SEW##_TIED
4366
4367#define CASE_FP_WIDEOP_OPCODE_LMULS(OP) \
4368 CASE_FP_WIDEOP_OPCODE_COMMON(OP, MF4, E16): \
4369 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, MF2, E16): \
4370 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, MF2, E32): \
4371 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, M1, E16): \
4372 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, M1, E32): \
4373 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, M2, E16): \
4374 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, M2, E32): \
4375 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, M4, E16): \
4376 case CASE_FP_WIDEOP_OPCODE_COMMON(OP, M4, E32) \
4377
4378#define CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, LMUL, SEW) \
4379 case RISCV::PseudoV##OP##_##LMUL##_##SEW##_TIED: \
4380 NewOpc = RISCV::PseudoV##OP##_##LMUL##_##SEW; \
4381 break;
4382
4383#define CASE_FP_WIDEOP_CHANGE_OPCODE_LMULS(OP) \
4384 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF4, E16) \
4385 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF2, E16) \
4386 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF2, E32) \
4387 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, M1, E16) \
4388 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, M1, E32) \
4389 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, M2, E16) \
4390 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, M2, E32) \
4391 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, M4, E16) \
4392 CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON(OP, M4, E32) \
4393// clang-format on
4394
4396 LiveVariables *LV,
4397 LiveIntervals *LIS) const {
4399 switch (MI.getOpcode()) {
4400 default:
4401 return nullptr;
4402 case CASE_FP_WIDEOP_OPCODE_LMULS(FWADD_WV):
4403 case CASE_FP_WIDEOP_OPCODE_LMULS(FWSUB_WV): {
4404 assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags) &&
4405 MI.getNumExplicitOperands() == 7 &&
4406 "Expect 7 explicit operands rd, rs2, rs1, rm, vl, sew, policy");
4407 // If the tail policy is undisturbed we can't convert.
4408 if ((MI.getOperand(RISCVII::getVecPolicyOpNum(MI.getDesc())).getImm() &
4409 1) == 0)
4410 return nullptr;
4411 // clang-format off
4412 unsigned NewOpc;
4413 switch (MI.getOpcode()) {
4414 default:
4415 llvm_unreachable("Unexpected opcode");
4418 }
4419 // clang-format on
4420
4421 MachineBasicBlock &MBB = *MI.getParent();
4422 MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc))
4423 .add(MI.getOperand(0))
4424 .addReg(MI.getOperand(0).getReg(), RegState::Undef)
4425 .add(MI.getOperand(1))
4426 .add(MI.getOperand(2))
4427 .add(MI.getOperand(3))
4428 .add(MI.getOperand(4))
4429 .add(MI.getOperand(5))
4430 .add(MI.getOperand(6));
4431 break;
4432 }
4433 case CASE_WIDEOP_OPCODE_LMULS(WADD_WV):
4434 case CASE_WIDEOP_OPCODE_LMULS(WADDU_WV):
4435 case CASE_WIDEOP_OPCODE_LMULS(WSUB_WV):
4436 case CASE_WIDEOP_OPCODE_LMULS(WSUBU_WV): {
4437 // If the tail policy is undisturbed we can't convert.
4438 assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags) &&
4439 MI.getNumExplicitOperands() == 6);
4440 if ((MI.getOperand(RISCVII::getVecPolicyOpNum(MI.getDesc())).getImm() &
4441 1) == 0)
4442 return nullptr;
4443
4444 // clang-format off
4445 unsigned NewOpc;
4446 switch (MI.getOpcode()) {
4447 default:
4448 llvm_unreachable("Unexpected opcode");
4453 }
4454 // clang-format on
4455
4456 MachineBasicBlock &MBB = *MI.getParent();
4457 MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc))
4458 .add(MI.getOperand(0))
4459 .addReg(MI.getOperand(0).getReg(), RegState::Undef)
4460 .add(MI.getOperand(1))
4461 .add(MI.getOperand(2))
4462 .add(MI.getOperand(3))
4463 .add(MI.getOperand(4))
4464 .add(MI.getOperand(5));
4465 break;
4466 }
4467 }
4468 MIB.copyImplicitOps(MI);
4469
4470 if (LV) {
4471 unsigned NumOps = MI.getNumOperands();
4472 for (unsigned I = 1; I < NumOps; ++I) {
4473 MachineOperand &Op = MI.getOperand(I);
4474 if (Op.isReg() && Op.isKill())
4475 LV->replaceKillInstruction(Op.getReg(), MI, *MIB);
4476 }
4477 }
4478
4479 if (LIS) {
4480 SlotIndex Idx = LIS->ReplaceMachineInstrInMaps(MI, *MIB);
4481
4482 if (MI.getOperand(0).isEarlyClobber()) {
4483 // Use operand 1 was tied to early-clobber def operand 0, so its live
4484 // interval could have ended at an early-clobber slot. Now they are not
4485 // tied we need to update it to the normal register slot.
4486 LiveInterval &LI = LIS->getInterval(MI.getOperand(1).getReg());
4488 if (S->end == Idx.getRegSlot(true))
4489 S->end = Idx.getRegSlot();
4490 }
4491 }
4492
4493 return MIB;
4494}
4495
4496#undef CASE_WIDEOP_OPCODE_COMMON
4497#undef CASE_WIDEOP_OPCODE_LMULS
4498#undef CASE_WIDEOP_CHANGE_OPCODE_COMMON
4499#undef CASE_WIDEOP_CHANGE_OPCODE_LMULS
4500#undef CASE_FP_WIDEOP_OPCODE_COMMON
4501#undef CASE_FP_WIDEOP_OPCODE_LMULS
4502#undef CASE_FP_WIDEOP_CHANGE_OPCODE_COMMON
4503#undef CASE_FP_WIDEOP_CHANGE_OPCODE_LMULS
4504
4507 Register DestReg, uint32_t Amount,
4508 MachineInstr::MIFlag Flag) const {
4510 if (llvm::has_single_bit<uint32_t>(Amount)) {
4511 uint32_t ShiftAmount = Log2_32(Amount);
4512 if (ShiftAmount == 0)
4513 return;
4514 BuildMI(MBB, II, DL, get(RISCV::SLLI), DestReg)
4515 .addReg(DestReg, RegState::Kill)
4516 .addImm(ShiftAmount)
4517 .setMIFlag(Flag);
4518 } else if (STI.hasShlAdd(3) &&
4519 ((Amount % 3 == 0 && isPowerOf2_64(Amount / 3)) ||
4520 (Amount % 5 == 0 && isPowerOf2_64(Amount / 5)) ||
4521 (Amount % 9 == 0 && isPowerOf2_64(Amount / 9)))) {
4522 // We can use Zba SHXADD+SLLI instructions for multiply in some cases.
4523 unsigned Opc;
4524 uint32_t ShiftAmount;
4525 if (Amount % 9 == 0) {
4526 Opc = RISCV::SH3ADD;
4527 ShiftAmount = Log2_64(Amount / 9);
4528 } else if (Amount % 5 == 0) {
4529 Opc = RISCV::SH2ADD;
4530 ShiftAmount = Log2_64(Amount / 5);
4531 } else if (Amount % 3 == 0) {
4532 Opc = RISCV::SH1ADD;
4533 ShiftAmount = Log2_64(Amount / 3);
4534 } else {
4535 llvm_unreachable("implied by if-clause");
4536 }
4537 if (ShiftAmount)
4538 BuildMI(MBB, II, DL, get(RISCV::SLLI), DestReg)
4539 .addReg(DestReg, RegState::Kill)
4540 .addImm(ShiftAmount)
4541 .setMIFlag(Flag);
4542 BuildMI(MBB, II, DL, get(Opc), DestReg)
4543 .addReg(DestReg, RegState::Kill)
4544 .addReg(DestReg)
4545 .setMIFlag(Flag);
4546 } else if (llvm::has_single_bit<uint32_t>(Amount - 1)) {
4547 Register ScaledRegister = MRI.createVirtualRegister(&RISCV::GPRRegClass);
4548 uint32_t ShiftAmount = Log2_32(Amount - 1);
4549 BuildMI(MBB, II, DL, get(RISCV::SLLI), ScaledRegister)
4550 .addReg(DestReg)
4551 .addImm(ShiftAmount)
4552 .setMIFlag(Flag);
4553 BuildMI(MBB, II, DL, get(RISCV::ADD), DestReg)
4554 .addReg(ScaledRegister, RegState::Kill)
4555 .addReg(DestReg, RegState::Kill)
4556 .setMIFlag(Flag);
4557 } else if (llvm::has_single_bit<uint32_t>(Amount + 1)) {
4558 Register ScaledRegister = MRI.createVirtualRegister(&RISCV::GPRRegClass);
4559 uint32_t ShiftAmount = Log2_32(Amount + 1);
4560 BuildMI(MBB, II, DL, get(RISCV::SLLI), ScaledRegister)
4561 .addReg(DestReg)
4562 .addImm(ShiftAmount)
4563 .setMIFlag(Flag);
4564 BuildMI(MBB, II, DL, get(RISCV::SUB), DestReg)
4565 .addReg(ScaledRegister, RegState::Kill)
4566 .addReg(DestReg, RegState::Kill)
4567 .setMIFlag(Flag);
4568 } else if (STI.hasStdExtZmmul()) {
4569 Register N = MRI.createVirtualRegister(&RISCV::GPRRegClass);
4570 movImm(MBB, II, DL, N, Amount, Flag);
4571 BuildMI(MBB, II, DL, get(RISCV::MUL), DestReg)
4572 .addReg(DestReg, RegState::Kill)
4574 .setMIFlag(Flag);
4575 } else {
4576 Register Acc;
4577 uint32_t PrevShiftAmount = 0;
4578 for (uint32_t ShiftAmount = 0; Amount >> ShiftAmount; ShiftAmount++) {
4579 if (Amount & (1U << ShiftAmount)) {
4580 if (ShiftAmount)
4581 BuildMI(MBB, II, DL, get(RISCV::SLLI), DestReg)
4582 .addReg(DestReg, RegState::Kill)
4583 .addImm(ShiftAmount - PrevShiftAmount)
4584 .setMIFlag(Flag);
4585 if (Amount >> (ShiftAmount + 1)) {
4586 // If we don't have an accmulator yet, create it and copy DestReg.
4587 if (!Acc) {
4588 Acc = MRI.createVirtualRegister(&RISCV::GPRRegClass);
4589 BuildMI(MBB, II, DL, get(TargetOpcode::COPY), Acc)
4590 .addReg(DestReg)
4591 .setMIFlag(Flag);
4592 } else {
4593 BuildMI(MBB, II, DL, get(RISCV::ADD), Acc)
4594 .addReg(Acc, RegState::Kill)
4595 .addReg(DestReg)
4596 .setMIFlag(Flag);
4597 }
4598 }
4599 PrevShiftAmount = ShiftAmount;
4600 }
4601 }
4602 assert(Acc && "Expected valid accumulator");
4603 BuildMI(MBB, II, DL, get(RISCV::ADD), DestReg)
4604 .addReg(DestReg, RegState::Kill)
4605 .addReg(Acc, RegState::Kill)
4606 .setMIFlag(Flag);
4607 }
4608}
4609
4612 static const std::pair<MachineMemOperand::Flags, const char *> TargetFlags[] =
4613 {{MONontemporalBit0, "riscv-nontemporal-domain-bit-0"},
4614 {MONontemporalBit1, "riscv-nontemporal-domain-bit-1"}};
4615 return ArrayRef(TargetFlags);
4616}
4617
4619 return OptLevel >= CodeGenOptLevel::Aggressive
4620 ? STI.getTailDupAggressiveThreshold()
4621 : 2;
4622}
4623
4625 // RVV lacks any support for immediate addressing for stack addresses, so be
4626 // conservative.
4627 unsigned Opcode = MI.getOpcode();
4628 if (!RISCVVPseudosTable::getPseudoInfo(Opcode) &&
4630 return false;
4631 return true;
4632}
4633
4634std::optional<std::pair<unsigned, unsigned>>
4636 switch (Opcode) {
4637 default:
4638 return std::nullopt;
4639 case RISCV::PseudoVSPILL2_M1:
4640 case RISCV::PseudoVRELOAD2_M1:
4641 return std::make_pair(2u, 1u);
4642 case RISCV::PseudoVSPILL2_M2:
4643 case RISCV::PseudoVRELOAD2_M2:
4644 return std::make_pair(2u, 2u);
4645 case RISCV::PseudoVSPILL2_M4:
4646 case RISCV::PseudoVRELOAD2_M4:
4647 return std::make_pair(2u, 4u);
4648 case RISCV::PseudoVSPILL3_M1:
4649 case RISCV::PseudoVRELOAD3_M1:
4650 return std::make_pair(3u, 1u);
4651 case RISCV::PseudoVSPILL3_M2:
4652 case RISCV::PseudoVRELOAD3_M2:
4653 return std::make_pair(3u, 2u);
4654 case RISCV::PseudoVSPILL4_M1:
4655 case RISCV::PseudoVRELOAD4_M1:
4656 return std::make_pair(4u, 1u);
4657 case RISCV::PseudoVSPILL4_M2:
4658 case RISCV::PseudoVRELOAD4_M2:
4659 return std::make_pair(4u, 2u);
4660 case RISCV::PseudoVSPILL5_M1:
4661 case RISCV::PseudoVRELOAD5_M1:
4662 return std::make_pair(5u, 1u);
4663 case RISCV::PseudoVSPILL6_M1:
4664 case RISCV::PseudoVRELOAD6_M1:
4665 return std::make_pair(6u, 1u);
4666 case RISCV::PseudoVSPILL7_M1:
4667 case RISCV::PseudoVRELOAD7_M1:
4668 return std::make_pair(7u, 1u);
4669 case RISCV::PseudoVSPILL8_M1:
4670 case RISCV::PseudoVRELOAD8_M1:
4671 return std::make_pair(8u, 1u);
4672 }
4673}
4674
4675bool RISCV::hasEqualFRM(const MachineInstr &MI1, const MachineInstr &MI2) {
4676 int16_t MI1FrmOpIdx =
4677 RISCV::getNamedOperandIdx(MI1.getOpcode(), RISCV::OpName::frm);
4678 int16_t MI2FrmOpIdx =
4679 RISCV::getNamedOperandIdx(MI2.getOpcode(), RISCV::OpName::frm);
4680 if (MI1FrmOpIdx < 0 || MI2FrmOpIdx < 0)
4681 return false;
4682 MachineOperand FrmOp1 = MI1.getOperand(MI1FrmOpIdx);
4683 MachineOperand FrmOp2 = MI2.getOperand(MI2FrmOpIdx);
4684 return FrmOp1.getImm() == FrmOp2.getImm();
4685}
4686
4687std::optional<unsigned>
4688RISCV::getVectorLowDemandedScalarBits(unsigned Opcode, unsigned Log2SEW) {
4689 switch (Opcode) {
4690 default:
4691 return std::nullopt;
4692
4693 // 11.6. Vector Single-Width Shift Instructions
4694 case RISCV::VSLL_VX:
4695 case RISCV::VSRL_VX:
4696 case RISCV::VSRA_VX:
4697 // 12.4. Vector Single-Width Scaling Shift Instructions
4698 case RISCV::VSSRL_VX:
4699 case RISCV::VSSRA_VX:
4700 // Zvbb
4701 case RISCV::VROL_VX:
4702 case RISCV::VROR_VX:
4703 // Only the low lg2(SEW) bits of the shift-amount value are used.
4704 return Log2SEW;
4705
4706 // 11.7 Vector Narrowing Integer Right Shift Instructions
4707 case RISCV::VNSRL_WX:
4708 case RISCV::VNSRA_WX:
4709 // 12.5. Vector Narrowing Fixed-Point Clip Instructions
4710 case RISCV::VNCLIPU_WX:
4711 case RISCV::VNCLIP_WX:
4712 // Zvbb
4713 case RISCV::VWSLL_VX:
4714 // Only the low lg2(2*SEW) bits of the shift-amount value are used.
4715 return Log2SEW + 1;
4716
4717 // 11.1. Vector Single-Width Integer Add and Subtract
4718 case RISCV::VADD_VX:
4719 case RISCV::VSUB_VX:
4720 case RISCV::VRSUB_VX:
4721 // 11.2. Vector Widening Integer Add/Subtract
4722 case RISCV::VWADDU_VX:
4723 case RISCV::VWSUBU_VX:
4724 case RISCV::VWADD_VX:
4725 case RISCV::VWSUB_VX:
4726 case RISCV::VWADDU_WX:
4727 case RISCV::VWSUBU_WX:
4728 case RISCV::VWADD_WX:
4729 case RISCV::VWSUB_WX:
4730 // 11.4. Vector Integer Add-with-Carry / Subtract-with-Borrow Instructions
4731 case RISCV::VADC_VXM:
4732 case RISCV::VADC_VIM:
4733 case RISCV::VMADC_VXM:
4734 case RISCV::VMADC_VIM:
4735 case RISCV::VMADC_VX:
4736 case RISCV::VSBC_VXM:
4737 case RISCV::VMSBC_VXM:
4738 case RISCV::VMSBC_VX:
4739 // 11.5 Vector Bitwise Logical Instructions
4740 case RISCV::VAND_VX:
4741 case RISCV::VOR_VX:
4742 case RISCV::VXOR_VX:
4743 // 11.8. Vector Integer Compare Instructions
4744 case RISCV::VMSEQ_VX:
4745 case RISCV::VMSNE_VX:
4746 case RISCV::VMSLTU_VX:
4747 case RISCV::VMSLT_VX:
4748 case RISCV::VMSLEU_VX:
4749 case RISCV::VMSLE_VX:
4750 case RISCV::VMSGTU_VX:
4751 case RISCV::VMSGT_VX:
4752 // 11.9. Vector Integer Min/Max Instructions
4753 case RISCV::VMINU_VX:
4754 case RISCV::VMIN_VX:
4755 case RISCV::VMAXU_VX:
4756 case RISCV::VMAX_VX:
4757 // 11.10. Vector Single-Width Integer Multiply Instructions
4758 case RISCV::VMUL_VX:
4759 case RISCV::VMULH_VX:
4760 case RISCV::VMULHU_VX:
4761 case RISCV::VMULHSU_VX:
4762 // 11.11. Vector Integer Divide Instructions
4763 case RISCV::VDIVU_VX:
4764 case RISCV::VDIV_VX:
4765 case RISCV::VREMU_VX:
4766 case RISCV::VREM_VX:
4767 // 11.12. Vector Widening Integer Multiply Instructions
4768 case RISCV::VWMUL_VX:
4769 case RISCV::VWMULU_VX:
4770 case RISCV::VWMULSU_VX:
4771 // 11.13. Vector Single-Width Integer Multiply-Add Instructions
4772 case RISCV::VMACC_VX:
4773 case RISCV::VNMSAC_VX:
4774 case RISCV::VMADD_VX:
4775 case RISCV::VNMSUB_VX:
4776 // 11.14. Vector Widening Integer Multiply-Add Instructions
4777 case RISCV::VWMACCU_VX:
4778 case RISCV::VWMACC_VX:
4779 case RISCV::VWMACCSU_VX:
4780 case RISCV::VWMACCUS_VX:
4781 // 11.15. Vector Integer Merge Instructions
4782 case RISCV::VMERGE_VXM:
4783 // 11.16. Vector Integer Move Instructions
4784 case RISCV::VMV_V_X:
4785 // 12.1. Vector Single-Width Saturating Add and Subtract
4786 case RISCV::VSADDU_VX:
4787 case RISCV::VSADD_VX:
4788 case RISCV::VSSUBU_VX:
4789 case RISCV::VSSUB_VX:
4790 // 12.2. Vector Single-Width Averaging Add and Subtract
4791 case RISCV::VAADDU_VX:
4792 case RISCV::VAADD_VX:
4793 case RISCV::VASUBU_VX:
4794 case RISCV::VASUB_VX:
4795 // 12.3. Vector Single-Width Fractional Multiply with Rounding and Saturation
4796 case RISCV::VSMUL_VX:
4797 // 16.1. Integer Scalar Move Instructions
4798 case RISCV::VMV_S_X:
4799 // Zvbb
4800 case RISCV::VANDN_VX:
4801 return 1U << Log2SEW;
4802 }
4803}
4804
4805unsigned RISCV::getRVVMCOpcode(unsigned RVVPseudoOpcode) {
4807 RISCVVPseudosTable::getPseudoInfo(RVVPseudoOpcode);
4808 if (!RVV)
4809 return 0;
4810 return RVV->BaseInstr;
4811}
4812
4813unsigned RISCV::getDestLog2EEW(const MCInstrDesc &Desc, unsigned Log2SEW) {
4814 unsigned DestEEW =
4816 // EEW = 1
4817 if (DestEEW == 0)
4818 return 0;
4819 // EEW = SEW * n
4820 unsigned Scaled = Log2SEW + (DestEEW - 1);
4821 assert(Scaled >= 3 && Scaled <= 6);
4822 return Scaled;
4823}
4824
4825static std::optional<int64_t> getEffectiveImm(const MachineOperand &MO) {
4826 assert(MO.isImm() || MO.getReg().isVirtual());
4827 if (MO.isImm())
4828 return MO.getImm();
4829 const MachineInstr *Def =
4830 MO.getParent()->getMF()->getRegInfo().getVRegDef(MO.getReg());
4831 int64_t Imm;
4832 if (isLoadImm(Def, Imm))
4833 return Imm;
4834 return std::nullopt;
4835}
4836
4837/// Given two VL operands, do we know that LHS <= RHS? Must be used in SSA form.
4839 assert((LHS.isImm() || LHS.getParent()->getMF()->getRegInfo().isSSA()) &&
4840 (RHS.isImm() || RHS.getParent()->getMF()->getRegInfo().isSSA()));
4841 if (LHS.isReg() && RHS.isReg() && LHS.getReg().isVirtual() &&
4842 LHS.getReg() == RHS.getReg())
4843 return true;
4844 if (RHS.isImm() && RHS.getImm() == RISCV::VLMaxSentinel)
4845 return true;
4846 if (LHS.isImm() && LHS.getImm() == 0)
4847 return true;
4848 if (LHS.isImm() && LHS.getImm() == RISCV::VLMaxSentinel)
4849 return false;
4850 std::optional<int64_t> LHSImm = getEffectiveImm(LHS),
4851 RHSImm = getEffectiveImm(RHS);
4852 if (!LHSImm || !RHSImm)
4853 return false;
4854 return LHSImm <= RHSImm;
4855}
4856
4857namespace {
4858class RISCVPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo {
4859 const MachineInstr *LHS;
4860 const MachineInstr *RHS;
4862
4863public:
4864 RISCVPipelinerLoopInfo(const MachineInstr *LHS, const MachineInstr *RHS,
4866 : LHS(LHS), RHS(RHS), Cond(Cond.begin(), Cond.end()) {}
4867
4868 bool shouldIgnoreForPipelining(const MachineInstr *MI) const override {
4869 // Make the instructions for loop control be placed in stage 0.
4870 // The predecessors of LHS/RHS are considered by the caller.
4871 if (LHS && MI == LHS)
4872 return true;
4873 if (RHS && MI == RHS)
4874 return true;
4875 return false;
4876 }
4877
4878 std::optional<bool> createTripCountGreaterCondition(
4879 int TC, MachineBasicBlock &MBB,
4880 SmallVectorImpl<MachineOperand> &CondParam) override {
4881 // A branch instruction will be inserted as "if (Cond) goto epilogue".
4882 // Cond is normalized for such use.
4883 // The predecessors of the branch are assumed to have already been inserted.
4884 CondParam = Cond;
4885 return {};
4886 }
4887
4888 void setPreheader(MachineBasicBlock *NewPreheader) override {}
4889
4890 void adjustTripCount(int TripCountAdjust) override {}
4891};
4892} // namespace
4893
4894std::unique_ptr<TargetInstrInfo::PipelinerLoopInfo>
4896 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
4898 if (analyzeBranch(*LoopBB, TBB, FBB, Cond, /*AllowModify=*/false))
4899 return nullptr;
4900
4901 // Infinite loops are not supported
4902 if (TBB == LoopBB && FBB == LoopBB)
4903 return nullptr;
4904
4905 // Must be conditional branch
4906 if (FBB == nullptr)
4907 return nullptr;
4908
4909 assert((TBB == LoopBB || FBB == LoopBB) &&
4910 "The Loop must be a single-basic-block loop");
4911
4912 // Normalization for createTripCountGreaterCondition()
4913 if (TBB == LoopBB)
4915
4916 const MachineRegisterInfo &MRI = LoopBB->getParent()->getRegInfo();
4917 auto FindRegDef = [&MRI](MachineOperand &Op) -> const MachineInstr * {
4918 if (!Op.isReg())
4919 return nullptr;
4920 Register Reg = Op.getReg();
4921 if (!Reg.isVirtual())
4922 return nullptr;
4923 return MRI.getVRegDef(Reg);
4924 };
4925
4926 const MachineInstr *LHS = FindRegDef(Cond[1]);
4927 const MachineInstr *RHS = FindRegDef(Cond[2]);
4928 if (LHS && LHS->isPHI())
4929 return nullptr;
4930 if (RHS && RHS->isPHI())
4931 return nullptr;
4932
4933 return std::make_unique<RISCVPipelinerLoopInfo>(LHS, RHS, Cond);
4934}
4935
4936// FIXME: We should remove this if we have a default generic scheduling model.
4938 unsigned RVVMCOpcode = RISCV::getRVVMCOpcode(Opc);
4939 Opc = RVVMCOpcode ? RVVMCOpcode : Opc;
4940 switch (Opc) {
4941 default:
4942 return false;
4943 // Integer div/rem.
4944 case RISCV::DIV:
4945 case RISCV::DIVW:
4946 case RISCV::DIVU:
4947 case RISCV::DIVUW:
4948 case RISCV::REM:
4949 case RISCV::REMW:
4950 case RISCV::REMU:
4951 case RISCV::REMUW:
4952 // Floating-point div/sqrt.
4953 case RISCV::FDIV_H:
4954 case RISCV::FDIV_S:
4955 case RISCV::FDIV_D:
4956 case RISCV::FDIV_H_INX:
4957 case RISCV::FDIV_S_INX:
4958 case RISCV::FDIV_D_INX:
4959 case RISCV::FDIV_D_IN32X:
4960 case RISCV::FSQRT_H:
4961 case RISCV::FSQRT_S:
4962 case RISCV::FSQRT_D:
4963 case RISCV::FSQRT_H_INX:
4964 case RISCV::FSQRT_S_INX:
4965 case RISCV::FSQRT_D_INX:
4966 case RISCV::FSQRT_D_IN32X:
4967 // Vector integer div/rem
4968 case RISCV::VDIV_VV:
4969 case RISCV::VDIV_VX:
4970 case RISCV::VDIVU_VV:
4971 case RISCV::VDIVU_VX:
4972 case RISCV::VREM_VV:
4973 case RISCV::VREM_VX:
4974 case RISCV::VREMU_VV:
4975 case RISCV::VREMU_VX:
4976 // Vector floating-point div/sqrt.
4977 case RISCV::VFDIV_VV:
4978 case RISCV::VFDIV_VF:
4979 case RISCV::VFRDIV_VF:
4980 case RISCV::VFSQRT_V:
4981 case RISCV::VFRSQRT7_V:
4982 return true;
4983 }
4984}
unsigned const MachineRegisterInfo * MRI
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.
@ MachineOutlinerDefault
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
SmallVector< int16_t, MAX_SRC_OPERANDS_NUM > OperandIndices
@ Scaled
static ARCCC::CondCode getOppositeBranchCondition(ARCCC::CondCode CC)
Return the inverse of passed condition, i.e. turning COND_E to COND_NE.
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
basic Basic Alias true
#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:55
#define I(x, y, z)
Definition MD5.cpp:58
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_OPCODE_LMULS(OP)
#define CASE_OPERAND_SIMM(NUM)
static std::optional< unsigned > getLMULForRVVWholeLoadStore(unsigned Opcode)
#define CASE_VFMA_CHANGE_OPCODE_VV(OLDOP, NEWOP)
static bool analyzeCandidate(outliner::Candidate &C)
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_WIDEOP_OPCODE_LMULS(OP)
static bool isMIReadsReg(const MachineInstr &MI, const TargetRegisterInfo *TRI, MCRegister RegNo)
#define OPCODE_LMUL_MASK_CASE(OPC)
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)
MachineOutlinerConstructionID
#define CASE_RVV_OPCODE_WIDEN(OP)
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 bool getFPPatterns(MachineInstr &Root, SmallVectorImpl< unsigned > &Patterns, bool DoRegPressureReduce)
#define OPCODE_LMUL_CASE(OPC)
#define CASE_OPERAND_UIMM(NUM)
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)
static MachineInstr * canFoldAsPredicatedOp(Register Reg, const MachineRegisterInfo &MRI, const TargetInstrInfo *TII)
Identify instructions that can be folded into a CCMOV instruction, and return the defining instructio...
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)
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:480
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
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
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
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
const T & front() const
front - Get the first element.
Definition ArrayRef.h:150
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
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:199
A debug info location.
Definition DebugLoc.h:124
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:214
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:703
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:87
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:33
const FeatureBitset & getFeatureBits() const
Set of metadata that should be preserved when using BuildMI().
MachineInstrBundleIterator< const MachineInstr > const_iterator
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
Instructions::const_iterator const_instr_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 & 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 & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
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
bool getFlag(MIFlag Flag) const
Return whether an MI flag is set.
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.
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 MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
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
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.
bool isReallyTriviallyReMaterializable(const MachineInstr &MI) const override
RISCVInstrInfo(const RISCVSubtarget &STI)
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool IsKill, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI, Register VReg, 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
MachineInstr * optimizeSelect(MachineInstr &MI, SmallPtrSetImpl< MachineInstr * > &SeenMIs, bool) const override
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
MachineInstr * foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI, ArrayRef< unsigned > Ops, MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS=nullptr, VirtRegMap *VRM=nullptr) const override
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
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
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DstReg, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) 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
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
bool analyzeSelect(const MachineInstr &MI, SmallVectorImpl< MachineOperand > &Cond, unsigned &TrueOp, unsigned &FalseOp, bool &Optimizable) 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
void enterBasicBlockEnd(MachineBasicBlock &MBB)
Start tracking liveness from the end of basic block MBB.
void setRegUsed(Register Reg, LaneBitmask LaneMask=LaneBitmask::getAll())
Tell the scavenger a register is used.
Register scavengeRegisterBackwards(const TargetRegisterClass &RC, MachineBasicBlock::iterator To, bool RestoreAfter, int SPAdj, bool AllowSpill=true)
Make a register of the specific register class available from the current position backwards to the p...
Wrapper class representing virtual and physical registers.
Definition Register.h:19
constexpr bool isValid() const
Definition Register.h:107
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:74
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
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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 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 bool isReallyTriviallyReMaterializable(const MachineInstr &MI) const
For instructions with opcodes for which the M_REMATERIALIZABLE flag is set, this hook lets the target...
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
A raw_ostream that writes to an std::string.
Changed
#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 getOppositeBranchCondition(CondCode)
unsigned getBrCond(CondCode CC, unsigned SelectOpc=0)
static bool isValidRoundingMode(unsigned Mode)
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 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_SIMM10_LSB0000_NONZERO
static unsigned getNF(uint8_t TSFlags)
static RISCVVType::VLMUL getLMul(uint8_t TSFlags)
static bool isTailAgnostic(unsigned VType)
LLVM_ABI std::pair< unsigned, bool > decodeVLMUL(VLMUL VLMul)
static bool isValidSEW(unsigned SEW)
LLVM_ABI void printVType(unsigned VType, raw_ostream &OS)
static unsigned getSEW(unsigned VType)
static VLMUL getVLMUL(unsigned VType)
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
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Dead
Unused definition.
@ Define
Register definition.
@ Kill
The last use of a register.
@ Undef
Value of the register doesn't matter.
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:318
@ Offset
Definition DWP.cpp:477
@ 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:1705
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:174
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:2452
static const MachineMemOperand::Flags MONontemporalBit0
unsigned getDeadRegState(bool B)
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:293
Op::Description Desc
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:348
constexpr bool has_single_bit(T Value) noexcept
Definition bit.h:147
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1712
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:342
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
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:198
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:82
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:548
unsigned getKillRegState(bool B)
unsigned getRenamableRegState(bool B)
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:191
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:2100
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:583
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:207
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:853
#define N
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.