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