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