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