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