1 //===-- RISCVInstrInfo.cpp - RISCV 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 RISCV implementation of the TargetInstrInfo class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "RISCVInstrInfo.h"
14 #include "MCTargetDesc/RISCVMatInt.h"
15 #include "RISCV.h"
16 #include "RISCVMachineFunctionInfo.h"
17 #include "RISCVSubtarget.h"
18 #include "RISCVTargetMachine.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Analysis/MemoryLocation.h"
22 #include "llvm/CodeGen/LiveVariables.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/RegisterScavenging.h"
27 #include "llvm/MC/MCInstBuilder.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/TargetRegistry.h"
30 
31 using namespace llvm;
32 
33 #define GEN_CHECK_COMPRESS_INSTR
34 #include "RISCVGenCompressInstEmitter.inc"
35 
36 #define GET_INSTRINFO_CTOR_DTOR
37 #include "RISCVGenInstrInfo.inc"
38 
39 namespace llvm {
40 namespace RISCVVPseudosTable {
41 
42 using namespace RISCV;
43 
44 #define GET_RISCVVPseudosTable_IMPL
45 #include "RISCVGenSearchableTables.inc"
46 
47 } // namespace RISCVVPseudosTable
48 } // namespace llvm
49 
50 RISCVInstrInfo::RISCVInstrInfo(RISCVSubtarget &STI)
51     : RISCVGenInstrInfo(RISCV::ADJCALLSTACKDOWN, RISCV::ADJCALLSTACKUP),
52       STI(STI) {}
53 
54 MCInst RISCVInstrInfo::getNop() const {
55   if (STI.getFeatureBits()[RISCV::FeatureStdExtC])
56     return MCInstBuilder(RISCV::C_NOP);
57   return MCInstBuilder(RISCV::ADDI)
58       .addReg(RISCV::X0)
59       .addReg(RISCV::X0)
60       .addImm(0);
61 }
62 
63 unsigned RISCVInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
64                                              int &FrameIndex) const {
65   switch (MI.getOpcode()) {
66   default:
67     return 0;
68   case RISCV::LB:
69   case RISCV::LBU:
70   case RISCV::LH:
71   case RISCV::LHU:
72   case RISCV::FLH:
73   case RISCV::LW:
74   case RISCV::FLW:
75   case RISCV::LWU:
76   case RISCV::LD:
77   case RISCV::FLD:
78     break;
79   }
80 
81   if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
82       MI.getOperand(2).getImm() == 0) {
83     FrameIndex = MI.getOperand(1).getIndex();
84     return MI.getOperand(0).getReg();
85   }
86 
87   return 0;
88 }
89 
90 unsigned RISCVInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
91                                             int &FrameIndex) const {
92   switch (MI.getOpcode()) {
93   default:
94     return 0;
95   case RISCV::SB:
96   case RISCV::SH:
97   case RISCV::SW:
98   case RISCV::FSH:
99   case RISCV::FSW:
100   case RISCV::SD:
101   case RISCV::FSD:
102     break;
103   }
104 
105   if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
106       MI.getOperand(2).getImm() == 0) {
107     FrameIndex = MI.getOperand(1).getIndex();
108     return MI.getOperand(0).getReg();
109   }
110 
111   return 0;
112 }
113 
114 static bool forwardCopyWillClobberTuple(unsigned DstReg, unsigned SrcReg,
115                                         unsigned NumRegs) {
116   // We really want the positive remainder mod 32 here, that happens to be
117   // easily obtainable with a mask.
118   return ((DstReg - SrcReg) & 0x1f) < NumRegs;
119 }
120 
121 void RISCVInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
122                                  MachineBasicBlock::iterator MBBI,
123                                  const DebugLoc &DL, MCRegister DstReg,
124                                  MCRegister SrcReg, bool KillSrc) const {
125   if (RISCV::GPRRegClass.contains(DstReg, SrcReg)) {
126     BuildMI(MBB, MBBI, DL, get(RISCV::ADDI), DstReg)
127         .addReg(SrcReg, getKillRegState(KillSrc))
128         .addImm(0);
129     return;
130   }
131 
132   // FPR->FPR copies and VR->VR copies.
133   unsigned Opc;
134   bool IsScalableVector = true;
135   unsigned NF = 1;
136   unsigned LMul = 1;
137   unsigned SubRegIdx = RISCV::sub_vrm1_0;
138   if (RISCV::FPR16RegClass.contains(DstReg, SrcReg)) {
139     Opc = RISCV::FSGNJ_H;
140     IsScalableVector = false;
141   } else if (RISCV::FPR32RegClass.contains(DstReg, SrcReg)) {
142     Opc = RISCV::FSGNJ_S;
143     IsScalableVector = false;
144   } else if (RISCV::FPR64RegClass.contains(DstReg, SrcReg)) {
145     Opc = RISCV::FSGNJ_D;
146     IsScalableVector = false;
147   } else if (RISCV::VRRegClass.contains(DstReg, SrcReg)) {
148     Opc = RISCV::PseudoVMV1R_V;
149   } else if (RISCV::VRM2RegClass.contains(DstReg, SrcReg)) {
150     Opc = RISCV::PseudoVMV2R_V;
151   } else if (RISCV::VRM4RegClass.contains(DstReg, SrcReg)) {
152     Opc = RISCV::PseudoVMV4R_V;
153   } else if (RISCV::VRM8RegClass.contains(DstReg, SrcReg)) {
154     Opc = RISCV::PseudoVMV8R_V;
155   } else if (RISCV::VRN2M1RegClass.contains(DstReg, SrcReg)) {
156     Opc = RISCV::PseudoVMV1R_V;
157     SubRegIdx = RISCV::sub_vrm1_0;
158     NF = 2;
159     LMul = 1;
160   } else if (RISCV::VRN2M2RegClass.contains(DstReg, SrcReg)) {
161     Opc = RISCV::PseudoVMV2R_V;
162     SubRegIdx = RISCV::sub_vrm2_0;
163     NF = 2;
164     LMul = 2;
165   } else if (RISCV::VRN2M4RegClass.contains(DstReg, SrcReg)) {
166     Opc = RISCV::PseudoVMV4R_V;
167     SubRegIdx = RISCV::sub_vrm4_0;
168     NF = 2;
169     LMul = 4;
170   } else if (RISCV::VRN3M1RegClass.contains(DstReg, SrcReg)) {
171     Opc = RISCV::PseudoVMV1R_V;
172     SubRegIdx = RISCV::sub_vrm1_0;
173     NF = 3;
174     LMul = 1;
175   } else if (RISCV::VRN3M2RegClass.contains(DstReg, SrcReg)) {
176     Opc = RISCV::PseudoVMV2R_V;
177     SubRegIdx = RISCV::sub_vrm2_0;
178     NF = 3;
179     LMul = 2;
180   } else if (RISCV::VRN4M1RegClass.contains(DstReg, SrcReg)) {
181     Opc = RISCV::PseudoVMV1R_V;
182     SubRegIdx = RISCV::sub_vrm1_0;
183     NF = 4;
184     LMul = 1;
185   } else if (RISCV::VRN4M2RegClass.contains(DstReg, SrcReg)) {
186     Opc = RISCV::PseudoVMV2R_V;
187     SubRegIdx = RISCV::sub_vrm2_0;
188     NF = 4;
189     LMul = 2;
190   } else if (RISCV::VRN5M1RegClass.contains(DstReg, SrcReg)) {
191     Opc = RISCV::PseudoVMV1R_V;
192     SubRegIdx = RISCV::sub_vrm1_0;
193     NF = 5;
194     LMul = 1;
195   } else if (RISCV::VRN6M1RegClass.contains(DstReg, SrcReg)) {
196     Opc = RISCV::PseudoVMV1R_V;
197     SubRegIdx = RISCV::sub_vrm1_0;
198     NF = 6;
199     LMul = 1;
200   } else if (RISCV::VRN7M1RegClass.contains(DstReg, SrcReg)) {
201     Opc = RISCV::PseudoVMV1R_V;
202     SubRegIdx = RISCV::sub_vrm1_0;
203     NF = 7;
204     LMul = 1;
205   } else if (RISCV::VRN8M1RegClass.contains(DstReg, SrcReg)) {
206     Opc = RISCV::PseudoVMV1R_V;
207     SubRegIdx = RISCV::sub_vrm1_0;
208     NF = 8;
209     LMul = 1;
210   } else {
211     llvm_unreachable("Impossible reg-to-reg copy");
212   }
213 
214   if (IsScalableVector) {
215     if (NF == 1) {
216       BuildMI(MBB, MBBI, DL, get(Opc), DstReg)
217           .addReg(SrcReg, getKillRegState(KillSrc));
218     } else {
219       const TargetRegisterInfo *TRI = STI.getRegisterInfo();
220 
221       int I = 0, End = NF, Incr = 1;
222       unsigned SrcEncoding = TRI->getEncodingValue(SrcReg);
223       unsigned DstEncoding = TRI->getEncodingValue(DstReg);
224       if (forwardCopyWillClobberTuple(DstEncoding, SrcEncoding, NF * LMul)) {
225         I = NF - 1;
226         End = -1;
227         Incr = -1;
228       }
229 
230       for (; I != End; I += Incr) {
231         BuildMI(MBB, MBBI, DL, get(Opc), TRI->getSubReg(DstReg, SubRegIdx + I))
232             .addReg(TRI->getSubReg(SrcReg, SubRegIdx + I),
233                     getKillRegState(KillSrc));
234       }
235     }
236   } else {
237     BuildMI(MBB, MBBI, DL, get(Opc), DstReg)
238         .addReg(SrcReg, getKillRegState(KillSrc))
239         .addReg(SrcReg, getKillRegState(KillSrc));
240   }
241 }
242 
243 void RISCVInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
244                                          MachineBasicBlock::iterator I,
245                                          Register SrcReg, bool IsKill, int FI,
246                                          const TargetRegisterClass *RC,
247                                          const TargetRegisterInfo *TRI) const {
248   DebugLoc DL;
249   if (I != MBB.end())
250     DL = I->getDebugLoc();
251 
252   MachineFunction *MF = MBB.getParent();
253   MachineFrameInfo &MFI = MF->getFrameInfo();
254 
255   unsigned Opcode;
256   bool IsScalableVector = true;
257   bool IsZvlsseg = true;
258   if (RISCV::GPRRegClass.hasSubClassEq(RC)) {
259     Opcode = TRI->getRegSizeInBits(RISCV::GPRRegClass) == 32 ?
260              RISCV::SW : RISCV::SD;
261     IsScalableVector = false;
262   } else if (RISCV::FPR16RegClass.hasSubClassEq(RC)) {
263     Opcode = RISCV::FSH;
264     IsScalableVector = false;
265   } else if (RISCV::FPR32RegClass.hasSubClassEq(RC)) {
266     Opcode = RISCV::FSW;
267     IsScalableVector = false;
268   } else if (RISCV::FPR64RegClass.hasSubClassEq(RC)) {
269     Opcode = RISCV::FSD;
270     IsScalableVector = false;
271   } else if (RISCV::VRRegClass.hasSubClassEq(RC)) {
272     Opcode = RISCV::PseudoVSPILL_M1;
273     IsZvlsseg = false;
274   } else if (RISCV::VRM2RegClass.hasSubClassEq(RC)) {
275     Opcode = RISCV::PseudoVSPILL_M2;
276     IsZvlsseg = false;
277   } else if (RISCV::VRM4RegClass.hasSubClassEq(RC)) {
278     Opcode = RISCV::PseudoVSPILL_M4;
279     IsZvlsseg = false;
280   } else if (RISCV::VRM8RegClass.hasSubClassEq(RC)) {
281     Opcode = RISCV::PseudoVSPILL_M8;
282     IsZvlsseg = false;
283   } else if (RISCV::VRN2M1RegClass.hasSubClassEq(RC))
284     Opcode = RISCV::PseudoVSPILL2_M1;
285   else if (RISCV::VRN2M2RegClass.hasSubClassEq(RC))
286     Opcode = RISCV::PseudoVSPILL2_M2;
287   else if (RISCV::VRN2M4RegClass.hasSubClassEq(RC))
288     Opcode = RISCV::PseudoVSPILL2_M4;
289   else if (RISCV::VRN3M1RegClass.hasSubClassEq(RC))
290     Opcode = RISCV::PseudoVSPILL3_M1;
291   else if (RISCV::VRN3M2RegClass.hasSubClassEq(RC))
292     Opcode = RISCV::PseudoVSPILL3_M2;
293   else if (RISCV::VRN4M1RegClass.hasSubClassEq(RC))
294     Opcode = RISCV::PseudoVSPILL4_M1;
295   else if (RISCV::VRN4M2RegClass.hasSubClassEq(RC))
296     Opcode = RISCV::PseudoVSPILL4_M2;
297   else if (RISCV::VRN5M1RegClass.hasSubClassEq(RC))
298     Opcode = RISCV::PseudoVSPILL5_M1;
299   else if (RISCV::VRN6M1RegClass.hasSubClassEq(RC))
300     Opcode = RISCV::PseudoVSPILL6_M1;
301   else if (RISCV::VRN7M1RegClass.hasSubClassEq(RC))
302     Opcode = RISCV::PseudoVSPILL7_M1;
303   else if (RISCV::VRN8M1RegClass.hasSubClassEq(RC))
304     Opcode = RISCV::PseudoVSPILL8_M1;
305   else
306     llvm_unreachable("Can't store this register to stack slot");
307 
308   if (IsScalableVector) {
309     MachineMemOperand *MMO = MF->getMachineMemOperand(
310         MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore,
311         MemoryLocation::UnknownSize, MFI.getObjectAlign(FI));
312 
313     MFI.setStackID(FI, TargetStackID::ScalableVector);
314     auto MIB = BuildMI(MBB, I, DL, get(Opcode))
315                    .addReg(SrcReg, getKillRegState(IsKill))
316                    .addFrameIndex(FI)
317                    .addMemOperand(MMO);
318     if (IsZvlsseg) {
319       // For spilling/reloading Zvlsseg registers, append the dummy field for
320       // the scaled vector length. The argument will be used when expanding
321       // these pseudo instructions.
322       MIB.addReg(RISCV::X0);
323     }
324   } else {
325     MachineMemOperand *MMO = MF->getMachineMemOperand(
326         MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOStore,
327         MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
328 
329     BuildMI(MBB, I, DL, get(Opcode))
330         .addReg(SrcReg, getKillRegState(IsKill))
331         .addFrameIndex(FI)
332         .addImm(0)
333         .addMemOperand(MMO);
334   }
335 }
336 
337 void RISCVInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
338                                           MachineBasicBlock::iterator I,
339                                           Register DstReg, int FI,
340                                           const TargetRegisterClass *RC,
341                                           const TargetRegisterInfo *TRI) const {
342   DebugLoc DL;
343   if (I != MBB.end())
344     DL = I->getDebugLoc();
345 
346   MachineFunction *MF = MBB.getParent();
347   MachineFrameInfo &MFI = MF->getFrameInfo();
348 
349   unsigned Opcode;
350   bool IsScalableVector = true;
351   bool IsZvlsseg = true;
352   if (RISCV::GPRRegClass.hasSubClassEq(RC)) {
353     Opcode = TRI->getRegSizeInBits(RISCV::GPRRegClass) == 32 ?
354              RISCV::LW : RISCV::LD;
355     IsScalableVector = false;
356   } else if (RISCV::FPR16RegClass.hasSubClassEq(RC)) {
357     Opcode = RISCV::FLH;
358     IsScalableVector = false;
359   } else if (RISCV::FPR32RegClass.hasSubClassEq(RC)) {
360     Opcode = RISCV::FLW;
361     IsScalableVector = false;
362   } else if (RISCV::FPR64RegClass.hasSubClassEq(RC)) {
363     Opcode = RISCV::FLD;
364     IsScalableVector = false;
365   } else if (RISCV::VRRegClass.hasSubClassEq(RC)) {
366     Opcode = RISCV::PseudoVRELOAD_M1;
367     IsZvlsseg = false;
368   } else if (RISCV::VRM2RegClass.hasSubClassEq(RC)) {
369     Opcode = RISCV::PseudoVRELOAD_M2;
370     IsZvlsseg = false;
371   } else if (RISCV::VRM4RegClass.hasSubClassEq(RC)) {
372     Opcode = RISCV::PseudoVRELOAD_M4;
373     IsZvlsseg = false;
374   } else if (RISCV::VRM8RegClass.hasSubClassEq(RC)) {
375     Opcode = RISCV::PseudoVRELOAD_M8;
376     IsZvlsseg = false;
377   } else if (RISCV::VRN2M1RegClass.hasSubClassEq(RC))
378     Opcode = RISCV::PseudoVRELOAD2_M1;
379   else if (RISCV::VRN2M2RegClass.hasSubClassEq(RC))
380     Opcode = RISCV::PseudoVRELOAD2_M2;
381   else if (RISCV::VRN2M4RegClass.hasSubClassEq(RC))
382     Opcode = RISCV::PseudoVRELOAD2_M4;
383   else if (RISCV::VRN3M1RegClass.hasSubClassEq(RC))
384     Opcode = RISCV::PseudoVRELOAD3_M1;
385   else if (RISCV::VRN3M2RegClass.hasSubClassEq(RC))
386     Opcode = RISCV::PseudoVRELOAD3_M2;
387   else if (RISCV::VRN4M1RegClass.hasSubClassEq(RC))
388     Opcode = RISCV::PseudoVRELOAD4_M1;
389   else if (RISCV::VRN4M2RegClass.hasSubClassEq(RC))
390     Opcode = RISCV::PseudoVRELOAD4_M2;
391   else if (RISCV::VRN5M1RegClass.hasSubClassEq(RC))
392     Opcode = RISCV::PseudoVRELOAD5_M1;
393   else if (RISCV::VRN6M1RegClass.hasSubClassEq(RC))
394     Opcode = RISCV::PseudoVRELOAD6_M1;
395   else if (RISCV::VRN7M1RegClass.hasSubClassEq(RC))
396     Opcode = RISCV::PseudoVRELOAD7_M1;
397   else if (RISCV::VRN8M1RegClass.hasSubClassEq(RC))
398     Opcode = RISCV::PseudoVRELOAD8_M1;
399   else
400     llvm_unreachable("Can't load this register from stack slot");
401 
402   if (IsScalableVector) {
403     MachineMemOperand *MMO = MF->getMachineMemOperand(
404         MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad,
405         MemoryLocation::UnknownSize, MFI.getObjectAlign(FI));
406 
407     MFI.setStackID(FI, TargetStackID::ScalableVector);
408     auto MIB = BuildMI(MBB, I, DL, get(Opcode), DstReg)
409                    .addFrameIndex(FI)
410                    .addMemOperand(MMO);
411     if (IsZvlsseg) {
412       // For spilling/reloading Zvlsseg registers, append the dummy field for
413       // the scaled vector length. The argument will be used when expanding
414       // these pseudo instructions.
415       MIB.addReg(RISCV::X0);
416     }
417   } else {
418     MachineMemOperand *MMO = MF->getMachineMemOperand(
419         MachinePointerInfo::getFixedStack(*MF, FI), MachineMemOperand::MOLoad,
420         MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
421 
422     BuildMI(MBB, I, DL, get(Opcode), DstReg)
423         .addFrameIndex(FI)
424         .addImm(0)
425         .addMemOperand(MMO);
426   }
427 }
428 
429 void RISCVInstrInfo::movImm(MachineBasicBlock &MBB,
430                             MachineBasicBlock::iterator MBBI,
431                             const DebugLoc &DL, Register DstReg, uint64_t Val,
432                             MachineInstr::MIFlag Flag) const {
433   MachineFunction *MF = MBB.getParent();
434   MachineRegisterInfo &MRI = MF->getRegInfo();
435   Register SrcReg = RISCV::X0;
436   Register Result = MRI.createVirtualRegister(&RISCV::GPRRegClass);
437   unsigned Num = 0;
438 
439   if (!STI.is64Bit() && !isInt<32>(Val))
440     report_fatal_error("Should only materialize 32-bit constants for RV32");
441 
442   RISCVMatInt::InstSeq Seq =
443       RISCVMatInt::generateInstSeq(Val, STI.getFeatureBits());
444   assert(!Seq.empty());
445 
446   for (RISCVMatInt::Inst &Inst : Seq) {
447     // Write the final result to DstReg if it's the last instruction in the Seq.
448     // Otherwise, write the result to the temp register.
449     if (++Num == Seq.size())
450       Result = DstReg;
451 
452     if (Inst.Opc == RISCV::LUI) {
453       BuildMI(MBB, MBBI, DL, get(RISCV::LUI), Result)
454           .addImm(Inst.Imm)
455           .setMIFlag(Flag);
456     } else if (Inst.Opc == RISCV::ADDUW) {
457       BuildMI(MBB, MBBI, DL, get(RISCV::ADDUW), Result)
458           .addReg(SrcReg, RegState::Kill)
459           .addReg(RISCV::X0)
460           .setMIFlag(Flag);
461     } else {
462       BuildMI(MBB, MBBI, DL, get(Inst.Opc), Result)
463           .addReg(SrcReg, RegState::Kill)
464           .addImm(Inst.Imm)
465           .setMIFlag(Flag);
466     }
467     // Only the first instruction has X0 as its source.
468     SrcReg = Result;
469   }
470 }
471 
472 static RISCVCC::CondCode getCondFromBranchOpc(unsigned Opc) {
473   switch (Opc) {
474   default:
475     return RISCVCC::COND_INVALID;
476   case RISCV::BEQ:
477     return RISCVCC::COND_EQ;
478   case RISCV::BNE:
479     return RISCVCC::COND_NE;
480   case RISCV::BLT:
481     return RISCVCC::COND_LT;
482   case RISCV::BGE:
483     return RISCVCC::COND_GE;
484   case RISCV::BLTU:
485     return RISCVCC::COND_LTU;
486   case RISCV::BGEU:
487     return RISCVCC::COND_GEU;
488   }
489 }
490 
491 // The contents of values added to Cond are not examined outside of
492 // RISCVInstrInfo, giving us flexibility in what to push to it. For RISCV, we
493 // push BranchOpcode, Reg1, Reg2.
494 static void parseCondBranch(MachineInstr &LastInst, MachineBasicBlock *&Target,
495                             SmallVectorImpl<MachineOperand> &Cond) {
496   // Block ends with fall-through condbranch.
497   assert(LastInst.getDesc().isConditionalBranch() &&
498          "Unknown conditional branch");
499   Target = LastInst.getOperand(2).getMBB();
500   unsigned CC = getCondFromBranchOpc(LastInst.getOpcode());
501   Cond.push_back(MachineOperand::CreateImm(CC));
502   Cond.push_back(LastInst.getOperand(0));
503   Cond.push_back(LastInst.getOperand(1));
504 }
505 
506 const MCInstrDesc &RISCVInstrInfo::getBrCond(RISCVCC::CondCode CC) const {
507   switch (CC) {
508   default:
509     llvm_unreachable("Unknown condition code!");
510   case RISCVCC::COND_EQ:
511     return get(RISCV::BEQ);
512   case RISCVCC::COND_NE:
513     return get(RISCV::BNE);
514   case RISCVCC::COND_LT:
515     return get(RISCV::BLT);
516   case RISCVCC::COND_GE:
517     return get(RISCV::BGE);
518   case RISCVCC::COND_LTU:
519     return get(RISCV::BLTU);
520   case RISCVCC::COND_GEU:
521     return get(RISCV::BGEU);
522   }
523 }
524 
525 RISCVCC::CondCode RISCVCC::getOppositeBranchCondition(RISCVCC::CondCode CC) {
526   switch (CC) {
527   default:
528     llvm_unreachable("Unrecognized conditional branch");
529   case RISCVCC::COND_EQ:
530     return RISCVCC::COND_NE;
531   case RISCVCC::COND_NE:
532     return RISCVCC::COND_EQ;
533   case RISCVCC::COND_LT:
534     return RISCVCC::COND_GE;
535   case RISCVCC::COND_GE:
536     return RISCVCC::COND_LT;
537   case RISCVCC::COND_LTU:
538     return RISCVCC::COND_GEU;
539   case RISCVCC::COND_GEU:
540     return RISCVCC::COND_LTU;
541   }
542 }
543 
544 bool RISCVInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
545                                    MachineBasicBlock *&TBB,
546                                    MachineBasicBlock *&FBB,
547                                    SmallVectorImpl<MachineOperand> &Cond,
548                                    bool AllowModify) const {
549   TBB = FBB = nullptr;
550   Cond.clear();
551 
552   // If the block has no terminators, it just falls into the block after it.
553   MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
554   if (I == MBB.end() || !isUnpredicatedTerminator(*I))
555     return false;
556 
557   // Count the number of terminators and find the first unconditional or
558   // indirect branch.
559   MachineBasicBlock::iterator FirstUncondOrIndirectBr = MBB.end();
560   int NumTerminators = 0;
561   for (auto J = I.getReverse(); J != MBB.rend() && isUnpredicatedTerminator(*J);
562        J++) {
563     NumTerminators++;
564     if (J->getDesc().isUnconditionalBranch() ||
565         J->getDesc().isIndirectBranch()) {
566       FirstUncondOrIndirectBr = J.getReverse();
567     }
568   }
569 
570   // If AllowModify is true, we can erase any terminators after
571   // FirstUncondOrIndirectBR.
572   if (AllowModify && FirstUncondOrIndirectBr != MBB.end()) {
573     while (std::next(FirstUncondOrIndirectBr) != MBB.end()) {
574       std::next(FirstUncondOrIndirectBr)->eraseFromParent();
575       NumTerminators--;
576     }
577     I = FirstUncondOrIndirectBr;
578   }
579 
580   // We can't handle blocks that end in an indirect branch.
581   if (I->getDesc().isIndirectBranch())
582     return true;
583 
584   // We can't handle blocks with more than 2 terminators.
585   if (NumTerminators > 2)
586     return true;
587 
588   // Handle a single unconditional branch.
589   if (NumTerminators == 1 && I->getDesc().isUnconditionalBranch()) {
590     TBB = getBranchDestBlock(*I);
591     return false;
592   }
593 
594   // Handle a single conditional branch.
595   if (NumTerminators == 1 && I->getDesc().isConditionalBranch()) {
596     parseCondBranch(*I, TBB, Cond);
597     return false;
598   }
599 
600   // Handle a conditional branch followed by an unconditional branch.
601   if (NumTerminators == 2 && std::prev(I)->getDesc().isConditionalBranch() &&
602       I->getDesc().isUnconditionalBranch()) {
603     parseCondBranch(*std::prev(I), TBB, Cond);
604     FBB = getBranchDestBlock(*I);
605     return false;
606   }
607 
608   // Otherwise, we can't handle this.
609   return true;
610 }
611 
612 unsigned RISCVInstrInfo::removeBranch(MachineBasicBlock &MBB,
613                                       int *BytesRemoved) const {
614   if (BytesRemoved)
615     *BytesRemoved = 0;
616   MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
617   if (I == MBB.end())
618     return 0;
619 
620   if (!I->getDesc().isUnconditionalBranch() &&
621       !I->getDesc().isConditionalBranch())
622     return 0;
623 
624   // Remove the branch.
625   if (BytesRemoved)
626     *BytesRemoved += getInstSizeInBytes(*I);
627   I->eraseFromParent();
628 
629   I = MBB.end();
630 
631   if (I == MBB.begin())
632     return 1;
633   --I;
634   if (!I->getDesc().isConditionalBranch())
635     return 1;
636 
637   // Remove the branch.
638   if (BytesRemoved)
639     *BytesRemoved += getInstSizeInBytes(*I);
640   I->eraseFromParent();
641   return 2;
642 }
643 
644 // Inserts a branch into the end of the specific MachineBasicBlock, returning
645 // the number of instructions inserted.
646 unsigned RISCVInstrInfo::insertBranch(
647     MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
648     ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
649   if (BytesAdded)
650     *BytesAdded = 0;
651 
652   // Shouldn't be a fall through.
653   assert(TBB && "insertBranch must not be told to insert a fallthrough");
654   assert((Cond.size() == 3 || Cond.size() == 0) &&
655          "RISCV branch conditions have two components!");
656 
657   // Unconditional branch.
658   if (Cond.empty()) {
659     MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(TBB);
660     if (BytesAdded)
661       *BytesAdded += getInstSizeInBytes(MI);
662     return 1;
663   }
664 
665   // Either a one or two-way conditional branch.
666   auto CC = static_cast<RISCVCC::CondCode>(Cond[0].getImm());
667   MachineInstr &CondMI =
668       *BuildMI(&MBB, DL, getBrCond(CC)).add(Cond[1]).add(Cond[2]).addMBB(TBB);
669   if (BytesAdded)
670     *BytesAdded += getInstSizeInBytes(CondMI);
671 
672   // One-way conditional branch.
673   if (!FBB)
674     return 1;
675 
676   // Two-way conditional branch.
677   MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(FBB);
678   if (BytesAdded)
679     *BytesAdded += getInstSizeInBytes(MI);
680   return 2;
681 }
682 
683 unsigned RISCVInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
684                                               MachineBasicBlock &DestBB,
685                                               const DebugLoc &DL,
686                                               int64_t BrOffset,
687                                               RegScavenger *RS) const {
688   assert(RS && "RegScavenger required for long branching");
689   assert(MBB.empty() &&
690          "new block should be inserted for expanding unconditional branch");
691   assert(MBB.pred_size() == 1);
692 
693   MachineFunction *MF = MBB.getParent();
694   MachineRegisterInfo &MRI = MF->getRegInfo();
695 
696   if (!isInt<32>(BrOffset))
697     report_fatal_error(
698         "Branch offsets outside of the signed 32-bit range not supported");
699 
700   // FIXME: A virtual register must be used initially, as the register
701   // scavenger won't work with empty blocks (SIInstrInfo::insertIndirectBranch
702   // uses the same workaround).
703   Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
704   auto II = MBB.end();
705 
706   MachineInstr &MI = *BuildMI(MBB, II, DL, get(RISCV::PseudoJump))
707                           .addReg(ScratchReg, RegState::Define | RegState::Dead)
708                           .addMBB(&DestBB, RISCVII::MO_CALL);
709 
710   RS->enterBasicBlockEnd(MBB);
711   unsigned Scav = RS->scavengeRegisterBackwards(RISCV::GPRRegClass,
712                                                 MI.getIterator(), false, 0);
713   MRI.replaceRegWith(ScratchReg, Scav);
714   MRI.clearVirtRegs();
715   RS->setRegUsed(Scav);
716   return 8;
717 }
718 
719 bool RISCVInstrInfo::reverseBranchCondition(
720     SmallVectorImpl<MachineOperand> &Cond) const {
721   assert((Cond.size() == 3) && "Invalid branch condition!");
722   auto CC = static_cast<RISCVCC::CondCode>(Cond[0].getImm());
723   Cond[0].setImm(getOppositeBranchCondition(CC));
724   return false;
725 }
726 
727 MachineBasicBlock *
728 RISCVInstrInfo::getBranchDestBlock(const MachineInstr &MI) const {
729   assert(MI.getDesc().isBranch() && "Unexpected opcode!");
730   // The branch target is always the last operand.
731   int NumOp = MI.getNumExplicitOperands();
732   return MI.getOperand(NumOp - 1).getMBB();
733 }
734 
735 bool RISCVInstrInfo::isBranchOffsetInRange(unsigned BranchOp,
736                                            int64_t BrOffset) const {
737   unsigned XLen = STI.getXLen();
738   // Ideally we could determine the supported branch offset from the
739   // RISCVII::FormMask, but this can't be used for Pseudo instructions like
740   // PseudoBR.
741   switch (BranchOp) {
742   default:
743     llvm_unreachable("Unexpected opcode!");
744   case RISCV::BEQ:
745   case RISCV::BNE:
746   case RISCV::BLT:
747   case RISCV::BGE:
748   case RISCV::BLTU:
749   case RISCV::BGEU:
750     return isIntN(13, BrOffset);
751   case RISCV::JAL:
752   case RISCV::PseudoBR:
753     return isIntN(21, BrOffset);
754   case RISCV::PseudoJump:
755     return isIntN(32, SignExtend64(BrOffset + 0x800, XLen));
756   }
757 }
758 
759 unsigned RISCVInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
760   unsigned Opcode = MI.getOpcode();
761 
762   switch (Opcode) {
763   default: {
764     if (MI.getParent() && MI.getParent()->getParent()) {
765       const auto MF = MI.getMF();
766       const auto &TM = static_cast<const RISCVTargetMachine &>(MF->getTarget());
767       const MCRegisterInfo &MRI = *TM.getMCRegisterInfo();
768       const MCSubtargetInfo &STI = *TM.getMCSubtargetInfo();
769       const RISCVSubtarget &ST = MF->getSubtarget<RISCVSubtarget>();
770       if (isCompressibleInst(MI, &ST, MRI, STI))
771         return 2;
772     }
773     return get(Opcode).getSize();
774   }
775   case TargetOpcode::EH_LABEL:
776   case TargetOpcode::IMPLICIT_DEF:
777   case TargetOpcode::KILL:
778   case TargetOpcode::DBG_VALUE:
779     return 0;
780   // These values are determined based on RISCVExpandAtomicPseudoInsts,
781   // RISCVExpandPseudoInsts and RISCVMCCodeEmitter, depending on where the
782   // pseudos are expanded.
783   case RISCV::PseudoCALLReg:
784   case RISCV::PseudoCALL:
785   case RISCV::PseudoJump:
786   case RISCV::PseudoTAIL:
787   case RISCV::PseudoLLA:
788   case RISCV::PseudoLA:
789   case RISCV::PseudoLA_TLS_IE:
790   case RISCV::PseudoLA_TLS_GD:
791     return 8;
792   case RISCV::PseudoAtomicLoadNand32:
793   case RISCV::PseudoAtomicLoadNand64:
794     return 20;
795   case RISCV::PseudoMaskedAtomicSwap32:
796   case RISCV::PseudoMaskedAtomicLoadAdd32:
797   case RISCV::PseudoMaskedAtomicLoadSub32:
798     return 28;
799   case RISCV::PseudoMaskedAtomicLoadNand32:
800     return 32;
801   case RISCV::PseudoMaskedAtomicLoadMax32:
802   case RISCV::PseudoMaskedAtomicLoadMin32:
803     return 44;
804   case RISCV::PseudoMaskedAtomicLoadUMax32:
805   case RISCV::PseudoMaskedAtomicLoadUMin32:
806     return 36;
807   case RISCV::PseudoCmpXchg32:
808   case RISCV::PseudoCmpXchg64:
809     return 16;
810   case RISCV::PseudoMaskedCmpXchg32:
811     return 32;
812   case TargetOpcode::INLINEASM:
813   case TargetOpcode::INLINEASM_BR: {
814     const MachineFunction &MF = *MI.getParent()->getParent();
815     const auto &TM = static_cast<const RISCVTargetMachine &>(MF.getTarget());
816     return getInlineAsmLength(MI.getOperand(0).getSymbolName(),
817                               *TM.getMCAsmInfo());
818   }
819   case RISCV::PseudoVSPILL2_M1:
820   case RISCV::PseudoVSPILL2_M2:
821   case RISCV::PseudoVSPILL2_M4:
822   case RISCV::PseudoVSPILL3_M1:
823   case RISCV::PseudoVSPILL3_M2:
824   case RISCV::PseudoVSPILL4_M1:
825   case RISCV::PseudoVSPILL4_M2:
826   case RISCV::PseudoVSPILL5_M1:
827   case RISCV::PseudoVSPILL6_M1:
828   case RISCV::PseudoVSPILL7_M1:
829   case RISCV::PseudoVSPILL8_M1:
830   case RISCV::PseudoVRELOAD2_M1:
831   case RISCV::PseudoVRELOAD2_M2:
832   case RISCV::PseudoVRELOAD2_M4:
833   case RISCV::PseudoVRELOAD3_M1:
834   case RISCV::PseudoVRELOAD3_M2:
835   case RISCV::PseudoVRELOAD4_M1:
836   case RISCV::PseudoVRELOAD4_M2:
837   case RISCV::PseudoVRELOAD5_M1:
838   case RISCV::PseudoVRELOAD6_M1:
839   case RISCV::PseudoVRELOAD7_M1:
840   case RISCV::PseudoVRELOAD8_M1: {
841     // The values are determined based on expandVSPILL and expandVRELOAD that
842     // expand the pseudos depending on NF.
843     unsigned NF = isRVVSpillForZvlsseg(Opcode)->first;
844     return 4 * (2 * NF - 1);
845   }
846   }
847 }
848 
849 bool RISCVInstrInfo::isAsCheapAsAMove(const MachineInstr &MI) const {
850   const unsigned Opcode = MI.getOpcode();
851   switch (Opcode) {
852   default:
853     break;
854   case RISCV::FSGNJ_D:
855   case RISCV::FSGNJ_S:
856     // The canonical floating-point move is fsgnj rd, rs, rs.
857     return MI.getOperand(1).isReg() && MI.getOperand(2).isReg() &&
858            MI.getOperand(1).getReg() == MI.getOperand(2).getReg();
859   case RISCV::ADDI:
860   case RISCV::ORI:
861   case RISCV::XORI:
862     return (MI.getOperand(1).isReg() &&
863             MI.getOperand(1).getReg() == RISCV::X0) ||
864            (MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0);
865   }
866   return MI.isAsCheapAsAMove();
867 }
868 
869 Optional<DestSourcePair>
870 RISCVInstrInfo::isCopyInstrImpl(const MachineInstr &MI) const {
871   if (MI.isMoveReg())
872     return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
873   switch (MI.getOpcode()) {
874   default:
875     break;
876   case RISCV::ADDI:
877     // Operand 1 can be a frameindex but callers expect registers
878     if (MI.getOperand(1).isReg() && MI.getOperand(2).isImm() &&
879         MI.getOperand(2).getImm() == 0)
880       return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
881     break;
882   case RISCV::FSGNJ_D:
883   case RISCV::FSGNJ_S:
884     // The canonical floating-point move is fsgnj rd, rs, rs.
885     if (MI.getOperand(1).isReg() && MI.getOperand(2).isReg() &&
886         MI.getOperand(1).getReg() == MI.getOperand(2).getReg())
887       return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
888     break;
889   }
890   return None;
891 }
892 
893 bool RISCVInstrInfo::verifyInstruction(const MachineInstr &MI,
894                                        StringRef &ErrInfo) const {
895   const MCInstrInfo *MCII = STI.getInstrInfo();
896   MCInstrDesc const &Desc = MCII->get(MI.getOpcode());
897 
898   for (auto &OI : enumerate(Desc.operands())) {
899     unsigned OpType = OI.value().OperandType;
900     if (OpType >= RISCVOp::OPERAND_FIRST_RISCV_IMM &&
901         OpType <= RISCVOp::OPERAND_LAST_RISCV_IMM) {
902       const MachineOperand &MO = MI.getOperand(OI.index());
903       if (MO.isImm()) {
904         int64_t Imm = MO.getImm();
905         bool Ok;
906         switch (OpType) {
907         default:
908           llvm_unreachable("Unexpected operand type");
909         case RISCVOp::OPERAND_UIMM2:
910           Ok = isUInt<2>(Imm);
911           break;
912         case RISCVOp::OPERAND_UIMM3:
913           Ok = isUInt<3>(Imm);
914           break;
915         case RISCVOp::OPERAND_UIMM4:
916           Ok = isUInt<4>(Imm);
917           break;
918         case RISCVOp::OPERAND_UIMM5:
919           Ok = isUInt<5>(Imm);
920           break;
921         case RISCVOp::OPERAND_UIMM7:
922           Ok = isUInt<7>(Imm);
923           break;
924         case RISCVOp::OPERAND_UIMM12:
925           Ok = isUInt<12>(Imm);
926           break;
927         case RISCVOp::OPERAND_SIMM12:
928           Ok = isInt<12>(Imm);
929           break;
930         case RISCVOp::OPERAND_UIMM20:
931           Ok = isUInt<20>(Imm);
932           break;
933         case RISCVOp::OPERAND_UIMMLOG2XLEN:
934           if (STI.getTargetTriple().isArch64Bit())
935             Ok = isUInt<6>(Imm);
936           else
937             Ok = isUInt<5>(Imm);
938           break;
939         }
940         if (!Ok) {
941           ErrInfo = "Invalid immediate";
942           return false;
943         }
944       }
945     }
946   }
947 
948   return true;
949 }
950 
951 // Return true if get the base operand, byte offset of an instruction and the
952 // memory width. Width is the size of memory that is being loaded/stored.
953 bool RISCVInstrInfo::getMemOperandWithOffsetWidth(
954     const MachineInstr &LdSt, const MachineOperand *&BaseReg, int64_t &Offset,
955     unsigned &Width, const TargetRegisterInfo *TRI) const {
956   if (!LdSt.mayLoadOrStore())
957     return false;
958 
959   // Here we assume the standard RISC-V ISA, which uses a base+offset
960   // addressing mode. You'll need to relax these conditions to support custom
961   // load/stores instructions.
962   if (LdSt.getNumExplicitOperands() != 3)
963     return false;
964   if (!LdSt.getOperand(1).isReg() || !LdSt.getOperand(2).isImm())
965     return false;
966 
967   if (!LdSt.hasOneMemOperand())
968     return false;
969 
970   Width = (*LdSt.memoperands_begin())->getSize();
971   BaseReg = &LdSt.getOperand(1);
972   Offset = LdSt.getOperand(2).getImm();
973   return true;
974 }
975 
976 bool RISCVInstrInfo::areMemAccessesTriviallyDisjoint(
977     const MachineInstr &MIa, const MachineInstr &MIb) const {
978   assert(MIa.mayLoadOrStore() && "MIa must be a load or store.");
979   assert(MIb.mayLoadOrStore() && "MIb must be a load or store.");
980 
981   if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
982       MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
983     return false;
984 
985   // Retrieve the base register, offset from the base register and width. Width
986   // is the size of memory that is being loaded/stored (e.g. 1, 2, 4).  If
987   // base registers are identical, and the offset of a lower memory access +
988   // the width doesn't overlap the offset of a higher memory access,
989   // then the memory accesses are different.
990   const TargetRegisterInfo *TRI = STI.getRegisterInfo();
991   const MachineOperand *BaseOpA = nullptr, *BaseOpB = nullptr;
992   int64_t OffsetA = 0, OffsetB = 0;
993   unsigned int WidthA = 0, WidthB = 0;
994   if (getMemOperandWithOffsetWidth(MIa, BaseOpA, OffsetA, WidthA, TRI) &&
995       getMemOperandWithOffsetWidth(MIb, BaseOpB, OffsetB, WidthB, TRI)) {
996     if (BaseOpA->isIdenticalTo(*BaseOpB)) {
997       int LowOffset = std::min(OffsetA, OffsetB);
998       int HighOffset = std::max(OffsetA, OffsetB);
999       int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
1000       if (LowOffset + LowWidth <= HighOffset)
1001         return true;
1002     }
1003   }
1004   return false;
1005 }
1006 
1007 std::pair<unsigned, unsigned>
1008 RISCVInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1009   const unsigned Mask = RISCVII::MO_DIRECT_FLAG_MASK;
1010   return std::make_pair(TF & Mask, TF & ~Mask);
1011 }
1012 
1013 ArrayRef<std::pair<unsigned, const char *>>
1014 RISCVInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1015   using namespace RISCVII;
1016   static const std::pair<unsigned, const char *> TargetFlags[] = {
1017       {MO_CALL, "riscv-call"},
1018       {MO_PLT, "riscv-plt"},
1019       {MO_LO, "riscv-lo"},
1020       {MO_HI, "riscv-hi"},
1021       {MO_PCREL_LO, "riscv-pcrel-lo"},
1022       {MO_PCREL_HI, "riscv-pcrel-hi"},
1023       {MO_GOT_HI, "riscv-got-hi"},
1024       {MO_TPREL_LO, "riscv-tprel-lo"},
1025       {MO_TPREL_HI, "riscv-tprel-hi"},
1026       {MO_TPREL_ADD, "riscv-tprel-add"},
1027       {MO_TLS_GOT_HI, "riscv-tls-got-hi"},
1028       {MO_TLS_GD_HI, "riscv-tls-gd-hi"}};
1029   return makeArrayRef(TargetFlags);
1030 }
1031 bool RISCVInstrInfo::isFunctionSafeToOutlineFrom(
1032     MachineFunction &MF, bool OutlineFromLinkOnceODRs) const {
1033   const Function &F = MF.getFunction();
1034 
1035   // Can F be deduplicated by the linker? If it can, don't outline from it.
1036   if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage())
1037     return false;
1038 
1039   // Don't outline from functions with section markings; the program could
1040   // expect that all the code is in the named section.
1041   if (F.hasSection())
1042     return false;
1043 
1044   // It's safe to outline from MF.
1045   return true;
1046 }
1047 
1048 bool RISCVInstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
1049                                             unsigned &Flags) const {
1050   // More accurate safety checking is done in getOutliningCandidateInfo.
1051   return true;
1052 }
1053 
1054 // Enum values indicating how an outlined call should be constructed.
1055 enum MachineOutlinerConstructionID {
1056   MachineOutlinerDefault
1057 };
1058 
1059 outliner::OutlinedFunction RISCVInstrInfo::getOutliningCandidateInfo(
1060     std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
1061 
1062   // First we need to filter out candidates where the X5 register (IE t0) can't
1063   // be used to setup the function call.
1064   auto CannotInsertCall = [](outliner::Candidate &C) {
1065     const TargetRegisterInfo *TRI = C.getMF()->getSubtarget().getRegisterInfo();
1066 
1067     C.initLRU(*TRI);
1068     LiveRegUnits LRU = C.LRU;
1069     return !LRU.available(RISCV::X5);
1070   };
1071 
1072   llvm::erase_if(RepeatedSequenceLocs, CannotInsertCall);
1073 
1074   // If the sequence doesn't have enough candidates left, then we're done.
1075   if (RepeatedSequenceLocs.size() < 2)
1076     return outliner::OutlinedFunction();
1077 
1078   unsigned SequenceSize = 0;
1079 
1080   auto I = RepeatedSequenceLocs[0].front();
1081   auto E = std::next(RepeatedSequenceLocs[0].back());
1082   for (; I != E; ++I)
1083     SequenceSize += getInstSizeInBytes(*I);
1084 
1085   // call t0, function = 8 bytes.
1086   unsigned CallOverhead = 8;
1087   for (auto &C : RepeatedSequenceLocs)
1088     C.setCallInfo(MachineOutlinerDefault, CallOverhead);
1089 
1090   // jr t0 = 4 bytes, 2 bytes if compressed instructions are enabled.
1091   unsigned FrameOverhead = 4;
1092   if (RepeatedSequenceLocs[0].getMF()->getSubtarget()
1093           .getFeatureBits()[RISCV::FeatureStdExtC])
1094     FrameOverhead = 2;
1095 
1096   return outliner::OutlinedFunction(RepeatedSequenceLocs, SequenceSize,
1097                                     FrameOverhead, MachineOutlinerDefault);
1098 }
1099 
1100 outliner::InstrType
1101 RISCVInstrInfo::getOutliningType(MachineBasicBlock::iterator &MBBI,
1102                                  unsigned Flags) const {
1103   MachineInstr &MI = *MBBI;
1104   MachineBasicBlock *MBB = MI.getParent();
1105   const TargetRegisterInfo *TRI =
1106       MBB->getParent()->getSubtarget().getRegisterInfo();
1107 
1108   // Positions generally can't safely be outlined.
1109   if (MI.isPosition()) {
1110     // We can manually strip out CFI instructions later.
1111     if (MI.isCFIInstruction())
1112       return outliner::InstrType::Invisible;
1113 
1114     return outliner::InstrType::Illegal;
1115   }
1116 
1117   // Don't trust the user to write safe inline assembly.
1118   if (MI.isInlineAsm())
1119     return outliner::InstrType::Illegal;
1120 
1121   // We can't outline branches to other basic blocks.
1122   if (MI.isTerminator() && !MBB->succ_empty())
1123     return outliner::InstrType::Illegal;
1124 
1125   // We need support for tail calls to outlined functions before return
1126   // statements can be allowed.
1127   if (MI.isReturn())
1128     return outliner::InstrType::Illegal;
1129 
1130   // Don't allow modifying the X5 register which we use for return addresses for
1131   // these outlined functions.
1132   if (MI.modifiesRegister(RISCV::X5, TRI) ||
1133       MI.getDesc().hasImplicitDefOfPhysReg(RISCV::X5))
1134     return outliner::InstrType::Illegal;
1135 
1136   // Make sure the operands don't reference something unsafe.
1137   for (const auto &MO : MI.operands())
1138     if (MO.isMBB() || MO.isBlockAddress() || MO.isCPI() || MO.isJTI())
1139       return outliner::InstrType::Illegal;
1140 
1141   // Don't allow instructions which won't be materialized to impact outlining
1142   // analysis.
1143   if (MI.isMetaInstruction())
1144     return outliner::InstrType::Invisible;
1145 
1146   return outliner::InstrType::Legal;
1147 }
1148 
1149 void RISCVInstrInfo::buildOutlinedFrame(
1150     MachineBasicBlock &MBB, MachineFunction &MF,
1151     const outliner::OutlinedFunction &OF) const {
1152 
1153   // Strip out any CFI instructions
1154   bool Changed = true;
1155   while (Changed) {
1156     Changed = false;
1157     auto I = MBB.begin();
1158     auto E = MBB.end();
1159     for (; I != E; ++I) {
1160       if (I->isCFIInstruction()) {
1161         I->removeFromParent();
1162         Changed = true;
1163         break;
1164       }
1165     }
1166   }
1167 
1168   MBB.addLiveIn(RISCV::X5);
1169 
1170   // Add in a return instruction to the end of the outlined frame.
1171   MBB.insert(MBB.end(), BuildMI(MF, DebugLoc(), get(RISCV::JALR))
1172       .addReg(RISCV::X0, RegState::Define)
1173       .addReg(RISCV::X5)
1174       .addImm(0));
1175 }
1176 
1177 MachineBasicBlock::iterator RISCVInstrInfo::insertOutlinedCall(
1178     Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It,
1179     MachineFunction &MF, const outliner::Candidate &C) const {
1180 
1181   // Add in a call instruction to the outlined function at the given location.
1182   It = MBB.insert(It,
1183                   BuildMI(MF, DebugLoc(), get(RISCV::PseudoCALLReg), RISCV::X5)
1184                       .addGlobalAddress(M.getNamedValue(MF.getName()), 0,
1185                                         RISCVII::MO_CALL));
1186   return It;
1187 }
1188 
1189 // clang-format off
1190 #define CASE_VFMA_OPCODE_COMMON(OP, TYPE, LMUL)                                \
1191   RISCV::PseudoV##OP##_##TYPE##_##LMUL
1192 
1193 #define CASE_VFMA_OPCODE_LMULS(OP, TYPE)                                       \
1194   CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF8):                                      \
1195   case CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF4):                                 \
1196   case CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF2):                                 \
1197   case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M1):                                  \
1198   case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M2):                                  \
1199   case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M4):                                  \
1200   case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M8)
1201 
1202 #define CASE_VFMA_SPLATS(OP)                                                   \
1203   CASE_VFMA_OPCODE_LMULS(OP, VF16):                                            \
1204   case CASE_VFMA_OPCODE_LMULS(OP, VF32):                                       \
1205   case CASE_VFMA_OPCODE_LMULS(OP, VF64)
1206 // clang-format on
1207 
1208 bool RISCVInstrInfo::findCommutedOpIndices(const MachineInstr &MI,
1209                                            unsigned &SrcOpIdx1,
1210                                            unsigned &SrcOpIdx2) const {
1211   const MCInstrDesc &Desc = MI.getDesc();
1212   if (!Desc.isCommutable())
1213     return false;
1214 
1215   switch (MI.getOpcode()) {
1216   case CASE_VFMA_SPLATS(FMADD):
1217   case CASE_VFMA_SPLATS(FMSUB):
1218   case CASE_VFMA_SPLATS(FMACC):
1219   case CASE_VFMA_SPLATS(FMSAC):
1220   case CASE_VFMA_SPLATS(FNMADD):
1221   case CASE_VFMA_SPLATS(FNMSUB):
1222   case CASE_VFMA_SPLATS(FNMACC):
1223   case CASE_VFMA_SPLATS(FNMSAC):
1224   case CASE_VFMA_OPCODE_LMULS(FMACC, VV):
1225   case CASE_VFMA_OPCODE_LMULS(FMSAC, VV):
1226   case CASE_VFMA_OPCODE_LMULS(FNMACC, VV):
1227   case CASE_VFMA_OPCODE_LMULS(FNMSAC, VV):
1228   case CASE_VFMA_OPCODE_LMULS(MADD, VX):
1229   case CASE_VFMA_OPCODE_LMULS(NMSUB, VX):
1230   case CASE_VFMA_OPCODE_LMULS(MACC, VX):
1231   case CASE_VFMA_OPCODE_LMULS(NMSAC, VX):
1232   case CASE_VFMA_OPCODE_LMULS(MACC, VV):
1233   case CASE_VFMA_OPCODE_LMULS(NMSAC, VV): {
1234     // If the tail policy is undisturbed we can't commute.
1235     assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags));
1236     if ((MI.getOperand(MI.getNumExplicitOperands() - 1).getImm() & 1) == 0)
1237       return false;
1238 
1239     // For these instructions we can only swap operand 1 and operand 3 by
1240     // changing the opcode.
1241     unsigned CommutableOpIdx1 = 1;
1242     unsigned CommutableOpIdx2 = 3;
1243     if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1,
1244                               CommutableOpIdx2))
1245       return false;
1246     return true;
1247   }
1248   case CASE_VFMA_OPCODE_LMULS(FMADD, VV):
1249   case CASE_VFMA_OPCODE_LMULS(FMSUB, VV):
1250   case CASE_VFMA_OPCODE_LMULS(FNMADD, VV):
1251   case CASE_VFMA_OPCODE_LMULS(FNMSUB, VV):
1252   case CASE_VFMA_OPCODE_LMULS(MADD, VV):
1253   case CASE_VFMA_OPCODE_LMULS(NMSUB, VV): {
1254     // If the tail policy is undisturbed we can't commute.
1255     assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags));
1256     if ((MI.getOperand(MI.getNumExplicitOperands() - 1).getImm() & 1) == 0)
1257       return false;
1258 
1259     // For these instructions we have more freedom. We can commute with the
1260     // other multiplicand or with the addend/subtrahend/minuend.
1261 
1262     // Any fixed operand must be from source 1, 2 or 3.
1263     if (SrcOpIdx1 != CommuteAnyOperandIndex && SrcOpIdx1 > 3)
1264       return false;
1265     if (SrcOpIdx2 != CommuteAnyOperandIndex && SrcOpIdx2 > 3)
1266       return false;
1267 
1268     // It both ops are fixed one must be the tied source.
1269     if (SrcOpIdx1 != CommuteAnyOperandIndex &&
1270         SrcOpIdx2 != CommuteAnyOperandIndex && SrcOpIdx1 != 1 && SrcOpIdx2 != 1)
1271       return false;
1272 
1273     // Look for two different register operands assumed to be commutable
1274     // regardless of the FMA opcode. The FMA opcode is adjusted later if
1275     // needed.
1276     if (SrcOpIdx1 == CommuteAnyOperandIndex ||
1277         SrcOpIdx2 == CommuteAnyOperandIndex) {
1278       // At least one of operands to be commuted is not specified and
1279       // this method is free to choose appropriate commutable operands.
1280       unsigned CommutableOpIdx1 = SrcOpIdx1;
1281       if (SrcOpIdx1 == SrcOpIdx2) {
1282         // Both of operands are not fixed. Set one of commutable
1283         // operands to the tied source.
1284         CommutableOpIdx1 = 1;
1285       } else if (SrcOpIdx1 == CommuteAnyOperandIndex) {
1286         // Only one of the operands is not fixed.
1287         CommutableOpIdx1 = SrcOpIdx2;
1288       }
1289 
1290       // CommutableOpIdx1 is well defined now. Let's choose another commutable
1291       // operand and assign its index to CommutableOpIdx2.
1292       unsigned CommutableOpIdx2;
1293       if (CommutableOpIdx1 != 1) {
1294         // If we haven't already used the tied source, we must use it now.
1295         CommutableOpIdx2 = 1;
1296       } else {
1297         Register Op1Reg = MI.getOperand(CommutableOpIdx1).getReg();
1298 
1299         // The commuted operands should have different registers.
1300         // Otherwise, the commute transformation does not change anything and
1301         // is useless. We use this as a hint to make our decision.
1302         if (Op1Reg != MI.getOperand(2).getReg())
1303           CommutableOpIdx2 = 2;
1304         else
1305           CommutableOpIdx2 = 3;
1306       }
1307 
1308       // Assign the found pair of commutable indices to SrcOpIdx1 and
1309       // SrcOpIdx2 to return those values.
1310       if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1,
1311                                 CommutableOpIdx2))
1312         return false;
1313     }
1314 
1315     return true;
1316   }
1317   }
1318 
1319   return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
1320 }
1321 
1322 #define CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, LMUL)               \
1323   case RISCV::PseudoV##OLDOP##_##TYPE##_##LMUL:                                \
1324     Opc = RISCV::PseudoV##NEWOP##_##TYPE##_##LMUL;                             \
1325     break;
1326 
1327 #define CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, TYPE)                      \
1328   CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF8)                      \
1329   CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF4)                      \
1330   CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF2)                      \
1331   CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M1)                       \
1332   CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M2)                       \
1333   CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M4)                       \
1334   CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M8)
1335 
1336 #define CASE_VFMA_CHANGE_OPCODE_SPLATS(OLDOP, NEWOP)                           \
1337   CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, VF16)                            \
1338   CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, VF32)                            \
1339   CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, VF64)
1340 
1341 MachineInstr *RISCVInstrInfo::commuteInstructionImpl(MachineInstr &MI,
1342                                                      bool NewMI,
1343                                                      unsigned OpIdx1,
1344                                                      unsigned OpIdx2) const {
1345   auto cloneIfNew = [NewMI](MachineInstr &MI) -> MachineInstr & {
1346     if (NewMI)
1347       return *MI.getParent()->getParent()->CloneMachineInstr(&MI);
1348     return MI;
1349   };
1350 
1351   switch (MI.getOpcode()) {
1352   case CASE_VFMA_SPLATS(FMACC):
1353   case CASE_VFMA_SPLATS(FMADD):
1354   case CASE_VFMA_SPLATS(FMSAC):
1355   case CASE_VFMA_SPLATS(FMSUB):
1356   case CASE_VFMA_SPLATS(FNMACC):
1357   case CASE_VFMA_SPLATS(FNMADD):
1358   case CASE_VFMA_SPLATS(FNMSAC):
1359   case CASE_VFMA_SPLATS(FNMSUB):
1360   case CASE_VFMA_OPCODE_LMULS(FMACC, VV):
1361   case CASE_VFMA_OPCODE_LMULS(FMSAC, VV):
1362   case CASE_VFMA_OPCODE_LMULS(FNMACC, VV):
1363   case CASE_VFMA_OPCODE_LMULS(FNMSAC, VV):
1364   case CASE_VFMA_OPCODE_LMULS(MADD, VX):
1365   case CASE_VFMA_OPCODE_LMULS(NMSUB, VX):
1366   case CASE_VFMA_OPCODE_LMULS(MACC, VX):
1367   case CASE_VFMA_OPCODE_LMULS(NMSAC, VX):
1368   case CASE_VFMA_OPCODE_LMULS(MACC, VV):
1369   case CASE_VFMA_OPCODE_LMULS(NMSAC, VV): {
1370     // It only make sense to toggle these between clobbering the
1371     // addend/subtrahend/minuend one of the multiplicands.
1372     assert((OpIdx1 == 1 || OpIdx2 == 1) && "Unexpected opcode index");
1373     assert((OpIdx1 == 3 || OpIdx2 == 3) && "Unexpected opcode index");
1374     unsigned Opc;
1375     switch (MI.getOpcode()) {
1376       default:
1377         llvm_unreachable("Unexpected opcode");
1378       CASE_VFMA_CHANGE_OPCODE_SPLATS(FMACC, FMADD)
1379       CASE_VFMA_CHANGE_OPCODE_SPLATS(FMADD, FMACC)
1380       CASE_VFMA_CHANGE_OPCODE_SPLATS(FMSAC, FMSUB)
1381       CASE_VFMA_CHANGE_OPCODE_SPLATS(FMSUB, FMSAC)
1382       CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMACC, FNMADD)
1383       CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMADD, FNMACC)
1384       CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMSAC, FNMSUB)
1385       CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMSUB, FNMSAC)
1386       CASE_VFMA_CHANGE_OPCODE_LMULS(FMACC, FMADD, VV)
1387       CASE_VFMA_CHANGE_OPCODE_LMULS(FMSAC, FMSUB, VV)
1388       CASE_VFMA_CHANGE_OPCODE_LMULS(FNMACC, FNMADD, VV)
1389       CASE_VFMA_CHANGE_OPCODE_LMULS(FNMSAC, FNMSUB, VV)
1390       CASE_VFMA_CHANGE_OPCODE_LMULS(MACC, MADD, VX)
1391       CASE_VFMA_CHANGE_OPCODE_LMULS(MADD, MACC, VX)
1392       CASE_VFMA_CHANGE_OPCODE_LMULS(NMSAC, NMSUB, VX)
1393       CASE_VFMA_CHANGE_OPCODE_LMULS(NMSUB, NMSAC, VX)
1394       CASE_VFMA_CHANGE_OPCODE_LMULS(MACC, MADD, VV)
1395       CASE_VFMA_CHANGE_OPCODE_LMULS(NMSAC, NMSUB, VV)
1396     }
1397 
1398     auto &WorkingMI = cloneIfNew(MI);
1399     WorkingMI.setDesc(get(Opc));
1400     return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false,
1401                                                    OpIdx1, OpIdx2);
1402   }
1403   case CASE_VFMA_OPCODE_LMULS(FMADD, VV):
1404   case CASE_VFMA_OPCODE_LMULS(FMSUB, VV):
1405   case CASE_VFMA_OPCODE_LMULS(FNMADD, VV):
1406   case CASE_VFMA_OPCODE_LMULS(FNMSUB, VV):
1407   case CASE_VFMA_OPCODE_LMULS(MADD, VV):
1408   case CASE_VFMA_OPCODE_LMULS(NMSUB, VV): {
1409     assert((OpIdx1 == 1 || OpIdx2 == 1) && "Unexpected opcode index");
1410     // If one of the operands, is the addend we need to change opcode.
1411     // Otherwise we're just swapping 2 of the multiplicands.
1412     if (OpIdx1 == 3 || OpIdx2 == 3) {
1413       unsigned Opc;
1414       switch (MI.getOpcode()) {
1415         default:
1416           llvm_unreachable("Unexpected opcode");
1417         CASE_VFMA_CHANGE_OPCODE_LMULS(FMADD, FMACC, VV)
1418         CASE_VFMA_CHANGE_OPCODE_LMULS(FMSUB, FMSAC, VV)
1419         CASE_VFMA_CHANGE_OPCODE_LMULS(FNMADD, FNMACC, VV)
1420         CASE_VFMA_CHANGE_OPCODE_LMULS(FNMSUB, FNMSAC, VV)
1421         CASE_VFMA_CHANGE_OPCODE_LMULS(MADD, MACC, VV)
1422         CASE_VFMA_CHANGE_OPCODE_LMULS(NMSUB, NMSAC, VV)
1423       }
1424 
1425       auto &WorkingMI = cloneIfNew(MI);
1426       WorkingMI.setDesc(get(Opc));
1427       return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false,
1428                                                      OpIdx1, OpIdx2);
1429     }
1430     // Let the default code handle it.
1431     break;
1432   }
1433   }
1434 
1435   return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
1436 }
1437 
1438 #undef CASE_VFMA_CHANGE_OPCODE_SPLATS
1439 #undef CASE_VFMA_CHANGE_OPCODE_LMULS
1440 #undef CASE_VFMA_CHANGE_OPCODE_COMMON
1441 #undef CASE_VFMA_SPLATS
1442 #undef CASE_VFMA_OPCODE_LMULS
1443 #undef CASE_VFMA_OPCODE_COMMON
1444 
1445 // clang-format off
1446 #define CASE_WIDEOP_OPCODE_COMMON(OP, LMUL)                                    \
1447   RISCV::PseudoV##OP##_##LMUL##_TIED
1448 
1449 #define CASE_WIDEOP_OPCODE_LMULS(OP)                                           \
1450   CASE_WIDEOP_OPCODE_COMMON(OP, MF8):                                          \
1451   case CASE_WIDEOP_OPCODE_COMMON(OP, MF4):                                     \
1452   case CASE_WIDEOP_OPCODE_COMMON(OP, MF2):                                     \
1453   case CASE_WIDEOP_OPCODE_COMMON(OP, M1):                                      \
1454   case CASE_WIDEOP_OPCODE_COMMON(OP, M2):                                      \
1455   case CASE_WIDEOP_OPCODE_COMMON(OP, M4)
1456 // clang-format on
1457 
1458 #define CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, LMUL)                             \
1459   case RISCV::PseudoV##OP##_##LMUL##_TIED:                                     \
1460     NewOpc = RISCV::PseudoV##OP##_##LMUL;                                      \
1461     break;
1462 
1463 #define CASE_WIDEOP_CHANGE_OPCODE_LMULS(OP)                                    \
1464   CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF8)                                    \
1465   CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF4)                                    \
1466   CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF2)                                    \
1467   CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M1)                                     \
1468   CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M2)                                     \
1469   CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M4)
1470 
1471 MachineInstr *RISCVInstrInfo::convertToThreeAddress(MachineInstr &MI,
1472                                                     LiveVariables *LV) const {
1473   switch (MI.getOpcode()) {
1474   default:
1475     break;
1476   case CASE_WIDEOP_OPCODE_LMULS(FWADD_WV):
1477   case CASE_WIDEOP_OPCODE_LMULS(FWSUB_WV):
1478   case CASE_WIDEOP_OPCODE_LMULS(WADD_WV):
1479   case CASE_WIDEOP_OPCODE_LMULS(WADDU_WV):
1480   case CASE_WIDEOP_OPCODE_LMULS(WSUB_WV):
1481   case CASE_WIDEOP_OPCODE_LMULS(WSUBU_WV): {
1482     // clang-format off
1483     unsigned NewOpc;
1484     switch (MI.getOpcode()) {
1485     default:
1486       llvm_unreachable("Unexpected opcode");
1487     CASE_WIDEOP_CHANGE_OPCODE_LMULS(FWADD_WV)
1488     CASE_WIDEOP_CHANGE_OPCODE_LMULS(FWSUB_WV)
1489     CASE_WIDEOP_CHANGE_OPCODE_LMULS(WADD_WV)
1490     CASE_WIDEOP_CHANGE_OPCODE_LMULS(WADDU_WV)
1491     CASE_WIDEOP_CHANGE_OPCODE_LMULS(WSUB_WV)
1492     CASE_WIDEOP_CHANGE_OPCODE_LMULS(WSUBU_WV)
1493     }
1494     //clang-format on
1495 
1496     MachineBasicBlock &MBB = *MI.getParent();
1497     MachineInstrBuilder MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc))
1498                                   .add(MI.getOperand(0))
1499                                   .add(MI.getOperand(1))
1500                                   .add(MI.getOperand(2))
1501                                   .add(MI.getOperand(3))
1502                                   .add(MI.getOperand(4));
1503     MIB.copyImplicitOps(MI);
1504 
1505     if (LV) {
1506       unsigned NumOps = MI.getNumOperands();
1507       for (unsigned I = 1; I < NumOps; ++I) {
1508         MachineOperand &Op = MI.getOperand(I);
1509         if (Op.isReg() && Op.isKill())
1510           LV->replaceKillInstruction(Op.getReg(), MI, *MIB);
1511       }
1512     }
1513 
1514     return MIB;
1515   }
1516   }
1517 
1518   return nullptr;
1519 }
1520 
1521 #undef CASE_WIDEOP_CHANGE_OPCODE_LMULS
1522 #undef CASE_WIDEOP_CHANGE_OPCODE_COMMON
1523 #undef CASE_WIDEOP_OPCODE_LMULS
1524 #undef CASE_WIDEOP_OPCODE_COMMON
1525 
1526 Register RISCVInstrInfo::getVLENFactoredAmount(MachineFunction &MF,
1527                                                MachineBasicBlock &MBB,
1528                                                MachineBasicBlock::iterator II,
1529                                                const DebugLoc &DL,
1530                                                int64_t Amount,
1531                                                MachineInstr::MIFlag Flag) const {
1532   assert(Amount > 0 && "There is no need to get VLEN scaled value.");
1533   assert(Amount % 8 == 0 &&
1534          "Reserve the stack by the multiple of one vector size.");
1535 
1536   MachineRegisterInfo &MRI = MF.getRegInfo();
1537   const RISCVInstrInfo *TII = MF.getSubtarget<RISCVSubtarget>().getInstrInfo();
1538   int64_t NumOfVReg = Amount / 8;
1539 
1540   Register VL = MRI.createVirtualRegister(&RISCV::GPRRegClass);
1541   BuildMI(MBB, II, DL, TII->get(RISCV::PseudoReadVLENB), VL)
1542     .setMIFlag(Flag);
1543   assert(isInt<32>(NumOfVReg) &&
1544          "Expect the number of vector registers within 32-bits.");
1545   if (isPowerOf2_32(NumOfVReg)) {
1546     uint32_t ShiftAmount = Log2_32(NumOfVReg);
1547     if (ShiftAmount == 0)
1548       return VL;
1549     BuildMI(MBB, II, DL, TII->get(RISCV::SLLI), VL)
1550         .addReg(VL, RegState::Kill)
1551         .addImm(ShiftAmount)
1552         .setMIFlag(Flag);
1553   } else if (isPowerOf2_32(NumOfVReg - 1)) {
1554     Register ScaledRegister = MRI.createVirtualRegister(&RISCV::GPRRegClass);
1555     uint32_t ShiftAmount = Log2_32(NumOfVReg - 1);
1556     BuildMI(MBB, II, DL, TII->get(RISCV::SLLI), ScaledRegister)
1557         .addReg(VL)
1558         .addImm(ShiftAmount)
1559         .setMIFlag(Flag);
1560     BuildMI(MBB, II, DL, TII->get(RISCV::ADD), VL)
1561         .addReg(ScaledRegister, RegState::Kill)
1562         .addReg(VL, RegState::Kill)
1563         .setMIFlag(Flag);
1564   } else if (isPowerOf2_32(NumOfVReg + 1)) {
1565     Register ScaledRegister = MRI.createVirtualRegister(&RISCV::GPRRegClass);
1566     uint32_t ShiftAmount = Log2_32(NumOfVReg + 1);
1567     BuildMI(MBB, II, DL, TII->get(RISCV::SLLI), ScaledRegister)
1568         .addReg(VL)
1569         .addImm(ShiftAmount)
1570         .setMIFlag(Flag);
1571     BuildMI(MBB, II, DL, TII->get(RISCV::SUB), VL)
1572         .addReg(ScaledRegister, RegState::Kill)
1573         .addReg(VL, RegState::Kill)
1574         .setMIFlag(Flag);
1575   } else {
1576     Register N = MRI.createVirtualRegister(&RISCV::GPRRegClass);
1577     if (!isInt<12>(NumOfVReg))
1578       movImm(MBB, II, DL, N, NumOfVReg);
1579     else {
1580       BuildMI(MBB, II, DL, TII->get(RISCV::ADDI), N)
1581           .addReg(RISCV::X0)
1582           .addImm(NumOfVReg)
1583           .setMIFlag(Flag);
1584     }
1585     if (!MF.getSubtarget<RISCVSubtarget>().hasStdExtM())
1586       MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
1587           MF.getFunction(),
1588           "M-extension must be enabled to calculate the vscaled size/offset."});
1589     BuildMI(MBB, II, DL, TII->get(RISCV::MUL), VL)
1590         .addReg(VL, RegState::Kill)
1591         .addReg(N, RegState::Kill)
1592         .setMIFlag(Flag);
1593   }
1594 
1595   return VL;
1596 }
1597 
1598 static bool isRVVWholeLoadStore(unsigned Opcode) {
1599   switch (Opcode) {
1600   default:
1601     return false;
1602   case RISCV::VS1R_V:
1603   case RISCV::VS2R_V:
1604   case RISCV::VS4R_V:
1605   case RISCV::VS8R_V:
1606   case RISCV::VL1RE8_V:
1607   case RISCV::VL2RE8_V:
1608   case RISCV::VL4RE8_V:
1609   case RISCV::VL8RE8_V:
1610   case RISCV::VL1RE16_V:
1611   case RISCV::VL2RE16_V:
1612   case RISCV::VL4RE16_V:
1613   case RISCV::VL8RE16_V:
1614   case RISCV::VL1RE32_V:
1615   case RISCV::VL2RE32_V:
1616   case RISCV::VL4RE32_V:
1617   case RISCV::VL8RE32_V:
1618   case RISCV::VL1RE64_V:
1619   case RISCV::VL2RE64_V:
1620   case RISCV::VL4RE64_V:
1621   case RISCV::VL8RE64_V:
1622     return true;
1623   }
1624 }
1625 
1626 bool RISCVInstrInfo::isRVVSpill(const MachineInstr &MI, bool CheckFIs) const {
1627   // RVV lacks any support for immediate addressing for stack addresses, so be
1628   // conservative.
1629   unsigned Opcode = MI.getOpcode();
1630   if (!RISCVVPseudosTable::getPseudoInfo(Opcode) &&
1631       !isRVVWholeLoadStore(Opcode) && !isRVVSpillForZvlsseg(Opcode))
1632     return false;
1633   return !CheckFIs || any_of(MI.operands(), [](const MachineOperand &MO) {
1634     return MO.isFI();
1635   });
1636 }
1637 
1638 Optional<std::pair<unsigned, unsigned>>
1639 RISCVInstrInfo::isRVVSpillForZvlsseg(unsigned Opcode) const {
1640   switch (Opcode) {
1641   default:
1642     return None;
1643   case RISCV::PseudoVSPILL2_M1:
1644   case RISCV::PseudoVRELOAD2_M1:
1645     return std::make_pair(2u, 1u);
1646   case RISCV::PseudoVSPILL2_M2:
1647   case RISCV::PseudoVRELOAD2_M2:
1648     return std::make_pair(2u, 2u);
1649   case RISCV::PseudoVSPILL2_M4:
1650   case RISCV::PseudoVRELOAD2_M4:
1651     return std::make_pair(2u, 4u);
1652   case RISCV::PseudoVSPILL3_M1:
1653   case RISCV::PseudoVRELOAD3_M1:
1654     return std::make_pair(3u, 1u);
1655   case RISCV::PseudoVSPILL3_M2:
1656   case RISCV::PseudoVRELOAD3_M2:
1657     return std::make_pair(3u, 2u);
1658   case RISCV::PseudoVSPILL4_M1:
1659   case RISCV::PseudoVRELOAD4_M1:
1660     return std::make_pair(4u, 1u);
1661   case RISCV::PseudoVSPILL4_M2:
1662   case RISCV::PseudoVRELOAD4_M2:
1663     return std::make_pair(4u, 2u);
1664   case RISCV::PseudoVSPILL5_M1:
1665   case RISCV::PseudoVRELOAD5_M1:
1666     return std::make_pair(5u, 1u);
1667   case RISCV::PseudoVSPILL6_M1:
1668   case RISCV::PseudoVRELOAD6_M1:
1669     return std::make_pair(6u, 1u);
1670   case RISCV::PseudoVSPILL7_M1:
1671   case RISCV::PseudoVRELOAD7_M1:
1672     return std::make_pair(7u, 1u);
1673   case RISCV::PseudoVSPILL8_M1:
1674   case RISCV::PseudoVRELOAD8_M1:
1675     return std::make_pair(8u, 1u);
1676   }
1677 }
1678