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 MachineInstr *RISCVInstrInfo::foldMemoryOperandImpl(
641     MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
642     MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS,
643     VirtRegMap *VRM) const {
644   const MachineFrameInfo &MFI = MF.getFrameInfo();
645 
646   // The below optimizations narrow the load so they are only valid for little
647   // endian.
648   // TODO: Support big endian by adding an offset into the frame object?
649   if (MF.getDataLayout().isBigEndian())
650     return nullptr;
651 
652   // Fold load from stack followed by sext.w into lw.
653   // TODO: Fold with sext.b, sext.h, zext.b, zext.h, zext.w?
654   if (Ops.size() == 1 && Ops[0] == 1 && RISCV::isSEXT_W(MI)) {
655     MachineMemOperand *MMO = MF.getMachineMemOperand(
656         MachinePointerInfo::getFixedStack(MF, FrameIndex),
657         MachineMemOperand::MOLoad, MFI.getObjectSize(FrameIndex),
658         MFI.getObjectAlign(FrameIndex));
659 
660     Register DstReg = MI.getOperand(0).getReg();
661     return BuildMI(*MI.getParent(), InsertPt, MI.getDebugLoc(), get(RISCV::LW),
662                    DstReg)
663         .addFrameIndex(FrameIndex)
664         .addImm(0)
665         .addMemOperand(MMO);
666   }
667 
668   return nullptr;
669 }
670 
671 void RISCVInstrInfo::movImm(MachineBasicBlock &MBB,
672                             MachineBasicBlock::iterator MBBI,
673                             const DebugLoc &DL, Register DstReg, uint64_t Val,
674                             MachineInstr::MIFlag Flag) const {
675   Register SrcReg = RISCV::X0;
676 
677   if (!STI.is64Bit() && !isInt<32>(Val))
678     report_fatal_error("Should only materialize 32-bit constants for RV32");
679 
680   RISCVMatInt::InstSeq Seq =
681       RISCVMatInt::generateInstSeq(Val, STI.getFeatureBits());
682   assert(!Seq.empty());
683 
684   for (RISCVMatInt::Inst &Inst : Seq) {
685     switch (Inst.getOpndKind()) {
686     case RISCVMatInt::Imm:
687       BuildMI(MBB, MBBI, DL, get(Inst.Opc), DstReg)
688           .addImm(Inst.Imm)
689           .setMIFlag(Flag);
690       break;
691     case RISCVMatInt::RegX0:
692       BuildMI(MBB, MBBI, DL, get(Inst.Opc), DstReg)
693           .addReg(SrcReg, RegState::Kill)
694           .addReg(RISCV::X0)
695           .setMIFlag(Flag);
696       break;
697     case RISCVMatInt::RegReg:
698       BuildMI(MBB, MBBI, DL, get(Inst.Opc), DstReg)
699           .addReg(SrcReg, RegState::Kill)
700           .addReg(SrcReg, RegState::Kill)
701           .setMIFlag(Flag);
702       break;
703     case RISCVMatInt::RegImm:
704       BuildMI(MBB, MBBI, DL, get(Inst.Opc), DstReg)
705           .addReg(SrcReg, RegState::Kill)
706           .addImm(Inst.Imm)
707           .setMIFlag(Flag);
708       break;
709     }
710 
711     // Only the first instruction has X0 as its source.
712     SrcReg = DstReg;
713   }
714 }
715 
716 static RISCVCC::CondCode getCondFromBranchOpc(unsigned Opc) {
717   switch (Opc) {
718   default:
719     return RISCVCC::COND_INVALID;
720   case RISCV::BEQ:
721     return RISCVCC::COND_EQ;
722   case RISCV::BNE:
723     return RISCVCC::COND_NE;
724   case RISCV::BLT:
725     return RISCVCC::COND_LT;
726   case RISCV::BGE:
727     return RISCVCC::COND_GE;
728   case RISCV::BLTU:
729     return RISCVCC::COND_LTU;
730   case RISCV::BGEU:
731     return RISCVCC::COND_GEU;
732   }
733 }
734 
735 // The contents of values added to Cond are not examined outside of
736 // RISCVInstrInfo, giving us flexibility in what to push to it. For RISCV, we
737 // push BranchOpcode, Reg1, Reg2.
738 static void parseCondBranch(MachineInstr &LastInst, MachineBasicBlock *&Target,
739                             SmallVectorImpl<MachineOperand> &Cond) {
740   // Block ends with fall-through condbranch.
741   assert(LastInst.getDesc().isConditionalBranch() &&
742          "Unknown conditional branch");
743   Target = LastInst.getOperand(2).getMBB();
744   unsigned CC = getCondFromBranchOpc(LastInst.getOpcode());
745   Cond.push_back(MachineOperand::CreateImm(CC));
746   Cond.push_back(LastInst.getOperand(0));
747   Cond.push_back(LastInst.getOperand(1));
748 }
749 
750 const MCInstrDesc &RISCVInstrInfo::getBrCond(RISCVCC::CondCode CC) const {
751   switch (CC) {
752   default:
753     llvm_unreachable("Unknown condition code!");
754   case RISCVCC::COND_EQ:
755     return get(RISCV::BEQ);
756   case RISCVCC::COND_NE:
757     return get(RISCV::BNE);
758   case RISCVCC::COND_LT:
759     return get(RISCV::BLT);
760   case RISCVCC::COND_GE:
761     return get(RISCV::BGE);
762   case RISCVCC::COND_LTU:
763     return get(RISCV::BLTU);
764   case RISCVCC::COND_GEU:
765     return get(RISCV::BGEU);
766   }
767 }
768 
769 RISCVCC::CondCode RISCVCC::getOppositeBranchCondition(RISCVCC::CondCode CC) {
770   switch (CC) {
771   default:
772     llvm_unreachable("Unrecognized conditional branch");
773   case RISCVCC::COND_EQ:
774     return RISCVCC::COND_NE;
775   case RISCVCC::COND_NE:
776     return RISCVCC::COND_EQ;
777   case RISCVCC::COND_LT:
778     return RISCVCC::COND_GE;
779   case RISCVCC::COND_GE:
780     return RISCVCC::COND_LT;
781   case RISCVCC::COND_LTU:
782     return RISCVCC::COND_GEU;
783   case RISCVCC::COND_GEU:
784     return RISCVCC::COND_LTU;
785   }
786 }
787 
788 bool RISCVInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
789                                    MachineBasicBlock *&TBB,
790                                    MachineBasicBlock *&FBB,
791                                    SmallVectorImpl<MachineOperand> &Cond,
792                                    bool AllowModify) const {
793   TBB = FBB = nullptr;
794   Cond.clear();
795 
796   // If the block has no terminators, it just falls into the block after it.
797   MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
798   if (I == MBB.end() || !isUnpredicatedTerminator(*I))
799     return false;
800 
801   // Count the number of terminators and find the first unconditional or
802   // indirect branch.
803   MachineBasicBlock::iterator FirstUncondOrIndirectBr = MBB.end();
804   int NumTerminators = 0;
805   for (auto J = I.getReverse(); J != MBB.rend() && isUnpredicatedTerminator(*J);
806        J++) {
807     NumTerminators++;
808     if (J->getDesc().isUnconditionalBranch() ||
809         J->getDesc().isIndirectBranch()) {
810       FirstUncondOrIndirectBr = J.getReverse();
811     }
812   }
813 
814   // If AllowModify is true, we can erase any terminators after
815   // FirstUncondOrIndirectBR.
816   if (AllowModify && FirstUncondOrIndirectBr != MBB.end()) {
817     while (std::next(FirstUncondOrIndirectBr) != MBB.end()) {
818       std::next(FirstUncondOrIndirectBr)->eraseFromParent();
819       NumTerminators--;
820     }
821     I = FirstUncondOrIndirectBr;
822   }
823 
824   // We can't handle blocks that end in an indirect branch.
825   if (I->getDesc().isIndirectBranch())
826     return true;
827 
828   // We can't handle blocks with more than 2 terminators.
829   if (NumTerminators > 2)
830     return true;
831 
832   // Handle a single unconditional branch.
833   if (NumTerminators == 1 && I->getDesc().isUnconditionalBranch()) {
834     TBB = getBranchDestBlock(*I);
835     return false;
836   }
837 
838   // Handle a single conditional branch.
839   if (NumTerminators == 1 && I->getDesc().isConditionalBranch()) {
840     parseCondBranch(*I, TBB, Cond);
841     return false;
842   }
843 
844   // Handle a conditional branch followed by an unconditional branch.
845   if (NumTerminators == 2 && std::prev(I)->getDesc().isConditionalBranch() &&
846       I->getDesc().isUnconditionalBranch()) {
847     parseCondBranch(*std::prev(I), TBB, Cond);
848     FBB = getBranchDestBlock(*I);
849     return false;
850   }
851 
852   // Otherwise, we can't handle this.
853   return true;
854 }
855 
856 unsigned RISCVInstrInfo::removeBranch(MachineBasicBlock &MBB,
857                                       int *BytesRemoved) const {
858   if (BytesRemoved)
859     *BytesRemoved = 0;
860   MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
861   if (I == MBB.end())
862     return 0;
863 
864   if (!I->getDesc().isUnconditionalBranch() &&
865       !I->getDesc().isConditionalBranch())
866     return 0;
867 
868   // Remove the branch.
869   if (BytesRemoved)
870     *BytesRemoved += getInstSizeInBytes(*I);
871   I->eraseFromParent();
872 
873   I = MBB.end();
874 
875   if (I == MBB.begin())
876     return 1;
877   --I;
878   if (!I->getDesc().isConditionalBranch())
879     return 1;
880 
881   // Remove the branch.
882   if (BytesRemoved)
883     *BytesRemoved += getInstSizeInBytes(*I);
884   I->eraseFromParent();
885   return 2;
886 }
887 
888 // Inserts a branch into the end of the specific MachineBasicBlock, returning
889 // the number of instructions inserted.
890 unsigned RISCVInstrInfo::insertBranch(
891     MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
892     ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
893   if (BytesAdded)
894     *BytesAdded = 0;
895 
896   // Shouldn't be a fall through.
897   assert(TBB && "insertBranch must not be told to insert a fallthrough");
898   assert((Cond.size() == 3 || Cond.size() == 0) &&
899          "RISCV branch conditions have two components!");
900 
901   // Unconditional branch.
902   if (Cond.empty()) {
903     MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(TBB);
904     if (BytesAdded)
905       *BytesAdded += getInstSizeInBytes(MI);
906     return 1;
907   }
908 
909   // Either a one or two-way conditional branch.
910   auto CC = static_cast<RISCVCC::CondCode>(Cond[0].getImm());
911   MachineInstr &CondMI =
912       *BuildMI(&MBB, DL, getBrCond(CC)).add(Cond[1]).add(Cond[2]).addMBB(TBB);
913   if (BytesAdded)
914     *BytesAdded += getInstSizeInBytes(CondMI);
915 
916   // One-way conditional branch.
917   if (!FBB)
918     return 1;
919 
920   // Two-way conditional branch.
921   MachineInstr &MI = *BuildMI(&MBB, DL, get(RISCV::PseudoBR)).addMBB(FBB);
922   if (BytesAdded)
923     *BytesAdded += getInstSizeInBytes(MI);
924   return 2;
925 }
926 
927 void RISCVInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
928                                           MachineBasicBlock &DestBB,
929                                           MachineBasicBlock &RestoreBB,
930                                           const DebugLoc &DL, int64_t BrOffset,
931                                           RegScavenger *RS) const {
932   assert(RS && "RegScavenger required for long branching");
933   assert(MBB.empty() &&
934          "new block should be inserted for expanding unconditional branch");
935   assert(MBB.pred_size() == 1);
936 
937   MachineFunction *MF = MBB.getParent();
938   MachineRegisterInfo &MRI = MF->getRegInfo();
939 
940   if (!isInt<32>(BrOffset))
941     report_fatal_error(
942         "Branch offsets outside of the signed 32-bit range not supported");
943 
944   // FIXME: A virtual register must be used initially, as the register
945   // scavenger won't work with empty blocks (SIInstrInfo::insertIndirectBranch
946   // uses the same workaround).
947   Register ScratchReg = MRI.createVirtualRegister(&RISCV::GPRRegClass);
948   auto II = MBB.end();
949 
950   MachineInstr &MI = *BuildMI(MBB, II, DL, get(RISCV::PseudoJump))
951                           .addReg(ScratchReg, RegState::Define | RegState::Dead)
952                           .addMBB(&DestBB, RISCVII::MO_CALL);
953 
954   RS->enterBasicBlockEnd(MBB);
955   Register Scav = RS->scavengeRegisterBackwards(RISCV::GPRRegClass,
956                                                 MI.getIterator(), false, 0);
957   // TODO: The case when there is no scavenged register needs special handling.
958   assert(Scav != RISCV::NoRegister && "No register is scavenged!");
959   MRI.replaceRegWith(ScratchReg, Scav);
960   MRI.clearVirtRegs();
961   RS->setRegUsed(Scav);
962 }
963 
964 bool RISCVInstrInfo::reverseBranchCondition(
965     SmallVectorImpl<MachineOperand> &Cond) const {
966   assert((Cond.size() == 3) && "Invalid branch condition!");
967   auto CC = static_cast<RISCVCC::CondCode>(Cond[0].getImm());
968   Cond[0].setImm(getOppositeBranchCondition(CC));
969   return false;
970 }
971 
972 MachineBasicBlock *
973 RISCVInstrInfo::getBranchDestBlock(const MachineInstr &MI) const {
974   assert(MI.getDesc().isBranch() && "Unexpected opcode!");
975   // The branch target is always the last operand.
976   int NumOp = MI.getNumExplicitOperands();
977   return MI.getOperand(NumOp - 1).getMBB();
978 }
979 
980 bool RISCVInstrInfo::isBranchOffsetInRange(unsigned BranchOp,
981                                            int64_t BrOffset) const {
982   unsigned XLen = STI.getXLen();
983   // Ideally we could determine the supported branch offset from the
984   // RISCVII::FormMask, but this can't be used for Pseudo instructions like
985   // PseudoBR.
986   switch (BranchOp) {
987   default:
988     llvm_unreachable("Unexpected opcode!");
989   case RISCV::BEQ:
990   case RISCV::BNE:
991   case RISCV::BLT:
992   case RISCV::BGE:
993   case RISCV::BLTU:
994   case RISCV::BGEU:
995     return isIntN(13, BrOffset);
996   case RISCV::JAL:
997   case RISCV::PseudoBR:
998     return isIntN(21, BrOffset);
999   case RISCV::PseudoJump:
1000     return isIntN(32, SignExtend64(BrOffset + 0x800, XLen));
1001   }
1002 }
1003 
1004 unsigned RISCVInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
1005   if (MI.isMetaInstruction())
1006     return 0;
1007 
1008   unsigned Opcode = MI.getOpcode();
1009 
1010   if (Opcode == TargetOpcode::INLINEASM ||
1011       Opcode == TargetOpcode::INLINEASM_BR) {
1012     const MachineFunction &MF = *MI.getParent()->getParent();
1013     const auto &TM = static_cast<const RISCVTargetMachine &>(MF.getTarget());
1014     return getInlineAsmLength(MI.getOperand(0).getSymbolName(),
1015                               *TM.getMCAsmInfo());
1016   }
1017 
1018   if (MI.getParent() && MI.getParent()->getParent()) {
1019     const auto MF = MI.getMF();
1020     const auto &TM = static_cast<const RISCVTargetMachine &>(MF->getTarget());
1021     const MCRegisterInfo &MRI = *TM.getMCRegisterInfo();
1022     const MCSubtargetInfo &STI = *TM.getMCSubtargetInfo();
1023     const RISCVSubtarget &ST = MF->getSubtarget<RISCVSubtarget>();
1024     if (isCompressibleInst(MI, &ST, MRI, STI))
1025       return 2;
1026   }
1027   return get(Opcode).getSize();
1028 }
1029 
1030 bool RISCVInstrInfo::isAsCheapAsAMove(const MachineInstr &MI) const {
1031   const unsigned Opcode = MI.getOpcode();
1032   switch (Opcode) {
1033   default:
1034     break;
1035   case RISCV::FSGNJ_D:
1036   case RISCV::FSGNJ_S:
1037   case RISCV::FSGNJ_H:
1038     // The canonical floating-point move is fsgnj rd, rs, rs.
1039     return MI.getOperand(1).isReg() && MI.getOperand(2).isReg() &&
1040            MI.getOperand(1).getReg() == MI.getOperand(2).getReg();
1041   case RISCV::ADDI:
1042   case RISCV::ORI:
1043   case RISCV::XORI:
1044     return (MI.getOperand(1).isReg() &&
1045             MI.getOperand(1).getReg() == RISCV::X0) ||
1046            (MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0);
1047   }
1048   return MI.isAsCheapAsAMove();
1049 }
1050 
1051 Optional<DestSourcePair>
1052 RISCVInstrInfo::isCopyInstrImpl(const MachineInstr &MI) const {
1053   if (MI.isMoveReg())
1054     return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
1055   switch (MI.getOpcode()) {
1056   default:
1057     break;
1058   case RISCV::ADDI:
1059     // Operand 1 can be a frameindex but callers expect registers
1060     if (MI.getOperand(1).isReg() && MI.getOperand(2).isImm() &&
1061         MI.getOperand(2).getImm() == 0)
1062       return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
1063     break;
1064   case RISCV::FSGNJ_D:
1065   case RISCV::FSGNJ_S:
1066   case RISCV::FSGNJ_H:
1067     // The canonical floating-point move is fsgnj rd, rs, rs.
1068     if (MI.getOperand(1).isReg() && MI.getOperand(2).isReg() &&
1069         MI.getOperand(1).getReg() == MI.getOperand(2).getReg())
1070       return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
1071     break;
1072   }
1073   return None;
1074 }
1075 
1076 bool RISCVInstrInfo::verifyInstruction(const MachineInstr &MI,
1077                                        StringRef &ErrInfo) const {
1078   const MCInstrInfo *MCII = STI.getInstrInfo();
1079   MCInstrDesc const &Desc = MCII->get(MI.getOpcode());
1080 
1081   for (auto &OI : enumerate(Desc.operands())) {
1082     unsigned OpType = OI.value().OperandType;
1083     if (OpType >= RISCVOp::OPERAND_FIRST_RISCV_IMM &&
1084         OpType <= RISCVOp::OPERAND_LAST_RISCV_IMM) {
1085       const MachineOperand &MO = MI.getOperand(OI.index());
1086       if (MO.isImm()) {
1087         int64_t Imm = MO.getImm();
1088         bool Ok;
1089         switch (OpType) {
1090         default:
1091           llvm_unreachable("Unexpected operand type");
1092 
1093           // clang-format off
1094 #define CASE_OPERAND_UIMM(NUM)                                                 \
1095   case RISCVOp::OPERAND_UIMM##NUM:                                             \
1096     Ok = isUInt<NUM>(Imm);                                                     \
1097     break;
1098         CASE_OPERAND_UIMM(2)
1099         CASE_OPERAND_UIMM(3)
1100         CASE_OPERAND_UIMM(4)
1101         CASE_OPERAND_UIMM(5)
1102         CASE_OPERAND_UIMM(7)
1103         CASE_OPERAND_UIMM(12)
1104         CASE_OPERAND_UIMM(20)
1105           // clang-format on
1106         case RISCVOp::OPERAND_SIMM12:
1107           Ok = isInt<12>(Imm);
1108           break;
1109         case RISCVOp::OPERAND_SIMM12_LSB00000:
1110           Ok = isShiftedInt<7, 5>(Imm);
1111           break;
1112         case RISCVOp::OPERAND_UIMMLOG2XLEN:
1113           if (STI.getTargetTriple().isArch64Bit())
1114             Ok = isUInt<6>(Imm);
1115           else
1116             Ok = isUInt<5>(Imm);
1117           break;
1118         case RISCVOp::OPERAND_RVKRNUM:
1119           Ok = Imm >= 0 && Imm <= 10;
1120           break;
1121         }
1122         if (!Ok) {
1123           ErrInfo = "Invalid immediate";
1124           return false;
1125         }
1126       }
1127     }
1128   }
1129 
1130   return true;
1131 }
1132 
1133 // Return true if get the base operand, byte offset of an instruction and the
1134 // memory width. Width is the size of memory that is being loaded/stored.
1135 bool RISCVInstrInfo::getMemOperandWithOffsetWidth(
1136     const MachineInstr &LdSt, const MachineOperand *&BaseReg, int64_t &Offset,
1137     unsigned &Width, const TargetRegisterInfo *TRI) const {
1138   if (!LdSt.mayLoadOrStore())
1139     return false;
1140 
1141   // Here we assume the standard RISC-V ISA, which uses a base+offset
1142   // addressing mode. You'll need to relax these conditions to support custom
1143   // load/stores instructions.
1144   if (LdSt.getNumExplicitOperands() != 3)
1145     return false;
1146   if (!LdSt.getOperand(1).isReg() || !LdSt.getOperand(2).isImm())
1147     return false;
1148 
1149   if (!LdSt.hasOneMemOperand())
1150     return false;
1151 
1152   Width = (*LdSt.memoperands_begin())->getSize();
1153   BaseReg = &LdSt.getOperand(1);
1154   Offset = LdSt.getOperand(2).getImm();
1155   return true;
1156 }
1157 
1158 bool RISCVInstrInfo::areMemAccessesTriviallyDisjoint(
1159     const MachineInstr &MIa, const MachineInstr &MIb) const {
1160   assert(MIa.mayLoadOrStore() && "MIa must be a load or store.");
1161   assert(MIb.mayLoadOrStore() && "MIb must be a load or store.");
1162 
1163   if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects() ||
1164       MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef())
1165     return false;
1166 
1167   // Retrieve the base register, offset from the base register and width. Width
1168   // is the size of memory that is being loaded/stored (e.g. 1, 2, 4).  If
1169   // base registers are identical, and the offset of a lower memory access +
1170   // the width doesn't overlap the offset of a higher memory access,
1171   // then the memory accesses are different.
1172   const TargetRegisterInfo *TRI = STI.getRegisterInfo();
1173   const MachineOperand *BaseOpA = nullptr, *BaseOpB = nullptr;
1174   int64_t OffsetA = 0, OffsetB = 0;
1175   unsigned int WidthA = 0, WidthB = 0;
1176   if (getMemOperandWithOffsetWidth(MIa, BaseOpA, OffsetA, WidthA, TRI) &&
1177       getMemOperandWithOffsetWidth(MIb, BaseOpB, OffsetB, WidthB, TRI)) {
1178     if (BaseOpA->isIdenticalTo(*BaseOpB)) {
1179       int LowOffset = std::min(OffsetA, OffsetB);
1180       int HighOffset = std::max(OffsetA, OffsetB);
1181       int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
1182       if (LowOffset + LowWidth <= HighOffset)
1183         return true;
1184     }
1185   }
1186   return false;
1187 }
1188 
1189 std::pair<unsigned, unsigned>
1190 RISCVInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
1191   const unsigned Mask = RISCVII::MO_DIRECT_FLAG_MASK;
1192   return std::make_pair(TF & Mask, TF & ~Mask);
1193 }
1194 
1195 ArrayRef<std::pair<unsigned, const char *>>
1196 RISCVInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
1197   using namespace RISCVII;
1198   static const std::pair<unsigned, const char *> TargetFlags[] = {
1199       {MO_CALL, "riscv-call"},
1200       {MO_PLT, "riscv-plt"},
1201       {MO_LO, "riscv-lo"},
1202       {MO_HI, "riscv-hi"},
1203       {MO_PCREL_LO, "riscv-pcrel-lo"},
1204       {MO_PCREL_HI, "riscv-pcrel-hi"},
1205       {MO_GOT_HI, "riscv-got-hi"},
1206       {MO_TPREL_LO, "riscv-tprel-lo"},
1207       {MO_TPREL_HI, "riscv-tprel-hi"},
1208       {MO_TPREL_ADD, "riscv-tprel-add"},
1209       {MO_TLS_GOT_HI, "riscv-tls-got-hi"},
1210       {MO_TLS_GD_HI, "riscv-tls-gd-hi"}};
1211   return makeArrayRef(TargetFlags);
1212 }
1213 bool RISCVInstrInfo::isFunctionSafeToOutlineFrom(
1214     MachineFunction &MF, bool OutlineFromLinkOnceODRs) const {
1215   const Function &F = MF.getFunction();
1216 
1217   // Can F be deduplicated by the linker? If it can, don't outline from it.
1218   if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage())
1219     return false;
1220 
1221   // Don't outline from functions with section markings; the program could
1222   // expect that all the code is in the named section.
1223   if (F.hasSection())
1224     return false;
1225 
1226   // It's safe to outline from MF.
1227   return true;
1228 }
1229 
1230 bool RISCVInstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
1231                                             unsigned &Flags) const {
1232   // More accurate safety checking is done in getOutliningCandidateInfo.
1233   return TargetInstrInfo::isMBBSafeToOutlineFrom(MBB, Flags);
1234 }
1235 
1236 // Enum values indicating how an outlined call should be constructed.
1237 enum MachineOutlinerConstructionID {
1238   MachineOutlinerDefault
1239 };
1240 
1241 bool RISCVInstrInfo::shouldOutlineFromFunctionByDefault(
1242     MachineFunction &MF) const {
1243   return MF.getFunction().hasMinSize();
1244 }
1245 
1246 outliner::OutlinedFunction RISCVInstrInfo::getOutliningCandidateInfo(
1247     std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
1248 
1249   // First we need to filter out candidates where the X5 register (IE t0) can't
1250   // be used to setup the function call.
1251   auto CannotInsertCall = [](outliner::Candidate &C) {
1252     const TargetRegisterInfo *TRI = C.getMF()->getSubtarget().getRegisterInfo();
1253     return !C.isAvailableAcrossAndOutOfSeq(RISCV::X5, *TRI);
1254   };
1255 
1256   llvm::erase_if(RepeatedSequenceLocs, CannotInsertCall);
1257 
1258   // If the sequence doesn't have enough candidates left, then we're done.
1259   if (RepeatedSequenceLocs.size() < 2)
1260     return outliner::OutlinedFunction();
1261 
1262   unsigned SequenceSize = 0;
1263 
1264   auto I = RepeatedSequenceLocs[0].front();
1265   auto E = std::next(RepeatedSequenceLocs[0].back());
1266   for (; I != E; ++I)
1267     SequenceSize += getInstSizeInBytes(*I);
1268 
1269   // call t0, function = 8 bytes.
1270   unsigned CallOverhead = 8;
1271   for (auto &C : RepeatedSequenceLocs)
1272     C.setCallInfo(MachineOutlinerDefault, CallOverhead);
1273 
1274   // jr t0 = 4 bytes, 2 bytes if compressed instructions are enabled.
1275   unsigned FrameOverhead = 4;
1276   if (RepeatedSequenceLocs[0].getMF()->getSubtarget()
1277           .getFeatureBits()[RISCV::FeatureStdExtC])
1278     FrameOverhead = 2;
1279 
1280   return outliner::OutlinedFunction(RepeatedSequenceLocs, SequenceSize,
1281                                     FrameOverhead, MachineOutlinerDefault);
1282 }
1283 
1284 outliner::InstrType
1285 RISCVInstrInfo::getOutliningType(MachineBasicBlock::iterator &MBBI,
1286                                  unsigned Flags) const {
1287   MachineInstr &MI = *MBBI;
1288   MachineBasicBlock *MBB = MI.getParent();
1289   const TargetRegisterInfo *TRI =
1290       MBB->getParent()->getSubtarget().getRegisterInfo();
1291 
1292   // Positions generally can't safely be outlined.
1293   if (MI.isPosition()) {
1294     // We can manually strip out CFI instructions later.
1295     if (MI.isCFIInstruction())
1296       // If current function has exception handling code, we can't outline &
1297       // strip these CFI instructions since it may break .eh_frame section
1298       // needed in unwinding.
1299       return MI.getMF()->getFunction().needsUnwindTableEntry()
1300                  ? outliner::InstrType::Illegal
1301                  : outliner::InstrType::Invisible;
1302 
1303     return outliner::InstrType::Illegal;
1304   }
1305 
1306   // Don't trust the user to write safe inline assembly.
1307   if (MI.isInlineAsm())
1308     return outliner::InstrType::Illegal;
1309 
1310   // We can't outline branches to other basic blocks.
1311   if (MI.isTerminator() && !MBB->succ_empty())
1312     return outliner::InstrType::Illegal;
1313 
1314   // We need support for tail calls to outlined functions before return
1315   // statements can be allowed.
1316   if (MI.isReturn())
1317     return outliner::InstrType::Illegal;
1318 
1319   // Don't allow modifying the X5 register which we use for return addresses for
1320   // these outlined functions.
1321   if (MI.modifiesRegister(RISCV::X5, TRI) ||
1322       MI.getDesc().hasImplicitDefOfPhysReg(RISCV::X5))
1323     return outliner::InstrType::Illegal;
1324 
1325   // Make sure the operands don't reference something unsafe.
1326   for (const auto &MO : MI.operands())
1327     if (MO.isMBB() || MO.isBlockAddress() || MO.isCPI() || MO.isJTI())
1328       return outliner::InstrType::Illegal;
1329 
1330   // Don't allow instructions which won't be materialized to impact outlining
1331   // analysis.
1332   if (MI.isMetaInstruction())
1333     return outliner::InstrType::Invisible;
1334 
1335   return outliner::InstrType::Legal;
1336 }
1337 
1338 void RISCVInstrInfo::buildOutlinedFrame(
1339     MachineBasicBlock &MBB, MachineFunction &MF,
1340     const outliner::OutlinedFunction &OF) const {
1341 
1342   // Strip out any CFI instructions
1343   bool Changed = true;
1344   while (Changed) {
1345     Changed = false;
1346     auto I = MBB.begin();
1347     auto E = MBB.end();
1348     for (; I != E; ++I) {
1349       if (I->isCFIInstruction()) {
1350         I->removeFromParent();
1351         Changed = true;
1352         break;
1353       }
1354     }
1355   }
1356 
1357   MBB.addLiveIn(RISCV::X5);
1358 
1359   // Add in a return instruction to the end of the outlined frame.
1360   MBB.insert(MBB.end(), BuildMI(MF, DebugLoc(), get(RISCV::JALR))
1361       .addReg(RISCV::X0, RegState::Define)
1362       .addReg(RISCV::X5)
1363       .addImm(0));
1364 }
1365 
1366 MachineBasicBlock::iterator RISCVInstrInfo::insertOutlinedCall(
1367     Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It,
1368     MachineFunction &MF, outliner::Candidate &C) const {
1369 
1370   // Add in a call instruction to the outlined function at the given location.
1371   It = MBB.insert(It,
1372                   BuildMI(MF, DebugLoc(), get(RISCV::PseudoCALLReg), RISCV::X5)
1373                       .addGlobalAddress(M.getNamedValue(MF.getName()), 0,
1374                                         RISCVII::MO_CALL));
1375   return It;
1376 }
1377 
1378 // MIR printer helper function to annotate Operands with a comment.
1379 std::string RISCVInstrInfo::createMIROperandComment(
1380     const MachineInstr &MI, const MachineOperand &Op, unsigned OpIdx,
1381     const TargetRegisterInfo *TRI) const {
1382   // Print a generic comment for this operand if there is one.
1383   std::string GenericComment =
1384       TargetInstrInfo::createMIROperandComment(MI, Op, OpIdx, TRI);
1385   if (!GenericComment.empty())
1386     return GenericComment;
1387 
1388   // If not, we must have an immediate operand.
1389   if (!Op.isImm())
1390     return std::string();
1391 
1392   std::string Comment;
1393   raw_string_ostream OS(Comment);
1394 
1395   uint64_t TSFlags = MI.getDesc().TSFlags;
1396 
1397   // Print the full VType operand of vsetvli/vsetivli instructions, and the SEW
1398   // operand of vector codegen pseudos.
1399   if ((MI.getOpcode() == RISCV::VSETVLI || MI.getOpcode() == RISCV::VSETIVLI ||
1400        MI.getOpcode() == RISCV::PseudoVSETVLI ||
1401        MI.getOpcode() == RISCV::PseudoVSETIVLI ||
1402        MI.getOpcode() == RISCV::PseudoVSETVLIX0) &&
1403       OpIdx == 2) {
1404     unsigned Imm = MI.getOperand(OpIdx).getImm();
1405     RISCVVType::printVType(Imm, OS);
1406   } else if (RISCVII::hasSEWOp(TSFlags)) {
1407     unsigned NumOperands = MI.getNumExplicitOperands();
1408     bool HasPolicy = RISCVII::hasVecPolicyOp(TSFlags);
1409 
1410     // The SEW operand is before any policy operand.
1411     if (OpIdx != NumOperands - HasPolicy - 1)
1412       return std::string();
1413 
1414     unsigned Log2SEW = MI.getOperand(OpIdx).getImm();
1415     unsigned SEW = Log2SEW ? 1 << Log2SEW : 8;
1416     assert(RISCVVType::isValidSEW(SEW) && "Unexpected SEW");
1417 
1418     OS << "e" << SEW;
1419   }
1420 
1421   OS.flush();
1422   return Comment;
1423 }
1424 
1425 // clang-format off
1426 #define CASE_VFMA_OPCODE_COMMON(OP, TYPE, LMUL)                                \
1427   RISCV::PseudoV##OP##_##TYPE##_##LMUL
1428 
1429 #define CASE_VFMA_OPCODE_LMULS_M1(OP, TYPE)                                    \
1430   CASE_VFMA_OPCODE_COMMON(OP, TYPE, M1):                                       \
1431   case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M2):                                  \
1432   case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M4):                                  \
1433   case CASE_VFMA_OPCODE_COMMON(OP, TYPE, M8)
1434 
1435 #define CASE_VFMA_OPCODE_LMULS_MF2(OP, TYPE)                                   \
1436   CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF2):                                      \
1437   case CASE_VFMA_OPCODE_LMULS_M1(OP, TYPE)
1438 
1439 #define CASE_VFMA_OPCODE_LMULS_MF4(OP, TYPE)                                   \
1440   CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF4):                                      \
1441   case CASE_VFMA_OPCODE_LMULS_MF2(OP, TYPE)
1442 
1443 #define CASE_VFMA_OPCODE_LMULS(OP, TYPE)                                       \
1444   CASE_VFMA_OPCODE_COMMON(OP, TYPE, MF8):                                      \
1445   case CASE_VFMA_OPCODE_LMULS_MF4(OP, TYPE)
1446 
1447 #define CASE_VFMA_SPLATS(OP)                                                   \
1448   CASE_VFMA_OPCODE_LMULS_MF4(OP, VF16):                                        \
1449   case CASE_VFMA_OPCODE_LMULS_MF2(OP, VF32):                                   \
1450   case CASE_VFMA_OPCODE_LMULS_M1(OP, VF64)
1451 // clang-format on
1452 
1453 bool RISCVInstrInfo::findCommutedOpIndices(const MachineInstr &MI,
1454                                            unsigned &SrcOpIdx1,
1455                                            unsigned &SrcOpIdx2) const {
1456   const MCInstrDesc &Desc = MI.getDesc();
1457   if (!Desc.isCommutable())
1458     return false;
1459 
1460   switch (MI.getOpcode()) {
1461   case CASE_VFMA_SPLATS(FMADD):
1462   case CASE_VFMA_SPLATS(FMSUB):
1463   case CASE_VFMA_SPLATS(FMACC):
1464   case CASE_VFMA_SPLATS(FMSAC):
1465   case CASE_VFMA_SPLATS(FNMADD):
1466   case CASE_VFMA_SPLATS(FNMSUB):
1467   case CASE_VFMA_SPLATS(FNMACC):
1468   case CASE_VFMA_SPLATS(FNMSAC):
1469   case CASE_VFMA_OPCODE_LMULS_MF4(FMACC, VV):
1470   case CASE_VFMA_OPCODE_LMULS_MF4(FMSAC, VV):
1471   case CASE_VFMA_OPCODE_LMULS_MF4(FNMACC, VV):
1472   case CASE_VFMA_OPCODE_LMULS_MF4(FNMSAC, VV):
1473   case CASE_VFMA_OPCODE_LMULS(MADD, VX):
1474   case CASE_VFMA_OPCODE_LMULS(NMSUB, VX):
1475   case CASE_VFMA_OPCODE_LMULS(MACC, VX):
1476   case CASE_VFMA_OPCODE_LMULS(NMSAC, VX):
1477   case CASE_VFMA_OPCODE_LMULS(MACC, VV):
1478   case CASE_VFMA_OPCODE_LMULS(NMSAC, VV): {
1479     // If the tail policy is undisturbed we can't commute.
1480     assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags));
1481     if ((MI.getOperand(MI.getNumExplicitOperands() - 1).getImm() & 1) == 0)
1482       return false;
1483 
1484     // For these instructions we can only swap operand 1 and operand 3 by
1485     // changing the opcode.
1486     unsigned CommutableOpIdx1 = 1;
1487     unsigned CommutableOpIdx2 = 3;
1488     if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1,
1489                               CommutableOpIdx2))
1490       return false;
1491     return true;
1492   }
1493   case CASE_VFMA_OPCODE_LMULS_MF4(FMADD, VV):
1494   case CASE_VFMA_OPCODE_LMULS_MF4(FMSUB, VV):
1495   case CASE_VFMA_OPCODE_LMULS_MF4(FNMADD, VV):
1496   case CASE_VFMA_OPCODE_LMULS_MF4(FNMSUB, VV):
1497   case CASE_VFMA_OPCODE_LMULS(MADD, VV):
1498   case CASE_VFMA_OPCODE_LMULS(NMSUB, VV): {
1499     // If the tail policy is undisturbed we can't commute.
1500     assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags));
1501     if ((MI.getOperand(MI.getNumExplicitOperands() - 1).getImm() & 1) == 0)
1502       return false;
1503 
1504     // For these instructions we have more freedom. We can commute with the
1505     // other multiplicand or with the addend/subtrahend/minuend.
1506 
1507     // Any fixed operand must be from source 1, 2 or 3.
1508     if (SrcOpIdx1 != CommuteAnyOperandIndex && SrcOpIdx1 > 3)
1509       return false;
1510     if (SrcOpIdx2 != CommuteAnyOperandIndex && SrcOpIdx2 > 3)
1511       return false;
1512 
1513     // It both ops are fixed one must be the tied source.
1514     if (SrcOpIdx1 != CommuteAnyOperandIndex &&
1515         SrcOpIdx2 != CommuteAnyOperandIndex && SrcOpIdx1 != 1 && SrcOpIdx2 != 1)
1516       return false;
1517 
1518     // Look for two different register operands assumed to be commutable
1519     // regardless of the FMA opcode. The FMA opcode is adjusted later if
1520     // needed.
1521     if (SrcOpIdx1 == CommuteAnyOperandIndex ||
1522         SrcOpIdx2 == CommuteAnyOperandIndex) {
1523       // At least one of operands to be commuted is not specified and
1524       // this method is free to choose appropriate commutable operands.
1525       unsigned CommutableOpIdx1 = SrcOpIdx1;
1526       if (SrcOpIdx1 == SrcOpIdx2) {
1527         // Both of operands are not fixed. Set one of commutable
1528         // operands to the tied source.
1529         CommutableOpIdx1 = 1;
1530       } else if (SrcOpIdx1 == CommuteAnyOperandIndex) {
1531         // Only one of the operands is not fixed.
1532         CommutableOpIdx1 = SrcOpIdx2;
1533       }
1534 
1535       // CommutableOpIdx1 is well defined now. Let's choose another commutable
1536       // operand and assign its index to CommutableOpIdx2.
1537       unsigned CommutableOpIdx2;
1538       if (CommutableOpIdx1 != 1) {
1539         // If we haven't already used the tied source, we must use it now.
1540         CommutableOpIdx2 = 1;
1541       } else {
1542         Register Op1Reg = MI.getOperand(CommutableOpIdx1).getReg();
1543 
1544         // The commuted operands should have different registers.
1545         // Otherwise, the commute transformation does not change anything and
1546         // is useless. We use this as a hint to make our decision.
1547         if (Op1Reg != MI.getOperand(2).getReg())
1548           CommutableOpIdx2 = 2;
1549         else
1550           CommutableOpIdx2 = 3;
1551       }
1552 
1553       // Assign the found pair of commutable indices to SrcOpIdx1 and
1554       // SrcOpIdx2 to return those values.
1555       if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2, CommutableOpIdx1,
1556                                 CommutableOpIdx2))
1557         return false;
1558     }
1559 
1560     return true;
1561   }
1562   }
1563 
1564   return TargetInstrInfo::findCommutedOpIndices(MI, SrcOpIdx1, SrcOpIdx2);
1565 }
1566 
1567 #define CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, LMUL)               \
1568   case RISCV::PseudoV##OLDOP##_##TYPE##_##LMUL:                                \
1569     Opc = RISCV::PseudoV##NEWOP##_##TYPE##_##LMUL;                             \
1570     break;
1571 
1572 #define CASE_VFMA_CHANGE_OPCODE_LMULS_M1(OLDOP, NEWOP, TYPE)                   \
1573   CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M1)                       \
1574   CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M2)                       \
1575   CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M4)                       \
1576   CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, M8)
1577 
1578 #define CASE_VFMA_CHANGE_OPCODE_LMULS_MF2(OLDOP, NEWOP, TYPE)                  \
1579   CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF2)                      \
1580   CASE_VFMA_CHANGE_OPCODE_LMULS_M1(OLDOP, NEWOP, TYPE)
1581 
1582 #define CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(OLDOP, NEWOP, TYPE)                  \
1583   CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF4)                      \
1584   CASE_VFMA_CHANGE_OPCODE_LMULS_MF2(OLDOP, NEWOP, TYPE)
1585 
1586 #define CASE_VFMA_CHANGE_OPCODE_LMULS(OLDOP, NEWOP, TYPE)                      \
1587   CASE_VFMA_CHANGE_OPCODE_COMMON(OLDOP, NEWOP, TYPE, MF8)                      \
1588   CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(OLDOP, NEWOP, TYPE)
1589 
1590 #define CASE_VFMA_CHANGE_OPCODE_SPLATS(OLDOP, NEWOP)                           \
1591   CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(OLDOP, NEWOP, VF16)                        \
1592   CASE_VFMA_CHANGE_OPCODE_LMULS_MF2(OLDOP, NEWOP, VF32)                        \
1593   CASE_VFMA_CHANGE_OPCODE_LMULS_M1(OLDOP, NEWOP, VF64)
1594 
1595 MachineInstr *RISCVInstrInfo::commuteInstructionImpl(MachineInstr &MI,
1596                                                      bool NewMI,
1597                                                      unsigned OpIdx1,
1598                                                      unsigned OpIdx2) const {
1599   auto cloneIfNew = [NewMI](MachineInstr &MI) -> MachineInstr & {
1600     if (NewMI)
1601       return *MI.getParent()->getParent()->CloneMachineInstr(&MI);
1602     return MI;
1603   };
1604 
1605   switch (MI.getOpcode()) {
1606   case CASE_VFMA_SPLATS(FMACC):
1607   case CASE_VFMA_SPLATS(FMADD):
1608   case CASE_VFMA_SPLATS(FMSAC):
1609   case CASE_VFMA_SPLATS(FMSUB):
1610   case CASE_VFMA_SPLATS(FNMACC):
1611   case CASE_VFMA_SPLATS(FNMADD):
1612   case CASE_VFMA_SPLATS(FNMSAC):
1613   case CASE_VFMA_SPLATS(FNMSUB):
1614   case CASE_VFMA_OPCODE_LMULS_MF4(FMACC, VV):
1615   case CASE_VFMA_OPCODE_LMULS_MF4(FMSAC, VV):
1616   case CASE_VFMA_OPCODE_LMULS_MF4(FNMACC, VV):
1617   case CASE_VFMA_OPCODE_LMULS_MF4(FNMSAC, VV):
1618   case CASE_VFMA_OPCODE_LMULS(MADD, VX):
1619   case CASE_VFMA_OPCODE_LMULS(NMSUB, VX):
1620   case CASE_VFMA_OPCODE_LMULS(MACC, VX):
1621   case CASE_VFMA_OPCODE_LMULS(NMSAC, VX):
1622   case CASE_VFMA_OPCODE_LMULS(MACC, VV):
1623   case CASE_VFMA_OPCODE_LMULS(NMSAC, VV): {
1624     // It only make sense to toggle these between clobbering the
1625     // addend/subtrahend/minuend one of the multiplicands.
1626     assert((OpIdx1 == 1 || OpIdx2 == 1) && "Unexpected opcode index");
1627     assert((OpIdx1 == 3 || OpIdx2 == 3) && "Unexpected opcode index");
1628     unsigned Opc;
1629     switch (MI.getOpcode()) {
1630       default:
1631         llvm_unreachable("Unexpected opcode");
1632       CASE_VFMA_CHANGE_OPCODE_SPLATS(FMACC, FMADD)
1633       CASE_VFMA_CHANGE_OPCODE_SPLATS(FMADD, FMACC)
1634       CASE_VFMA_CHANGE_OPCODE_SPLATS(FMSAC, FMSUB)
1635       CASE_VFMA_CHANGE_OPCODE_SPLATS(FMSUB, FMSAC)
1636       CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMACC, FNMADD)
1637       CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMADD, FNMACC)
1638       CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMSAC, FNMSUB)
1639       CASE_VFMA_CHANGE_OPCODE_SPLATS(FNMSUB, FNMSAC)
1640       CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(FMACC, FMADD, VV)
1641       CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(FMSAC, FMSUB, VV)
1642       CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(FNMACC, FNMADD, VV)
1643       CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(FNMSAC, FNMSUB, VV)
1644       CASE_VFMA_CHANGE_OPCODE_LMULS(MACC, MADD, VX)
1645       CASE_VFMA_CHANGE_OPCODE_LMULS(MADD, MACC, VX)
1646       CASE_VFMA_CHANGE_OPCODE_LMULS(NMSAC, NMSUB, VX)
1647       CASE_VFMA_CHANGE_OPCODE_LMULS(NMSUB, NMSAC, VX)
1648       CASE_VFMA_CHANGE_OPCODE_LMULS(MACC, MADD, VV)
1649       CASE_VFMA_CHANGE_OPCODE_LMULS(NMSAC, NMSUB, 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   case CASE_VFMA_OPCODE_LMULS_MF4(FMADD, VV):
1658   case CASE_VFMA_OPCODE_LMULS_MF4(FMSUB, VV):
1659   case CASE_VFMA_OPCODE_LMULS_MF4(FNMADD, VV):
1660   case CASE_VFMA_OPCODE_LMULS_MF4(FNMSUB, VV):
1661   case CASE_VFMA_OPCODE_LMULS(MADD, VV):
1662   case CASE_VFMA_OPCODE_LMULS(NMSUB, VV): {
1663     assert((OpIdx1 == 1 || OpIdx2 == 1) && "Unexpected opcode index");
1664     // If one of the operands, is the addend we need to change opcode.
1665     // Otherwise we're just swapping 2 of the multiplicands.
1666     if (OpIdx1 == 3 || OpIdx2 == 3) {
1667       unsigned Opc;
1668       switch (MI.getOpcode()) {
1669         default:
1670           llvm_unreachable("Unexpected opcode");
1671         CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(FMADD, FMACC, VV)
1672         CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(FMSUB, FMSAC, VV)
1673         CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(FNMADD, FNMACC, VV)
1674         CASE_VFMA_CHANGE_OPCODE_LMULS_MF4(FNMSUB, FNMSAC, VV)
1675         CASE_VFMA_CHANGE_OPCODE_LMULS(MADD, MACC, VV)
1676         CASE_VFMA_CHANGE_OPCODE_LMULS(NMSUB, NMSAC, VV)
1677       }
1678 
1679       auto &WorkingMI = cloneIfNew(MI);
1680       WorkingMI.setDesc(get(Opc));
1681       return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false,
1682                                                      OpIdx1, OpIdx2);
1683     }
1684     // Let the default code handle it.
1685     break;
1686   }
1687   }
1688 
1689   return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
1690 }
1691 
1692 #undef CASE_VFMA_CHANGE_OPCODE_SPLATS
1693 #undef CASE_VFMA_CHANGE_OPCODE_LMULS
1694 #undef CASE_VFMA_CHANGE_OPCODE_COMMON
1695 #undef CASE_VFMA_SPLATS
1696 #undef CASE_VFMA_OPCODE_LMULS
1697 #undef CASE_VFMA_OPCODE_COMMON
1698 
1699 // clang-format off
1700 #define CASE_WIDEOP_OPCODE_COMMON(OP, LMUL)                                    \
1701   RISCV::PseudoV##OP##_##LMUL##_TIED
1702 
1703 #define CASE_WIDEOP_OPCODE_LMULS_MF4(OP)                                       \
1704   CASE_WIDEOP_OPCODE_COMMON(OP, MF4):                                          \
1705   case CASE_WIDEOP_OPCODE_COMMON(OP, MF2):                                     \
1706   case CASE_WIDEOP_OPCODE_COMMON(OP, M1):                                      \
1707   case CASE_WIDEOP_OPCODE_COMMON(OP, M2):                                      \
1708   case CASE_WIDEOP_OPCODE_COMMON(OP, M4)
1709 
1710 #define CASE_WIDEOP_OPCODE_LMULS(OP)                                           \
1711   CASE_WIDEOP_OPCODE_COMMON(OP, MF8):                                          \
1712   case CASE_WIDEOP_OPCODE_LMULS_MF4(OP)
1713 // clang-format on
1714 
1715 #define CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, LMUL)                             \
1716   case RISCV::PseudoV##OP##_##LMUL##_TIED:                                     \
1717     NewOpc = RISCV::PseudoV##OP##_##LMUL;                                      \
1718     break;
1719 
1720 #define CASE_WIDEOP_CHANGE_OPCODE_LMULS_MF4(OP)                                 \
1721   CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF4)                                    \
1722   CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF2)                                    \
1723   CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M1)                                     \
1724   CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M2)                                     \
1725   CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, M4)
1726 
1727 #define CASE_WIDEOP_CHANGE_OPCODE_LMULS(OP)                                    \
1728   CASE_WIDEOP_CHANGE_OPCODE_COMMON(OP, MF8)                                    \
1729   CASE_WIDEOP_CHANGE_OPCODE_LMULS_MF4(OP)
1730 
1731 MachineInstr *RISCVInstrInfo::convertToThreeAddress(MachineInstr &MI,
1732                                                     LiveVariables *LV,
1733                                                     LiveIntervals *LIS) const {
1734   switch (MI.getOpcode()) {
1735   default:
1736     break;
1737   case CASE_WIDEOP_OPCODE_LMULS_MF4(FWADD_WV):
1738   case CASE_WIDEOP_OPCODE_LMULS_MF4(FWSUB_WV):
1739   case CASE_WIDEOP_OPCODE_LMULS(WADD_WV):
1740   case CASE_WIDEOP_OPCODE_LMULS(WADDU_WV):
1741   case CASE_WIDEOP_OPCODE_LMULS(WSUB_WV):
1742   case CASE_WIDEOP_OPCODE_LMULS(WSUBU_WV): {
1743     // If the tail policy is undisturbed we can't convert.
1744     assert(RISCVII::hasVecPolicyOp(MI.getDesc().TSFlags) &&
1745            MI.getNumExplicitOperands() == 6);
1746     if ((MI.getOperand(5).getImm() & 1) == 0)
1747       return nullptr;
1748 
1749     // clang-format off
1750     unsigned NewOpc;
1751     switch (MI.getOpcode()) {
1752     default:
1753       llvm_unreachable("Unexpected opcode");
1754     CASE_WIDEOP_CHANGE_OPCODE_LMULS_MF4(FWADD_WV)
1755     CASE_WIDEOP_CHANGE_OPCODE_LMULS_MF4(FWSUB_WV)
1756     CASE_WIDEOP_CHANGE_OPCODE_LMULS(WADD_WV)
1757     CASE_WIDEOP_CHANGE_OPCODE_LMULS(WADDU_WV)
1758     CASE_WIDEOP_CHANGE_OPCODE_LMULS(WSUB_WV)
1759     CASE_WIDEOP_CHANGE_OPCODE_LMULS(WSUBU_WV)
1760     }
1761     // clang-format on
1762 
1763     MachineBasicBlock &MBB = *MI.getParent();
1764     MachineInstrBuilder MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc))
1765                                   .add(MI.getOperand(0))
1766                                   .add(MI.getOperand(1))
1767                                   .add(MI.getOperand(2))
1768                                   .add(MI.getOperand(3))
1769                                   .add(MI.getOperand(4));
1770     MIB.copyImplicitOps(MI);
1771 
1772     if (LV) {
1773       unsigned NumOps = MI.getNumOperands();
1774       for (unsigned I = 1; I < NumOps; ++I) {
1775         MachineOperand &Op = MI.getOperand(I);
1776         if (Op.isReg() && Op.isKill())
1777           LV->replaceKillInstruction(Op.getReg(), MI, *MIB);
1778       }
1779     }
1780 
1781     if (LIS) {
1782       SlotIndex Idx = LIS->ReplaceMachineInstrInMaps(MI, *MIB);
1783 
1784       if (MI.getOperand(0).isEarlyClobber()) {
1785         // Use operand 1 was tied to early-clobber def operand 0, so its live
1786         // interval could have ended at an early-clobber slot. Now they are not
1787         // tied we need to update it to the normal register slot.
1788         LiveInterval &LI = LIS->getInterval(MI.getOperand(1).getReg());
1789         LiveRange::Segment *S = LI.getSegmentContaining(Idx);
1790         if (S->end == Idx.getRegSlot(true))
1791           S->end = Idx.getRegSlot();
1792       }
1793     }
1794 
1795     return MIB;
1796   }
1797   }
1798 
1799   return nullptr;
1800 }
1801 
1802 #undef CASE_WIDEOP_CHANGE_OPCODE_LMULS
1803 #undef CASE_WIDEOP_CHANGE_OPCODE_COMMON
1804 #undef CASE_WIDEOP_OPCODE_LMULS
1805 #undef CASE_WIDEOP_OPCODE_COMMON
1806 
1807 Register RISCVInstrInfo::getVLENFactoredAmount(MachineFunction &MF,
1808                                                MachineBasicBlock &MBB,
1809                                                MachineBasicBlock::iterator II,
1810                                                const DebugLoc &DL,
1811                                                int64_t Amount,
1812                                                MachineInstr::MIFlag Flag) const {
1813   assert(Amount > 0 && "There is no need to get VLEN scaled value.");
1814   assert(Amount % 8 == 0 &&
1815          "Reserve the stack by the multiple of one vector size.");
1816 
1817   MachineRegisterInfo &MRI = MF.getRegInfo();
1818   int64_t NumOfVReg = Amount / 8;
1819 
1820   Register VL = MRI.createVirtualRegister(&RISCV::GPRRegClass);
1821   BuildMI(MBB, II, DL, get(RISCV::PseudoReadVLENB), VL)
1822     .setMIFlag(Flag);
1823   assert(isInt<32>(NumOfVReg) &&
1824          "Expect the number of vector registers within 32-bits.");
1825   if (isPowerOf2_32(NumOfVReg)) {
1826     uint32_t ShiftAmount = Log2_32(NumOfVReg);
1827     if (ShiftAmount == 0)
1828       return VL;
1829     BuildMI(MBB, II, DL, get(RISCV::SLLI), VL)
1830         .addReg(VL, RegState::Kill)
1831         .addImm(ShiftAmount)
1832         .setMIFlag(Flag);
1833   } else if (STI.hasStdExtZba() &&
1834              ((NumOfVReg % 3 == 0 && isPowerOf2_64(NumOfVReg / 3)) ||
1835               (NumOfVReg % 5 == 0 && isPowerOf2_64(NumOfVReg / 5)) ||
1836               (NumOfVReg % 9 == 0 && isPowerOf2_64(NumOfVReg / 9)))) {
1837     // We can use Zba SHXADD+SLLI instructions for multiply in some cases.
1838     unsigned Opc;
1839     uint32_t ShiftAmount;
1840     if (NumOfVReg % 9 == 0) {
1841       Opc = RISCV::SH3ADD;
1842       ShiftAmount = Log2_64(NumOfVReg / 9);
1843     } else if (NumOfVReg % 5 == 0) {
1844       Opc = RISCV::SH2ADD;
1845       ShiftAmount = Log2_64(NumOfVReg / 5);
1846     } else if (NumOfVReg % 3 == 0) {
1847       Opc = RISCV::SH1ADD;
1848       ShiftAmount = Log2_64(NumOfVReg / 3);
1849     } else {
1850       llvm_unreachable("Unexpected number of vregs");
1851     }
1852     if (ShiftAmount)
1853       BuildMI(MBB, II, DL, get(RISCV::SLLI), VL)
1854           .addReg(VL, RegState::Kill)
1855           .addImm(ShiftAmount)
1856           .setMIFlag(Flag);
1857     BuildMI(MBB, II, DL, get(Opc), VL)
1858         .addReg(VL, RegState::Kill)
1859         .addReg(VL)
1860         .setMIFlag(Flag);
1861   } else if (isPowerOf2_32(NumOfVReg - 1)) {
1862     Register ScaledRegister = MRI.createVirtualRegister(&RISCV::GPRRegClass);
1863     uint32_t ShiftAmount = Log2_32(NumOfVReg - 1);
1864     BuildMI(MBB, II, DL, get(RISCV::SLLI), ScaledRegister)
1865         .addReg(VL)
1866         .addImm(ShiftAmount)
1867         .setMIFlag(Flag);
1868     BuildMI(MBB, II, DL, get(RISCV::ADD), VL)
1869         .addReg(ScaledRegister, RegState::Kill)
1870         .addReg(VL, RegState::Kill)
1871         .setMIFlag(Flag);
1872   } else if (isPowerOf2_32(NumOfVReg + 1)) {
1873     Register ScaledRegister = MRI.createVirtualRegister(&RISCV::GPRRegClass);
1874     uint32_t ShiftAmount = Log2_32(NumOfVReg + 1);
1875     BuildMI(MBB, II, DL, get(RISCV::SLLI), ScaledRegister)
1876         .addReg(VL)
1877         .addImm(ShiftAmount)
1878         .setMIFlag(Flag);
1879     BuildMI(MBB, II, DL, get(RISCV::SUB), VL)
1880         .addReg(ScaledRegister, RegState::Kill)
1881         .addReg(VL, RegState::Kill)
1882         .setMIFlag(Flag);
1883   } else {
1884     Register N = MRI.createVirtualRegister(&RISCV::GPRRegClass);
1885     movImm(MBB, II, DL, N, NumOfVReg, Flag);
1886     if (!STI.hasStdExtM())
1887       MF.getFunction().getContext().diagnose(DiagnosticInfoUnsupported{
1888           MF.getFunction(),
1889           "M-extension must be enabled to calculate the vscaled size/offset."});
1890     BuildMI(MBB, II, DL, get(RISCV::MUL), VL)
1891         .addReg(VL, RegState::Kill)
1892         .addReg(N, RegState::Kill)
1893         .setMIFlag(Flag);
1894   }
1895 
1896   return VL;
1897 }
1898 
1899 // Returns true if this is the sext.w pattern, addiw rd, rs1, 0.
1900 bool RISCV::isSEXT_W(const MachineInstr &MI) {
1901   return MI.getOpcode() == RISCV::ADDIW && MI.getOperand(1).isReg() &&
1902          MI.getOperand(2).isImm() && MI.getOperand(2).getImm() == 0;
1903 }
1904 
1905 static bool isRVVWholeLoadStore(unsigned Opcode) {
1906   switch (Opcode) {
1907   default:
1908     return false;
1909   case RISCV::VS1R_V:
1910   case RISCV::VS2R_V:
1911   case RISCV::VS4R_V:
1912   case RISCV::VS8R_V:
1913   case RISCV::VL1RE8_V:
1914   case RISCV::VL2RE8_V:
1915   case RISCV::VL4RE8_V:
1916   case RISCV::VL8RE8_V:
1917   case RISCV::VL1RE16_V:
1918   case RISCV::VL2RE16_V:
1919   case RISCV::VL4RE16_V:
1920   case RISCV::VL8RE16_V:
1921   case RISCV::VL1RE32_V:
1922   case RISCV::VL2RE32_V:
1923   case RISCV::VL4RE32_V:
1924   case RISCV::VL8RE32_V:
1925   case RISCV::VL1RE64_V:
1926   case RISCV::VL2RE64_V:
1927   case RISCV::VL4RE64_V:
1928   case RISCV::VL8RE64_V:
1929     return true;
1930   }
1931 }
1932 
1933 bool RISCV::isRVVSpill(const MachineInstr &MI) {
1934   // RVV lacks any support for immediate addressing for stack addresses, so be
1935   // conservative.
1936   unsigned Opcode = MI.getOpcode();
1937   if (!RISCVVPseudosTable::getPseudoInfo(Opcode) &&
1938       !isRVVWholeLoadStore(Opcode) && !isRVVSpillForZvlsseg(Opcode))
1939     return false;
1940   return true;
1941 }
1942 
1943 Optional<std::pair<unsigned, unsigned>>
1944 RISCV::isRVVSpillForZvlsseg(unsigned Opcode) {
1945   switch (Opcode) {
1946   default:
1947     return None;
1948   case RISCV::PseudoVSPILL2_M1:
1949   case RISCV::PseudoVRELOAD2_M1:
1950     return std::make_pair(2u, 1u);
1951   case RISCV::PseudoVSPILL2_M2:
1952   case RISCV::PseudoVRELOAD2_M2:
1953     return std::make_pair(2u, 2u);
1954   case RISCV::PseudoVSPILL2_M4:
1955   case RISCV::PseudoVRELOAD2_M4:
1956     return std::make_pair(2u, 4u);
1957   case RISCV::PseudoVSPILL3_M1:
1958   case RISCV::PseudoVRELOAD3_M1:
1959     return std::make_pair(3u, 1u);
1960   case RISCV::PseudoVSPILL3_M2:
1961   case RISCV::PseudoVRELOAD3_M2:
1962     return std::make_pair(3u, 2u);
1963   case RISCV::PseudoVSPILL4_M1:
1964   case RISCV::PseudoVRELOAD4_M1:
1965     return std::make_pair(4u, 1u);
1966   case RISCV::PseudoVSPILL4_M2:
1967   case RISCV::PseudoVRELOAD4_M2:
1968     return std::make_pair(4u, 2u);
1969   case RISCV::PseudoVSPILL5_M1:
1970   case RISCV::PseudoVRELOAD5_M1:
1971     return std::make_pair(5u, 1u);
1972   case RISCV::PseudoVSPILL6_M1:
1973   case RISCV::PseudoVRELOAD6_M1:
1974     return std::make_pair(6u, 1u);
1975   case RISCV::PseudoVSPILL7_M1:
1976   case RISCV::PseudoVRELOAD7_M1:
1977     return std::make_pair(7u, 1u);
1978   case RISCV::PseudoVSPILL8_M1:
1979   case RISCV::PseudoVRELOAD8_M1:
1980     return std::make_pair(8u, 1u);
1981   }
1982 }
1983 
1984 bool RISCV::isFaultFirstLoad(const MachineInstr &MI) {
1985   return MI.getNumExplicitDefs() == 2 && MI.modifiesRegister(RISCV::VL) &&
1986          !MI.isInlineAsm();
1987 }
1988