xref: /freebsd/contrib/llvm-project/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===-- ARMBaseInstrInfo.cpp - ARM Instruction Information ----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains the Base ARM implementation of the TargetInstrInfo class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "ARMBaseInstrInfo.h"
140b57cec5SDimitry Andric #include "ARMBaseRegisterInfo.h"
150b57cec5SDimitry Andric #include "ARMConstantPoolValue.h"
160b57cec5SDimitry Andric #include "ARMFeatures.h"
170b57cec5SDimitry Andric #include "ARMHazardRecognizer.h"
180b57cec5SDimitry Andric #include "ARMMachineFunctionInfo.h"
190b57cec5SDimitry Andric #include "ARMSubtarget.h"
200b57cec5SDimitry Andric #include "MCTargetDesc/ARMAddressingModes.h"
210b57cec5SDimitry Andric #include "MCTargetDesc/ARMBaseInfo.h"
22e8d8bef9SDimitry Andric #include "MVETailPredUtils.h"
230b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
240b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
250b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h"
260b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
27bdd1243dSDimitry Andric #include "llvm/CodeGen/DFAPacketizer.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/LiveVariables.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/MachineConstantPool.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h"
365ffd83dbSDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
370b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
38bdd1243dSDimitry Andric #include "llvm/CodeGen/MachinePipeliner.h"
390b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
40e8d8bef9SDimitry Andric #include "llvm/CodeGen/MachineScheduler.h"
41e8d8bef9SDimitry Andric #include "llvm/CodeGen/MultiHazardRecognizer.h"
420b57cec5SDimitry Andric #include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
430b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGNodes.h"
440b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
450b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
460b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSchedule.h"
470b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
480b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
490b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
500b57cec5SDimitry Andric #include "llvm/IR/Function.h"
510b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
520b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
530b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
540b57cec5SDimitry Andric #include "llvm/MC/MCInstrItineraries.h"
550b57cec5SDimitry Andric #include "llvm/Support/BranchProbability.h"
560b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
570b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
580b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
590b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
600b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
610b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
620b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
6306c3fb27SDimitry Andric #include "llvm/TargetParser/Triple.h"
640b57cec5SDimitry Andric #include <algorithm>
650b57cec5SDimitry Andric #include <cassert>
660b57cec5SDimitry Andric #include <cstdint>
670b57cec5SDimitry Andric #include <iterator>
680b57cec5SDimitry Andric #include <new>
690b57cec5SDimitry Andric #include <utility>
700b57cec5SDimitry Andric #include <vector>
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric using namespace llvm;
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric #define DEBUG_TYPE "arm-instrinfo"
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric #define GET_INSTRINFO_CTOR_DTOR
770b57cec5SDimitry Andric #include "ARMGenInstrInfo.inc"
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric static cl::opt<bool>
800b57cec5SDimitry Andric EnableARM3Addr("enable-arm-3-addr-conv", cl::Hidden,
810b57cec5SDimitry Andric                cl::desc("Enable ARM 2-addr to 3-addr conv"));
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric /// ARM_MLxEntry - Record information about MLA / MLS instructions.
840b57cec5SDimitry Andric struct ARM_MLxEntry {
850b57cec5SDimitry Andric   uint16_t MLxOpc;     // MLA / MLS opcode
860b57cec5SDimitry Andric   uint16_t MulOpc;     // Expanded multiplication opcode
870b57cec5SDimitry Andric   uint16_t AddSubOpc;  // Expanded add / sub opcode
880b57cec5SDimitry Andric   bool NegAcc;         // True if the acc is negated before the add / sub.
890b57cec5SDimitry Andric   bool HasLane;        // True if instruction has an extra "lane" operand.
900b57cec5SDimitry Andric };
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric static const ARM_MLxEntry ARM_MLxTable[] = {
930b57cec5SDimitry Andric   // MLxOpc,          MulOpc,           AddSubOpc,       NegAcc, HasLane
940b57cec5SDimitry Andric   // fp scalar ops
950b57cec5SDimitry Andric   { ARM::VMLAS,       ARM::VMULS,       ARM::VADDS,      false,  false },
960b57cec5SDimitry Andric   { ARM::VMLSS,       ARM::VMULS,       ARM::VSUBS,      false,  false },
970b57cec5SDimitry Andric   { ARM::VMLAD,       ARM::VMULD,       ARM::VADDD,      false,  false },
980b57cec5SDimitry Andric   { ARM::VMLSD,       ARM::VMULD,       ARM::VSUBD,      false,  false },
990b57cec5SDimitry Andric   { ARM::VNMLAS,      ARM::VNMULS,      ARM::VSUBS,      true,   false },
1000b57cec5SDimitry Andric   { ARM::VNMLSS,      ARM::VMULS,       ARM::VSUBS,      true,   false },
1010b57cec5SDimitry Andric   { ARM::VNMLAD,      ARM::VNMULD,      ARM::VSUBD,      true,   false },
1020b57cec5SDimitry Andric   { ARM::VNMLSD,      ARM::VMULD,       ARM::VSUBD,      true,   false },
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   // fp SIMD ops
1050b57cec5SDimitry Andric   { ARM::VMLAfd,      ARM::VMULfd,      ARM::VADDfd,     false,  false },
1060b57cec5SDimitry Andric   { ARM::VMLSfd,      ARM::VMULfd,      ARM::VSUBfd,     false,  false },
1070b57cec5SDimitry Andric   { ARM::VMLAfq,      ARM::VMULfq,      ARM::VADDfq,     false,  false },
1080b57cec5SDimitry Andric   { ARM::VMLSfq,      ARM::VMULfq,      ARM::VSUBfq,     false,  false },
1090b57cec5SDimitry Andric   { ARM::VMLAslfd,    ARM::VMULslfd,    ARM::VADDfd,     false,  true  },
1100b57cec5SDimitry Andric   { ARM::VMLSslfd,    ARM::VMULslfd,    ARM::VSUBfd,     false,  true  },
1110b57cec5SDimitry Andric   { ARM::VMLAslfq,    ARM::VMULslfq,    ARM::VADDfq,     false,  true  },
1120b57cec5SDimitry Andric   { ARM::VMLSslfq,    ARM::VMULslfq,    ARM::VSUBfq,     false,  true  },
1130b57cec5SDimitry Andric };
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric ARMBaseInstrInfo::ARMBaseInstrInfo(const ARMSubtarget& STI)
1160b57cec5SDimitry Andric   : ARMGenInstrInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
1170b57cec5SDimitry Andric     Subtarget(STI) {
118bdd1243dSDimitry Andric   for (unsigned i = 0, e = std::size(ARM_MLxTable); i != e; ++i) {
1190b57cec5SDimitry Andric     if (!MLxEntryMap.insert(std::make_pair(ARM_MLxTable[i].MLxOpc, i)).second)
1200b57cec5SDimitry Andric       llvm_unreachable("Duplicated entries?");
1210b57cec5SDimitry Andric     MLxHazardOpcodes.insert(ARM_MLxTable[i].AddSubOpc);
1220b57cec5SDimitry Andric     MLxHazardOpcodes.insert(ARM_MLxTable[i].MulOpc);
1230b57cec5SDimitry Andric   }
1240b57cec5SDimitry Andric }
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric // Use a ScoreboardHazardRecognizer for prepass ARM scheduling. TargetInstrImpl
1270b57cec5SDimitry Andric // currently defaults to no prepass hazard recognizer.
1280b57cec5SDimitry Andric ScheduleHazardRecognizer *
1290b57cec5SDimitry Andric ARMBaseInstrInfo::CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
1300b57cec5SDimitry Andric                                                const ScheduleDAG *DAG) const {
1310b57cec5SDimitry Andric   if (usePreRAHazardRecognizer()) {
1320b57cec5SDimitry Andric     const InstrItineraryData *II =
1330b57cec5SDimitry Andric         static_cast<const ARMSubtarget *>(STI)->getInstrItineraryData();
1340b57cec5SDimitry Andric     return new ScoreboardHazardRecognizer(II, DAG, "pre-RA-sched");
1350b57cec5SDimitry Andric   }
1360b57cec5SDimitry Andric   return TargetInstrInfo::CreateTargetHazardRecognizer(STI, DAG);
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric 
139e8d8bef9SDimitry Andric // Called during:
140e8d8bef9SDimitry Andric // - pre-RA scheduling
141e8d8bef9SDimitry Andric // - post-RA scheduling when FeatureUseMISched is set
142e8d8bef9SDimitry Andric ScheduleHazardRecognizer *ARMBaseInstrInfo::CreateTargetMIHazardRecognizer(
143e8d8bef9SDimitry Andric     const InstrItineraryData *II, const ScheduleDAGMI *DAG) const {
144e8d8bef9SDimitry Andric   MultiHazardRecognizer *MHR = new MultiHazardRecognizer();
145e8d8bef9SDimitry Andric 
146e8d8bef9SDimitry Andric   // We would like to restrict this hazard recognizer to only
147e8d8bef9SDimitry Andric   // post-RA scheduling; we can tell that we're post-RA because we don't
148e8d8bef9SDimitry Andric   // track VRegLiveness.
149e8d8bef9SDimitry Andric   // Cortex-M7: TRM indicates that there is a single ITCM bank and two DTCM
150e8d8bef9SDimitry Andric   //            banks banked on bit 2.  Assume that TCMs are in use.
151e8d8bef9SDimitry Andric   if (Subtarget.isCortexM7() && !DAG->hasVRegLiveness())
152e8d8bef9SDimitry Andric     MHR->AddHazardRecognizer(
153e8d8bef9SDimitry Andric         std::make_unique<ARMBankConflictHazardRecognizer>(DAG, 0x4, true));
154e8d8bef9SDimitry Andric 
155e8d8bef9SDimitry Andric   // Not inserting ARMHazardRecognizerFPMLx because that would change
156e8d8bef9SDimitry Andric   // legacy behavior
157e8d8bef9SDimitry Andric 
158e8d8bef9SDimitry Andric   auto BHR = TargetInstrInfo::CreateTargetMIHazardRecognizer(II, DAG);
159e8d8bef9SDimitry Andric   MHR->AddHazardRecognizer(std::unique_ptr<ScheduleHazardRecognizer>(BHR));
160e8d8bef9SDimitry Andric   return MHR;
161e8d8bef9SDimitry Andric }
162e8d8bef9SDimitry Andric 
163e8d8bef9SDimitry Andric // Called during post-RA scheduling when FeatureUseMISched is not set
1640b57cec5SDimitry Andric ScheduleHazardRecognizer *ARMBaseInstrInfo::
1650b57cec5SDimitry Andric CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
1660b57cec5SDimitry Andric                                    const ScheduleDAG *DAG) const {
167e8d8bef9SDimitry Andric   MultiHazardRecognizer *MHR = new MultiHazardRecognizer();
168e8d8bef9SDimitry Andric 
1690b57cec5SDimitry Andric   if (Subtarget.isThumb2() || Subtarget.hasVFP2Base())
170e8d8bef9SDimitry Andric     MHR->AddHazardRecognizer(std::make_unique<ARMHazardRecognizerFPMLx>());
171e8d8bef9SDimitry Andric 
172e8d8bef9SDimitry Andric   auto BHR = TargetInstrInfo::CreateTargetPostRAHazardRecognizer(II, DAG);
173e8d8bef9SDimitry Andric   if (BHR)
174e8d8bef9SDimitry Andric     MHR->AddHazardRecognizer(std::unique_ptr<ScheduleHazardRecognizer>(BHR));
175e8d8bef9SDimitry Andric   return MHR;
1760b57cec5SDimitry Andric }
1770b57cec5SDimitry Andric 
178349cc55cSDimitry Andric MachineInstr *
179349cc55cSDimitry Andric ARMBaseInstrInfo::convertToThreeAddress(MachineInstr &MI, LiveVariables *LV,
180349cc55cSDimitry Andric                                         LiveIntervals *LIS) const {
1810b57cec5SDimitry Andric   // FIXME: Thumb2 support.
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   if (!EnableARM3Addr)
1840b57cec5SDimitry Andric     return nullptr;
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric   MachineFunction &MF = *MI.getParent()->getParent();
1870b57cec5SDimitry Andric   uint64_t TSFlags = MI.getDesc().TSFlags;
1880b57cec5SDimitry Andric   bool isPre = false;
1890b57cec5SDimitry Andric   switch ((TSFlags & ARMII::IndexModeMask) >> ARMII::IndexModeShift) {
1900b57cec5SDimitry Andric   default: return nullptr;
1910b57cec5SDimitry Andric   case ARMII::IndexModePre:
1920b57cec5SDimitry Andric     isPre = true;
1930b57cec5SDimitry Andric     break;
1940b57cec5SDimitry Andric   case ARMII::IndexModePost:
1950b57cec5SDimitry Andric     break;
1960b57cec5SDimitry Andric   }
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric   // Try splitting an indexed load/store to an un-indexed one plus an add/sub
1990b57cec5SDimitry Andric   // operation.
2000b57cec5SDimitry Andric   unsigned MemOpc = getUnindexedOpcode(MI.getOpcode());
2010b57cec5SDimitry Andric   if (MemOpc == 0)
2020b57cec5SDimitry Andric     return nullptr;
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric   MachineInstr *UpdateMI = nullptr;
2050b57cec5SDimitry Andric   MachineInstr *MemMI = nullptr;
2060b57cec5SDimitry Andric   unsigned AddrMode = (TSFlags & ARMII::AddrModeMask);
2070b57cec5SDimitry Andric   const MCInstrDesc &MCID = MI.getDesc();
2080b57cec5SDimitry Andric   unsigned NumOps = MCID.getNumOperands();
2090b57cec5SDimitry Andric   bool isLoad = !MI.mayStore();
2100b57cec5SDimitry Andric   const MachineOperand &WB = isLoad ? MI.getOperand(1) : MI.getOperand(0);
2110b57cec5SDimitry Andric   const MachineOperand &Base = MI.getOperand(2);
2120b57cec5SDimitry Andric   const MachineOperand &Offset = MI.getOperand(NumOps - 3);
2138bcb0991SDimitry Andric   Register WBReg = WB.getReg();
2148bcb0991SDimitry Andric   Register BaseReg = Base.getReg();
2158bcb0991SDimitry Andric   Register OffReg = Offset.getReg();
2160b57cec5SDimitry Andric   unsigned OffImm = MI.getOperand(NumOps - 2).getImm();
2170b57cec5SDimitry Andric   ARMCC::CondCodes Pred = (ARMCC::CondCodes)MI.getOperand(NumOps - 1).getImm();
2180b57cec5SDimitry Andric   switch (AddrMode) {
2190b57cec5SDimitry Andric   default: llvm_unreachable("Unknown indexed op!");
2200b57cec5SDimitry Andric   case ARMII::AddrMode2: {
2210b57cec5SDimitry Andric     bool isSub = ARM_AM::getAM2Op(OffImm) == ARM_AM::sub;
2220b57cec5SDimitry Andric     unsigned Amt = ARM_AM::getAM2Offset(OffImm);
2230b57cec5SDimitry Andric     if (OffReg == 0) {
2240b57cec5SDimitry Andric       if (ARM_AM::getSOImmVal(Amt) == -1)
2250b57cec5SDimitry Andric         // Can't encode it in a so_imm operand. This transformation will
2260b57cec5SDimitry Andric         // add more than 1 instruction. Abandon!
2270b57cec5SDimitry Andric         return nullptr;
2280b57cec5SDimitry Andric       UpdateMI = BuildMI(MF, MI.getDebugLoc(),
2290b57cec5SDimitry Andric                          get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
2300b57cec5SDimitry Andric                      .addReg(BaseReg)
2310b57cec5SDimitry Andric                      .addImm(Amt)
2320b57cec5SDimitry Andric                      .add(predOps(Pred))
2330b57cec5SDimitry Andric                      .add(condCodeOp());
2340b57cec5SDimitry Andric     } else if (Amt != 0) {
2350b57cec5SDimitry Andric       ARM_AM::ShiftOpc ShOpc = ARM_AM::getAM2ShiftOpc(OffImm);
2360b57cec5SDimitry Andric       unsigned SOOpc = ARM_AM::getSORegOpc(ShOpc, Amt);
2370b57cec5SDimitry Andric       UpdateMI = BuildMI(MF, MI.getDebugLoc(),
2380b57cec5SDimitry Andric                          get(isSub ? ARM::SUBrsi : ARM::ADDrsi), WBReg)
2390b57cec5SDimitry Andric                      .addReg(BaseReg)
2400b57cec5SDimitry Andric                      .addReg(OffReg)
2410b57cec5SDimitry Andric                      .addReg(0)
2420b57cec5SDimitry Andric                      .addImm(SOOpc)
2430b57cec5SDimitry Andric                      .add(predOps(Pred))
2440b57cec5SDimitry Andric                      .add(condCodeOp());
2450b57cec5SDimitry Andric     } else
2460b57cec5SDimitry Andric       UpdateMI = BuildMI(MF, MI.getDebugLoc(),
2470b57cec5SDimitry Andric                          get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
2480b57cec5SDimitry Andric                      .addReg(BaseReg)
2490b57cec5SDimitry Andric                      .addReg(OffReg)
2500b57cec5SDimitry Andric                      .add(predOps(Pred))
2510b57cec5SDimitry Andric                      .add(condCodeOp());
2520b57cec5SDimitry Andric     break;
2530b57cec5SDimitry Andric   }
2540b57cec5SDimitry Andric   case ARMII::AddrMode3 : {
2550b57cec5SDimitry Andric     bool isSub = ARM_AM::getAM3Op(OffImm) == ARM_AM::sub;
2560b57cec5SDimitry Andric     unsigned Amt = ARM_AM::getAM3Offset(OffImm);
2570b57cec5SDimitry Andric     if (OffReg == 0)
2580b57cec5SDimitry Andric       // Immediate is 8-bits. It's guaranteed to fit in a so_imm operand.
2590b57cec5SDimitry Andric       UpdateMI = BuildMI(MF, MI.getDebugLoc(),
2600b57cec5SDimitry Andric                          get(isSub ? ARM::SUBri : ARM::ADDri), WBReg)
2610b57cec5SDimitry Andric                      .addReg(BaseReg)
2620b57cec5SDimitry Andric                      .addImm(Amt)
2630b57cec5SDimitry Andric                      .add(predOps(Pred))
2640b57cec5SDimitry Andric                      .add(condCodeOp());
2650b57cec5SDimitry Andric     else
2660b57cec5SDimitry Andric       UpdateMI = BuildMI(MF, MI.getDebugLoc(),
2670b57cec5SDimitry Andric                          get(isSub ? ARM::SUBrr : ARM::ADDrr), WBReg)
2680b57cec5SDimitry Andric                      .addReg(BaseReg)
2690b57cec5SDimitry Andric                      .addReg(OffReg)
2700b57cec5SDimitry Andric                      .add(predOps(Pred))
2710b57cec5SDimitry Andric                      .add(condCodeOp());
2720b57cec5SDimitry Andric     break;
2730b57cec5SDimitry Andric   }
2740b57cec5SDimitry Andric   }
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric   std::vector<MachineInstr*> NewMIs;
2770b57cec5SDimitry Andric   if (isPre) {
2780b57cec5SDimitry Andric     if (isLoad)
2790b57cec5SDimitry Andric       MemMI =
2800b57cec5SDimitry Andric           BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
2810b57cec5SDimitry Andric               .addReg(WBReg)
2820b57cec5SDimitry Andric               .addImm(0)
2830b57cec5SDimitry Andric               .addImm(Pred);
2840b57cec5SDimitry Andric     else
2850b57cec5SDimitry Andric       MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
2860b57cec5SDimitry Andric                   .addReg(MI.getOperand(1).getReg())
2870b57cec5SDimitry Andric                   .addReg(WBReg)
2880b57cec5SDimitry Andric                   .addReg(0)
2890b57cec5SDimitry Andric                   .addImm(0)
2900b57cec5SDimitry Andric                   .addImm(Pred);
2910b57cec5SDimitry Andric     NewMIs.push_back(MemMI);
2920b57cec5SDimitry Andric     NewMIs.push_back(UpdateMI);
2930b57cec5SDimitry Andric   } else {
2940b57cec5SDimitry Andric     if (isLoad)
2950b57cec5SDimitry Andric       MemMI =
2960b57cec5SDimitry Andric           BuildMI(MF, MI.getDebugLoc(), get(MemOpc), MI.getOperand(0).getReg())
2970b57cec5SDimitry Andric               .addReg(BaseReg)
2980b57cec5SDimitry Andric               .addImm(0)
2990b57cec5SDimitry Andric               .addImm(Pred);
3000b57cec5SDimitry Andric     else
3010b57cec5SDimitry Andric       MemMI = BuildMI(MF, MI.getDebugLoc(), get(MemOpc))
3020b57cec5SDimitry Andric                   .addReg(MI.getOperand(1).getReg())
3030b57cec5SDimitry Andric                   .addReg(BaseReg)
3040b57cec5SDimitry Andric                   .addReg(0)
3050b57cec5SDimitry Andric                   .addImm(0)
3060b57cec5SDimitry Andric                   .addImm(Pred);
3070b57cec5SDimitry Andric     if (WB.isDead())
3080b57cec5SDimitry Andric       UpdateMI->getOperand(0).setIsDead();
3090b57cec5SDimitry Andric     NewMIs.push_back(UpdateMI);
3100b57cec5SDimitry Andric     NewMIs.push_back(MemMI);
3110b57cec5SDimitry Andric   }
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric   // Transfer LiveVariables states, kill / dead info.
3140b57cec5SDimitry Andric   if (LV) {
3154824e7fdSDimitry Andric     for (const MachineOperand &MO : MI.operands()) {
316bdd1243dSDimitry Andric       if (MO.isReg() && MO.getReg().isVirtual()) {
3178bcb0991SDimitry Andric         Register Reg = MO.getReg();
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric         LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
3200b57cec5SDimitry Andric         if (MO.isDef()) {
3210b57cec5SDimitry Andric           MachineInstr *NewMI = (Reg == WBReg) ? UpdateMI : MemMI;
3220b57cec5SDimitry Andric           if (MO.isDead())
3230b57cec5SDimitry Andric             LV->addVirtualRegisterDead(Reg, *NewMI);
3240b57cec5SDimitry Andric         }
3250b57cec5SDimitry Andric         if (MO.isUse() && MO.isKill()) {
3260b57cec5SDimitry Andric           for (unsigned j = 0; j < 2; ++j) {
3270b57cec5SDimitry Andric             // Look at the two new MI's in reverse order.
3280b57cec5SDimitry Andric             MachineInstr *NewMI = NewMIs[j];
3290b57cec5SDimitry Andric             if (!NewMI->readsRegister(Reg))
3300b57cec5SDimitry Andric               continue;
3310b57cec5SDimitry Andric             LV->addVirtualRegisterKilled(Reg, *NewMI);
3320b57cec5SDimitry Andric             if (VI.removeKill(MI))
3330b57cec5SDimitry Andric               VI.Kills.push_back(NewMI);
3340b57cec5SDimitry Andric             break;
3350b57cec5SDimitry Andric           }
3360b57cec5SDimitry Andric         }
3370b57cec5SDimitry Andric       }
3380b57cec5SDimitry Andric     }
3390b57cec5SDimitry Andric   }
3400b57cec5SDimitry Andric 
341349cc55cSDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
342349cc55cSDimitry Andric   MBB.insert(MI, NewMIs[1]);
343349cc55cSDimitry Andric   MBB.insert(MI, NewMIs[0]);
3440b57cec5SDimitry Andric   return NewMIs[0];
3450b57cec5SDimitry Andric }
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric // Branch analysis.
34881ad6265SDimitry Andric // Cond vector output format:
34981ad6265SDimitry Andric //   0 elements indicates an unconditional branch
35081ad6265SDimitry Andric //   2 elements indicates a conditional branch; the elements are
35181ad6265SDimitry Andric //     the condition to check and the CPSR.
35281ad6265SDimitry Andric //   3 elements indicates a hardware loop end; the elements
35381ad6265SDimitry Andric //     are the opcode, the operand value to test, and a dummy
35481ad6265SDimitry Andric //     operand used to pad out to 3 operands.
3550b57cec5SDimitry Andric bool ARMBaseInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
3560b57cec5SDimitry Andric                                      MachineBasicBlock *&TBB,
3570b57cec5SDimitry Andric                                      MachineBasicBlock *&FBB,
3580b57cec5SDimitry Andric                                      SmallVectorImpl<MachineOperand> &Cond,
3590b57cec5SDimitry Andric                                      bool AllowModify) const {
3600b57cec5SDimitry Andric   TBB = nullptr;
3610b57cec5SDimitry Andric   FBB = nullptr;
3620b57cec5SDimitry Andric 
363e8d8bef9SDimitry Andric   MachineBasicBlock::instr_iterator I = MBB.instr_end();
364e8d8bef9SDimitry Andric   if (I == MBB.instr_begin())
3650b57cec5SDimitry Andric     return false; // Empty blocks are easy.
3660b57cec5SDimitry Andric   --I;
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric   // Walk backwards from the end of the basic block until the branch is
3690b57cec5SDimitry Andric   // analyzed or we give up.
3700b57cec5SDimitry Andric   while (isPredicated(*I) || I->isTerminator() || I->isDebugValue()) {
3710b57cec5SDimitry Andric     // Flag to be raised on unanalyzeable instructions. This is useful in cases
3720b57cec5SDimitry Andric     // where we want to clean up on the end of the basic block before we bail
3730b57cec5SDimitry Andric     // out.
3740b57cec5SDimitry Andric     bool CantAnalyze = false;
3750b57cec5SDimitry Andric 
376e8d8bef9SDimitry Andric     // Skip over DEBUG values, predicated nonterminators and speculation
377e8d8bef9SDimitry Andric     // barrier terminators.
378e8d8bef9SDimitry Andric     while (I->isDebugInstr() || !I->isTerminator() ||
379e8d8bef9SDimitry Andric            isSpeculationBarrierEndBBOpcode(I->getOpcode()) ||
380e8d8bef9SDimitry Andric            I->getOpcode() == ARM::t2DoLoopStartTP){
381e8d8bef9SDimitry Andric       if (I == MBB.instr_begin())
3820b57cec5SDimitry Andric         return false;
3830b57cec5SDimitry Andric       --I;
3840b57cec5SDimitry Andric     }
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric     if (isIndirectBranchOpcode(I->getOpcode()) ||
3870b57cec5SDimitry Andric         isJumpTableBranchOpcode(I->getOpcode())) {
3880b57cec5SDimitry Andric       // Indirect branches and jump tables can't be analyzed, but we still want
3890b57cec5SDimitry Andric       // to clean up any instructions at the tail of the basic block.
3900b57cec5SDimitry Andric       CantAnalyze = true;
3910b57cec5SDimitry Andric     } else if (isUncondBranchOpcode(I->getOpcode())) {
3920b57cec5SDimitry Andric       TBB = I->getOperand(0).getMBB();
3930b57cec5SDimitry Andric     } else if (isCondBranchOpcode(I->getOpcode())) {
3940b57cec5SDimitry Andric       // Bail out if we encounter multiple conditional branches.
3950b57cec5SDimitry Andric       if (!Cond.empty())
3960b57cec5SDimitry Andric         return true;
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric       assert(!FBB && "FBB should have been null.");
3990b57cec5SDimitry Andric       FBB = TBB;
4000b57cec5SDimitry Andric       TBB = I->getOperand(0).getMBB();
4010b57cec5SDimitry Andric       Cond.push_back(I->getOperand(1));
4020b57cec5SDimitry Andric       Cond.push_back(I->getOperand(2));
4030b57cec5SDimitry Andric     } else if (I->isReturn()) {
4040b57cec5SDimitry Andric       // Returns can't be analyzed, but we should run cleanup.
405e8d8bef9SDimitry Andric       CantAnalyze = true;
40681ad6265SDimitry Andric     } else if (I->getOpcode() == ARM::t2LoopEnd &&
40781ad6265SDimitry Andric                MBB.getParent()
40881ad6265SDimitry Andric                    ->getSubtarget<ARMSubtarget>()
40981ad6265SDimitry Andric                    .enableMachinePipeliner()) {
41081ad6265SDimitry Andric       if (!Cond.empty())
41181ad6265SDimitry Andric         return true;
41281ad6265SDimitry Andric       FBB = TBB;
41381ad6265SDimitry Andric       TBB = I->getOperand(1).getMBB();
41481ad6265SDimitry Andric       Cond.push_back(MachineOperand::CreateImm(I->getOpcode()));
41581ad6265SDimitry Andric       Cond.push_back(I->getOperand(0));
41681ad6265SDimitry Andric       Cond.push_back(MachineOperand::CreateImm(0));
4170b57cec5SDimitry Andric     } else {
4180b57cec5SDimitry Andric       // We encountered other unrecognized terminator. Bail out immediately.
4190b57cec5SDimitry Andric       return true;
4200b57cec5SDimitry Andric     }
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric     // Cleanup code - to be run for unpredicated unconditional branches and
4230b57cec5SDimitry Andric     //                returns.
4240b57cec5SDimitry Andric     if (!isPredicated(*I) &&
4250b57cec5SDimitry Andric           (isUncondBranchOpcode(I->getOpcode()) ||
4260b57cec5SDimitry Andric            isIndirectBranchOpcode(I->getOpcode()) ||
4270b57cec5SDimitry Andric            isJumpTableBranchOpcode(I->getOpcode()) ||
4280b57cec5SDimitry Andric            I->isReturn())) {
4290b57cec5SDimitry Andric       // Forget any previous condition branch information - it no longer applies.
4300b57cec5SDimitry Andric       Cond.clear();
4310b57cec5SDimitry Andric       FBB = nullptr;
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric       // If we can modify the function, delete everything below this
4340b57cec5SDimitry Andric       // unconditional branch.
4350b57cec5SDimitry Andric       if (AllowModify) {
4360b57cec5SDimitry Andric         MachineBasicBlock::iterator DI = std::next(I);
437e8d8bef9SDimitry Andric         while (DI != MBB.instr_end()) {
4380b57cec5SDimitry Andric           MachineInstr &InstToDelete = *DI;
4390b57cec5SDimitry Andric           ++DI;
440e8d8bef9SDimitry Andric           // Speculation barriers must not be deleted.
441e8d8bef9SDimitry Andric           if (isSpeculationBarrierEndBBOpcode(InstToDelete.getOpcode()))
442e8d8bef9SDimitry Andric             continue;
4430b57cec5SDimitry Andric           InstToDelete.eraseFromParent();
4440b57cec5SDimitry Andric         }
4450b57cec5SDimitry Andric       }
4460b57cec5SDimitry Andric     }
4470b57cec5SDimitry Andric 
448e8d8bef9SDimitry Andric     if (CantAnalyze) {
449e8d8bef9SDimitry Andric       // We may not be able to analyze the block, but we could still have
450e8d8bef9SDimitry Andric       // an unconditional branch as the last instruction in the block, which
451e8d8bef9SDimitry Andric       // just branches to layout successor. If this is the case, then just
452e8d8bef9SDimitry Andric       // remove it if we're allowed to make modifications.
453e8d8bef9SDimitry Andric       if (AllowModify && !isPredicated(MBB.back()) &&
454e8d8bef9SDimitry Andric           isUncondBranchOpcode(MBB.back().getOpcode()) &&
455e8d8bef9SDimitry Andric           TBB && MBB.isLayoutSuccessor(TBB))
456e8d8bef9SDimitry Andric         removeBranch(MBB);
4570b57cec5SDimitry Andric       return true;
458e8d8bef9SDimitry Andric     }
4590b57cec5SDimitry Andric 
460e8d8bef9SDimitry Andric     if (I == MBB.instr_begin())
4610b57cec5SDimitry Andric       return false;
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric     --I;
4640b57cec5SDimitry Andric   }
4650b57cec5SDimitry Andric 
4660b57cec5SDimitry Andric   // We made it past the terminators without bailing out - we must have
4670b57cec5SDimitry Andric   // analyzed this branch successfully.
4680b57cec5SDimitry Andric   return false;
4690b57cec5SDimitry Andric }
4700b57cec5SDimitry Andric 
4710b57cec5SDimitry Andric unsigned ARMBaseInstrInfo::removeBranch(MachineBasicBlock &MBB,
4720b57cec5SDimitry Andric                                         int *BytesRemoved) const {
4730b57cec5SDimitry Andric   assert(!BytesRemoved && "code size not handled");
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric   MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
4760b57cec5SDimitry Andric   if (I == MBB.end())
4770b57cec5SDimitry Andric     return 0;
4780b57cec5SDimitry Andric 
4790b57cec5SDimitry Andric   if (!isUncondBranchOpcode(I->getOpcode()) &&
48081ad6265SDimitry Andric       !isCondBranchOpcode(I->getOpcode()) && I->getOpcode() != ARM::t2LoopEnd)
4810b57cec5SDimitry Andric     return 0;
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric   // Remove the branch.
4840b57cec5SDimitry Andric   I->eraseFromParent();
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric   I = MBB.end();
4870b57cec5SDimitry Andric 
4880b57cec5SDimitry Andric   if (I == MBB.begin()) return 1;
4890b57cec5SDimitry Andric   --I;
49081ad6265SDimitry Andric   if (!isCondBranchOpcode(I->getOpcode()) && I->getOpcode() != ARM::t2LoopEnd)
4910b57cec5SDimitry Andric     return 1;
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric   // Remove the branch.
4940b57cec5SDimitry Andric   I->eraseFromParent();
4950b57cec5SDimitry Andric   return 2;
4960b57cec5SDimitry Andric }
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric unsigned ARMBaseInstrInfo::insertBranch(MachineBasicBlock &MBB,
4990b57cec5SDimitry Andric                                         MachineBasicBlock *TBB,
5000b57cec5SDimitry Andric                                         MachineBasicBlock *FBB,
5010b57cec5SDimitry Andric                                         ArrayRef<MachineOperand> Cond,
5020b57cec5SDimitry Andric                                         const DebugLoc &DL,
5030b57cec5SDimitry Andric                                         int *BytesAdded) const {
5040b57cec5SDimitry Andric   assert(!BytesAdded && "code size not handled");
5050b57cec5SDimitry Andric   ARMFunctionInfo *AFI = MBB.getParent()->getInfo<ARMFunctionInfo>();
5060b57cec5SDimitry Andric   int BOpc   = !AFI->isThumbFunction()
5070b57cec5SDimitry Andric     ? ARM::B : (AFI->isThumb2Function() ? ARM::t2B : ARM::tB);
5080b57cec5SDimitry Andric   int BccOpc = !AFI->isThumbFunction()
5090b57cec5SDimitry Andric     ? ARM::Bcc : (AFI->isThumb2Function() ? ARM::t2Bcc : ARM::tBcc);
5100b57cec5SDimitry Andric   bool isThumb = AFI->isThumbFunction() || AFI->isThumb2Function();
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric   // Shouldn't be a fall through.
5130b57cec5SDimitry Andric   assert(TBB && "insertBranch must not be told to insert a fallthrough");
51481ad6265SDimitry Andric   assert((Cond.size() == 2 || Cond.size() == 0 || Cond.size() == 3) &&
51581ad6265SDimitry Andric          "ARM branch conditions have two or three components!");
5160b57cec5SDimitry Andric 
5170b57cec5SDimitry Andric   // For conditional branches, we use addOperand to preserve CPSR flags.
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric   if (!FBB) {
5200b57cec5SDimitry Andric     if (Cond.empty()) { // Unconditional branch?
5210b57cec5SDimitry Andric       if (isThumb)
5220b57cec5SDimitry Andric         BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB).add(predOps(ARMCC::AL));
5230b57cec5SDimitry Andric       else
5240b57cec5SDimitry Andric         BuildMI(&MBB, DL, get(BOpc)).addMBB(TBB);
52581ad6265SDimitry Andric     } else if (Cond.size() == 2) {
5260b57cec5SDimitry Andric       BuildMI(&MBB, DL, get(BccOpc))
5270b57cec5SDimitry Andric           .addMBB(TBB)
5280b57cec5SDimitry Andric           .addImm(Cond[0].getImm())
5290b57cec5SDimitry Andric           .add(Cond[1]);
53081ad6265SDimitry Andric     } else
53181ad6265SDimitry Andric       BuildMI(&MBB, DL, get(Cond[0].getImm())).add(Cond[1]).addMBB(TBB);
5320b57cec5SDimitry Andric     return 1;
5330b57cec5SDimitry Andric   }
5340b57cec5SDimitry Andric 
5350b57cec5SDimitry Andric   // Two-way conditional branch.
53681ad6265SDimitry Andric   if (Cond.size() == 2)
5370b57cec5SDimitry Andric     BuildMI(&MBB, DL, get(BccOpc))
5380b57cec5SDimitry Andric         .addMBB(TBB)
5390b57cec5SDimitry Andric         .addImm(Cond[0].getImm())
5400b57cec5SDimitry Andric         .add(Cond[1]);
54181ad6265SDimitry Andric   else if (Cond.size() == 3)
54281ad6265SDimitry Andric     BuildMI(&MBB, DL, get(Cond[0].getImm())).add(Cond[1]).addMBB(TBB);
5430b57cec5SDimitry Andric   if (isThumb)
5440b57cec5SDimitry Andric     BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB).add(predOps(ARMCC::AL));
5450b57cec5SDimitry Andric   else
5460b57cec5SDimitry Andric     BuildMI(&MBB, DL, get(BOpc)).addMBB(FBB);
5470b57cec5SDimitry Andric   return 2;
5480b57cec5SDimitry Andric }
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric bool ARMBaseInstrInfo::
5510b57cec5SDimitry Andric reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
55281ad6265SDimitry Andric   if (Cond.size() == 2) {
5530b57cec5SDimitry Andric     ARMCC::CondCodes CC = (ARMCC::CondCodes)(int)Cond[0].getImm();
5540b57cec5SDimitry Andric     Cond[0].setImm(ARMCC::getOppositeCondition(CC));
5550b57cec5SDimitry Andric     return false;
5560b57cec5SDimitry Andric   }
55781ad6265SDimitry Andric   return true;
55881ad6265SDimitry Andric }
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric bool ARMBaseInstrInfo::isPredicated(const MachineInstr &MI) const {
5610b57cec5SDimitry Andric   if (MI.isBundle()) {
5620b57cec5SDimitry Andric     MachineBasicBlock::const_instr_iterator I = MI.getIterator();
5630b57cec5SDimitry Andric     MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
5640b57cec5SDimitry Andric     while (++I != E && I->isInsideBundle()) {
5650b57cec5SDimitry Andric       int PIdx = I->findFirstPredOperandIdx();
5660b57cec5SDimitry Andric       if (PIdx != -1 && I->getOperand(PIdx).getImm() != ARMCC::AL)
5670b57cec5SDimitry Andric         return true;
5680b57cec5SDimitry Andric     }
5690b57cec5SDimitry Andric     return false;
5700b57cec5SDimitry Andric   }
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric   int PIdx = MI.findFirstPredOperandIdx();
5730b57cec5SDimitry Andric   return PIdx != -1 && MI.getOperand(PIdx).getImm() != ARMCC::AL;
5740b57cec5SDimitry Andric }
5750b57cec5SDimitry Andric 
5765ffd83dbSDimitry Andric std::string ARMBaseInstrInfo::createMIROperandComment(
5775ffd83dbSDimitry Andric     const MachineInstr &MI, const MachineOperand &Op, unsigned OpIdx,
5785ffd83dbSDimitry Andric     const TargetRegisterInfo *TRI) const {
5795ffd83dbSDimitry Andric 
5805ffd83dbSDimitry Andric   // First, let's see if there is a generic comment for this operand
5815ffd83dbSDimitry Andric   std::string GenericComment =
5825ffd83dbSDimitry Andric       TargetInstrInfo::createMIROperandComment(MI, Op, OpIdx, TRI);
5835ffd83dbSDimitry Andric   if (!GenericComment.empty())
5845ffd83dbSDimitry Andric     return GenericComment;
5855ffd83dbSDimitry Andric 
5865ffd83dbSDimitry Andric   // If not, check if we have an immediate operand.
58781ad6265SDimitry Andric   if (!Op.isImm())
5885ffd83dbSDimitry Andric     return std::string();
5895ffd83dbSDimitry Andric 
5905ffd83dbSDimitry Andric   // And print its corresponding condition code if the immediate is a
5915ffd83dbSDimitry Andric   // predicate.
5925ffd83dbSDimitry Andric   int FirstPredOp = MI.findFirstPredOperandIdx();
5935ffd83dbSDimitry Andric   if (FirstPredOp != (int) OpIdx)
5945ffd83dbSDimitry Andric     return std::string();
5955ffd83dbSDimitry Andric 
5965ffd83dbSDimitry Andric   std::string CC = "CC::";
5975ffd83dbSDimitry Andric   CC += ARMCondCodeToString((ARMCC::CondCodes)Op.getImm());
5985ffd83dbSDimitry Andric   return CC;
5995ffd83dbSDimitry Andric }
6005ffd83dbSDimitry Andric 
6010b57cec5SDimitry Andric bool ARMBaseInstrInfo::PredicateInstruction(
6020b57cec5SDimitry Andric     MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
6030b57cec5SDimitry Andric   unsigned Opc = MI.getOpcode();
6040b57cec5SDimitry Andric   if (isUncondBranchOpcode(Opc)) {
6050b57cec5SDimitry Andric     MI.setDesc(get(getMatchingCondBranchOpcode(Opc)));
6060b57cec5SDimitry Andric     MachineInstrBuilder(*MI.getParent()->getParent(), MI)
6070b57cec5SDimitry Andric       .addImm(Pred[0].getImm())
6080b57cec5SDimitry Andric       .addReg(Pred[1].getReg());
6090b57cec5SDimitry Andric     return true;
6100b57cec5SDimitry Andric   }
6110b57cec5SDimitry Andric 
6120b57cec5SDimitry Andric   int PIdx = MI.findFirstPredOperandIdx();
6130b57cec5SDimitry Andric   if (PIdx != -1) {
6140b57cec5SDimitry Andric     MachineOperand &PMO = MI.getOperand(PIdx);
6150b57cec5SDimitry Andric     PMO.setImm(Pred[0].getImm());
6160b57cec5SDimitry Andric     MI.getOperand(PIdx+1).setReg(Pred[1].getReg());
617e8d8bef9SDimitry Andric 
618e8d8bef9SDimitry Andric     // Thumb 1 arithmetic instructions do not set CPSR when executed inside an
619e8d8bef9SDimitry Andric     // IT block. This affects how they are printed.
620e8d8bef9SDimitry Andric     const MCInstrDesc &MCID = MI.getDesc();
621e8d8bef9SDimitry Andric     if (MCID.TSFlags & ARMII::ThumbArithFlagSetting) {
622bdd1243dSDimitry Andric       assert(MCID.operands()[1].isOptionalDef() &&
623bdd1243dSDimitry Andric              "CPSR def isn't expected operand");
624e8d8bef9SDimitry Andric       assert((MI.getOperand(1).isDead() ||
625e8d8bef9SDimitry Andric               MI.getOperand(1).getReg() != ARM::CPSR) &&
626e8d8bef9SDimitry Andric              "if conversion tried to stop defining used CPSR");
627e8d8bef9SDimitry Andric       MI.getOperand(1).setReg(ARM::NoRegister);
628e8d8bef9SDimitry Andric     }
629e8d8bef9SDimitry Andric 
6300b57cec5SDimitry Andric     return true;
6310b57cec5SDimitry Andric   }
6320b57cec5SDimitry Andric   return false;
6330b57cec5SDimitry Andric }
6340b57cec5SDimitry Andric 
6350b57cec5SDimitry Andric bool ARMBaseInstrInfo::SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
6360b57cec5SDimitry Andric                                          ArrayRef<MachineOperand> Pred2) const {
6370b57cec5SDimitry Andric   if (Pred1.size() > 2 || Pred2.size() > 2)
6380b57cec5SDimitry Andric     return false;
6390b57cec5SDimitry Andric 
6400b57cec5SDimitry Andric   ARMCC::CondCodes CC1 = (ARMCC::CondCodes)Pred1[0].getImm();
6410b57cec5SDimitry Andric   ARMCC::CondCodes CC2 = (ARMCC::CondCodes)Pred2[0].getImm();
6420b57cec5SDimitry Andric   if (CC1 == CC2)
6430b57cec5SDimitry Andric     return true;
6440b57cec5SDimitry Andric 
6450b57cec5SDimitry Andric   switch (CC1) {
6460b57cec5SDimitry Andric   default:
6470b57cec5SDimitry Andric     return false;
6480b57cec5SDimitry Andric   case ARMCC::AL:
6490b57cec5SDimitry Andric     return true;
6500b57cec5SDimitry Andric   case ARMCC::HS:
6510b57cec5SDimitry Andric     return CC2 == ARMCC::HI;
6520b57cec5SDimitry Andric   case ARMCC::LS:
6530b57cec5SDimitry Andric     return CC2 == ARMCC::LO || CC2 == ARMCC::EQ;
6540b57cec5SDimitry Andric   case ARMCC::GE:
6550b57cec5SDimitry Andric     return CC2 == ARMCC::GT;
6560b57cec5SDimitry Andric   case ARMCC::LE:
6570b57cec5SDimitry Andric     return CC2 == ARMCC::LT;
6580b57cec5SDimitry Andric   }
6590b57cec5SDimitry Andric }
6600b57cec5SDimitry Andric 
661e8d8bef9SDimitry Andric bool ARMBaseInstrInfo::ClobbersPredicate(MachineInstr &MI,
662e8d8bef9SDimitry Andric                                          std::vector<MachineOperand> &Pred,
663e8d8bef9SDimitry Andric                                          bool SkipDead) const {
6640b57cec5SDimitry Andric   bool Found = false;
6654824e7fdSDimitry Andric   for (const MachineOperand &MO : MI.operands()) {
666e8d8bef9SDimitry Andric     bool ClobbersCPSR = MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR);
667e8d8bef9SDimitry Andric     bool IsCPSR = MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR;
668e8d8bef9SDimitry Andric     if (ClobbersCPSR || IsCPSR) {
669e8d8bef9SDimitry Andric 
670e8d8bef9SDimitry Andric       // Filter out T1 instructions that have a dead CPSR,
671e8d8bef9SDimitry Andric       // allowing IT blocks to be generated containing T1 instructions
672e8d8bef9SDimitry Andric       const MCInstrDesc &MCID = MI.getDesc();
673e8d8bef9SDimitry Andric       if (MCID.TSFlags & ARMII::ThumbArithFlagSetting && MO.isDead() &&
674e8d8bef9SDimitry Andric           SkipDead)
675e8d8bef9SDimitry Andric         continue;
676e8d8bef9SDimitry Andric 
6770b57cec5SDimitry Andric       Pred.push_back(MO);
6780b57cec5SDimitry Andric       Found = true;
6790b57cec5SDimitry Andric     }
6800b57cec5SDimitry Andric   }
6810b57cec5SDimitry Andric 
6820b57cec5SDimitry Andric   return Found;
6830b57cec5SDimitry Andric }
6840b57cec5SDimitry Andric 
6850b57cec5SDimitry Andric bool ARMBaseInstrInfo::isCPSRDefined(const MachineInstr &MI) {
6860b57cec5SDimitry Andric   for (const auto &MO : MI.operands())
6870b57cec5SDimitry Andric     if (MO.isReg() && MO.getReg() == ARM::CPSR && MO.isDef() && !MO.isDead())
6880b57cec5SDimitry Andric       return true;
6890b57cec5SDimitry Andric   return false;
6900b57cec5SDimitry Andric }
6910b57cec5SDimitry Andric 
6920b57cec5SDimitry Andric static bool isEligibleForITBlock(const MachineInstr *MI) {
6930b57cec5SDimitry Andric   switch (MI->getOpcode()) {
6940b57cec5SDimitry Andric   default: return true;
6950b57cec5SDimitry Andric   case ARM::tADC:   // ADC (register) T1
6960b57cec5SDimitry Andric   case ARM::tADDi3: // ADD (immediate) T1
6970b57cec5SDimitry Andric   case ARM::tADDi8: // ADD (immediate) T2
6980b57cec5SDimitry Andric   case ARM::tADDrr: // ADD (register) T1
6990b57cec5SDimitry Andric   case ARM::tAND:   // AND (register) T1
7000b57cec5SDimitry Andric   case ARM::tASRri: // ASR (immediate) T1
7010b57cec5SDimitry Andric   case ARM::tASRrr: // ASR (register) T1
7020b57cec5SDimitry Andric   case ARM::tBIC:   // BIC (register) T1
7030b57cec5SDimitry Andric   case ARM::tEOR:   // EOR (register) T1
7040b57cec5SDimitry Andric   case ARM::tLSLri: // LSL (immediate) T1
7050b57cec5SDimitry Andric   case ARM::tLSLrr: // LSL (register) T1
7060b57cec5SDimitry Andric   case ARM::tLSRri: // LSR (immediate) T1
7070b57cec5SDimitry Andric   case ARM::tLSRrr: // LSR (register) T1
7080b57cec5SDimitry Andric   case ARM::tMUL:   // MUL T1
7090b57cec5SDimitry Andric   case ARM::tMVN:   // MVN (register) T1
7100b57cec5SDimitry Andric   case ARM::tORR:   // ORR (register) T1
7110b57cec5SDimitry Andric   case ARM::tROR:   // ROR (register) T1
7120b57cec5SDimitry Andric   case ARM::tRSB:   // RSB (immediate) T1
7130b57cec5SDimitry Andric   case ARM::tSBC:   // SBC (register) T1
7140b57cec5SDimitry Andric   case ARM::tSUBi3: // SUB (immediate) T1
7150b57cec5SDimitry Andric   case ARM::tSUBi8: // SUB (immediate) T2
7160b57cec5SDimitry Andric   case ARM::tSUBrr: // SUB (register) T1
7170b57cec5SDimitry Andric     return !ARMBaseInstrInfo::isCPSRDefined(*MI);
7180b57cec5SDimitry Andric   }
7190b57cec5SDimitry Andric }
7200b57cec5SDimitry Andric 
7210b57cec5SDimitry Andric /// isPredicable - Return true if the specified instruction can be predicated.
7220b57cec5SDimitry Andric /// By default, this returns true for every instruction with a
7230b57cec5SDimitry Andric /// PredicateOperand.
7240b57cec5SDimitry Andric bool ARMBaseInstrInfo::isPredicable(const MachineInstr &MI) const {
7250b57cec5SDimitry Andric   if (!MI.isPredicable())
7260b57cec5SDimitry Andric     return false;
7270b57cec5SDimitry Andric 
7280b57cec5SDimitry Andric   if (MI.isBundle())
7290b57cec5SDimitry Andric     return false;
7300b57cec5SDimitry Andric 
7310b57cec5SDimitry Andric   if (!isEligibleForITBlock(&MI))
7320b57cec5SDimitry Andric     return false;
7330b57cec5SDimitry Andric 
734e8d8bef9SDimitry Andric   const MachineFunction *MF = MI.getParent()->getParent();
7350b57cec5SDimitry Andric   const ARMFunctionInfo *AFI =
736e8d8bef9SDimitry Andric       MF->getInfo<ARMFunctionInfo>();
7370b57cec5SDimitry Andric 
7380b57cec5SDimitry Andric   // Neon instructions in Thumb2 IT blocks are deprecated, see ARMARM.
7390b57cec5SDimitry Andric   // In their ARM encoding, they can't be encoded in a conditional form.
7400b57cec5SDimitry Andric   if ((MI.getDesc().TSFlags & ARMII::DomainMask) == ARMII::DomainNEON)
7410b57cec5SDimitry Andric     return false;
7420b57cec5SDimitry Andric 
743e8d8bef9SDimitry Andric   // Make indirect control flow changes unpredicable when SLS mitigation is
744e8d8bef9SDimitry Andric   // enabled.
745e8d8bef9SDimitry Andric   const ARMSubtarget &ST = MF->getSubtarget<ARMSubtarget>();
746e8d8bef9SDimitry Andric   if (ST.hardenSlsRetBr() && isIndirectControlFlowNotComingBack(MI))
747e8d8bef9SDimitry Andric     return false;
748e8d8bef9SDimitry Andric   if (ST.hardenSlsBlr() && isIndirectCall(MI))
749e8d8bef9SDimitry Andric     return false;
750e8d8bef9SDimitry Andric 
7510b57cec5SDimitry Andric   if (AFI->isThumb2Function()) {
7520b57cec5SDimitry Andric     if (getSubtarget().restrictIT())
7530b57cec5SDimitry Andric       return isV8EligibleForIT(&MI);
7540b57cec5SDimitry Andric   }
7550b57cec5SDimitry Andric 
7560b57cec5SDimitry Andric   return true;
7570b57cec5SDimitry Andric }
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric namespace llvm {
7600b57cec5SDimitry Andric 
7610b57cec5SDimitry Andric template <> bool IsCPSRDead<MachineInstr>(const MachineInstr *MI) {
7624824e7fdSDimitry Andric   for (const MachineOperand &MO : MI->operands()) {
7630b57cec5SDimitry Andric     if (!MO.isReg() || MO.isUndef() || MO.isUse())
7640b57cec5SDimitry Andric       continue;
7650b57cec5SDimitry Andric     if (MO.getReg() != ARM::CPSR)
7660b57cec5SDimitry Andric       continue;
7670b57cec5SDimitry Andric     if (!MO.isDead())
7680b57cec5SDimitry Andric       return false;
7690b57cec5SDimitry Andric   }
7700b57cec5SDimitry Andric   // all definitions of CPSR are dead
7710b57cec5SDimitry Andric   return true;
7720b57cec5SDimitry Andric }
7730b57cec5SDimitry Andric 
7740b57cec5SDimitry Andric } // end namespace llvm
7750b57cec5SDimitry Andric 
7760b57cec5SDimitry Andric /// GetInstSize - Return the size of the specified MachineInstr.
7770b57cec5SDimitry Andric ///
7780b57cec5SDimitry Andric unsigned ARMBaseInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
7790b57cec5SDimitry Andric   const MachineBasicBlock &MBB = *MI.getParent();
7800b57cec5SDimitry Andric   const MachineFunction *MF = MBB.getParent();
7810b57cec5SDimitry Andric   const MCAsmInfo *MAI = MF->getTarget().getMCAsmInfo();
7820b57cec5SDimitry Andric 
7830b57cec5SDimitry Andric   const MCInstrDesc &MCID = MI.getDesc();
7840b57cec5SDimitry Andric 
7850b57cec5SDimitry Andric   switch (MI.getOpcode()) {
7860b57cec5SDimitry Andric   default:
7871fd87a68SDimitry Andric     // Return the size specified in .td file. If there's none, return 0, as we
7881fd87a68SDimitry Andric     // can't define a default size (Thumb1 instructions are 2 bytes, Thumb2
7891fd87a68SDimitry Andric     // instructions are 2-4 bytes, and ARM instructions are 4 bytes), in
7901fd87a68SDimitry Andric     // contrast to AArch64 instructions which have a default size of 4 bytes for
7911fd87a68SDimitry Andric     // example.
7921fd87a68SDimitry Andric     return MCID.getSize();
7930b57cec5SDimitry Andric   case TargetOpcode::BUNDLE:
7940b57cec5SDimitry Andric     return getInstBundleLength(MI);
7950b57cec5SDimitry Andric   case ARM::CONSTPOOL_ENTRY:
7960b57cec5SDimitry Andric   case ARM::JUMPTABLE_INSTS:
7970b57cec5SDimitry Andric   case ARM::JUMPTABLE_ADDRS:
7980b57cec5SDimitry Andric   case ARM::JUMPTABLE_TBB:
7990b57cec5SDimitry Andric   case ARM::JUMPTABLE_TBH:
8000b57cec5SDimitry Andric     // If this machine instr is a constant pool entry, its size is recorded as
8010b57cec5SDimitry Andric     // operand #2.
8020b57cec5SDimitry Andric     return MI.getOperand(2).getImm();
8030b57cec5SDimitry Andric   case ARM::SPACE:
8040b57cec5SDimitry Andric     return MI.getOperand(1).getImm();
8050b57cec5SDimitry Andric   case ARM::INLINEASM:
8060b57cec5SDimitry Andric   case ARM::INLINEASM_BR: {
8070b57cec5SDimitry Andric     // If this machine instr is an inline asm, measure it.
8080b57cec5SDimitry Andric     unsigned Size = getInlineAsmLength(MI.getOperand(0).getSymbolName(), *MAI);
8090b57cec5SDimitry Andric     if (!MF->getInfo<ARMFunctionInfo>()->isThumbFunction())
8100b57cec5SDimitry Andric       Size = alignTo(Size, 4);
8110b57cec5SDimitry Andric     return Size;
8120b57cec5SDimitry Andric   }
8130b57cec5SDimitry Andric   }
8140b57cec5SDimitry Andric }
8150b57cec5SDimitry Andric 
8160b57cec5SDimitry Andric unsigned ARMBaseInstrInfo::getInstBundleLength(const MachineInstr &MI) const {
8170b57cec5SDimitry Andric   unsigned Size = 0;
8180b57cec5SDimitry Andric   MachineBasicBlock::const_instr_iterator I = MI.getIterator();
8190b57cec5SDimitry Andric   MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
8200b57cec5SDimitry Andric   while (++I != E && I->isInsideBundle()) {
8210b57cec5SDimitry Andric     assert(!I->isBundle() && "No nested bundle!");
8220b57cec5SDimitry Andric     Size += getInstSizeInBytes(*I);
8230b57cec5SDimitry Andric   }
8240b57cec5SDimitry Andric   return Size;
8250b57cec5SDimitry Andric }
8260b57cec5SDimitry Andric 
8270b57cec5SDimitry Andric void ARMBaseInstrInfo::copyFromCPSR(MachineBasicBlock &MBB,
8280b57cec5SDimitry Andric                                     MachineBasicBlock::iterator I,
8290b57cec5SDimitry Andric                                     unsigned DestReg, bool KillSrc,
8300b57cec5SDimitry Andric                                     const ARMSubtarget &Subtarget) const {
8310b57cec5SDimitry Andric   unsigned Opc = Subtarget.isThumb()
8320b57cec5SDimitry Andric                      ? (Subtarget.isMClass() ? ARM::t2MRS_M : ARM::t2MRS_AR)
8330b57cec5SDimitry Andric                      : ARM::MRS;
8340b57cec5SDimitry Andric 
8350b57cec5SDimitry Andric   MachineInstrBuilder MIB =
8360b57cec5SDimitry Andric       BuildMI(MBB, I, I->getDebugLoc(), get(Opc), DestReg);
8370b57cec5SDimitry Andric 
8380b57cec5SDimitry Andric   // There is only 1 A/R class MRS instruction, and it always refers to
8390b57cec5SDimitry Andric   // APSR. However, there are lots of other possibilities on M-class cores.
8400b57cec5SDimitry Andric   if (Subtarget.isMClass())
8410b57cec5SDimitry Andric     MIB.addImm(0x800);
8420b57cec5SDimitry Andric 
8430b57cec5SDimitry Andric   MIB.add(predOps(ARMCC::AL))
8440b57cec5SDimitry Andric      .addReg(ARM::CPSR, RegState::Implicit | getKillRegState(KillSrc));
8450b57cec5SDimitry Andric }
8460b57cec5SDimitry Andric 
8470b57cec5SDimitry Andric void ARMBaseInstrInfo::copyToCPSR(MachineBasicBlock &MBB,
8480b57cec5SDimitry Andric                                   MachineBasicBlock::iterator I,
8490b57cec5SDimitry Andric                                   unsigned SrcReg, bool KillSrc,
8500b57cec5SDimitry Andric                                   const ARMSubtarget &Subtarget) const {
8510b57cec5SDimitry Andric   unsigned Opc = Subtarget.isThumb()
8520b57cec5SDimitry Andric                      ? (Subtarget.isMClass() ? ARM::t2MSR_M : ARM::t2MSR_AR)
8530b57cec5SDimitry Andric                      : ARM::MSR;
8540b57cec5SDimitry Andric 
8550b57cec5SDimitry Andric   MachineInstrBuilder MIB = BuildMI(MBB, I, I->getDebugLoc(), get(Opc));
8560b57cec5SDimitry Andric 
8570b57cec5SDimitry Andric   if (Subtarget.isMClass())
8580b57cec5SDimitry Andric     MIB.addImm(0x800);
8590b57cec5SDimitry Andric   else
8600b57cec5SDimitry Andric     MIB.addImm(8);
8610b57cec5SDimitry Andric 
8620b57cec5SDimitry Andric   MIB.addReg(SrcReg, getKillRegState(KillSrc))
8630b57cec5SDimitry Andric      .add(predOps(ARMCC::AL))
8640b57cec5SDimitry Andric      .addReg(ARM::CPSR, RegState::Implicit | RegState::Define);
8650b57cec5SDimitry Andric }
8660b57cec5SDimitry Andric 
8670b57cec5SDimitry Andric void llvm::addUnpredicatedMveVpredNOp(MachineInstrBuilder &MIB) {
8680b57cec5SDimitry Andric   MIB.addImm(ARMVCC::None);
8690b57cec5SDimitry Andric   MIB.addReg(0);
870349cc55cSDimitry Andric   MIB.addReg(0); // tp_reg
8710b57cec5SDimitry Andric }
8720b57cec5SDimitry Andric 
8730b57cec5SDimitry Andric void llvm::addUnpredicatedMveVpredROp(MachineInstrBuilder &MIB,
8745ffd83dbSDimitry Andric                                       Register DestReg) {
8750b57cec5SDimitry Andric   addUnpredicatedMveVpredNOp(MIB);
8760b57cec5SDimitry Andric   MIB.addReg(DestReg, RegState::Undef);
8770b57cec5SDimitry Andric }
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric void llvm::addPredicatedMveVpredNOp(MachineInstrBuilder &MIB, unsigned Cond) {
8800b57cec5SDimitry Andric   MIB.addImm(Cond);
8810b57cec5SDimitry Andric   MIB.addReg(ARM::VPR, RegState::Implicit);
882349cc55cSDimitry Andric   MIB.addReg(0); // tp_reg
8830b57cec5SDimitry Andric }
8840b57cec5SDimitry Andric 
8850b57cec5SDimitry Andric void llvm::addPredicatedMveVpredROp(MachineInstrBuilder &MIB,
8860b57cec5SDimitry Andric                                     unsigned Cond, unsigned Inactive) {
8870b57cec5SDimitry Andric   addPredicatedMveVpredNOp(MIB, Cond);
8880b57cec5SDimitry Andric   MIB.addReg(Inactive);
8890b57cec5SDimitry Andric }
8900b57cec5SDimitry Andric 
8910b57cec5SDimitry Andric void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
8920b57cec5SDimitry Andric                                    MachineBasicBlock::iterator I,
893480093f4SDimitry Andric                                    const DebugLoc &DL, MCRegister DestReg,
894480093f4SDimitry Andric                                    MCRegister SrcReg, bool KillSrc) const {
8950b57cec5SDimitry Andric   bool GPRDest = ARM::GPRRegClass.contains(DestReg);
8960b57cec5SDimitry Andric   bool GPRSrc = ARM::GPRRegClass.contains(SrcReg);
8970b57cec5SDimitry Andric 
8980b57cec5SDimitry Andric   if (GPRDest && GPRSrc) {
8990b57cec5SDimitry Andric     BuildMI(MBB, I, DL, get(ARM::MOVr), DestReg)
9000b57cec5SDimitry Andric         .addReg(SrcReg, getKillRegState(KillSrc))
9010b57cec5SDimitry Andric         .add(predOps(ARMCC::AL))
9020b57cec5SDimitry Andric         .add(condCodeOp());
9030b57cec5SDimitry Andric     return;
9040b57cec5SDimitry Andric   }
9050b57cec5SDimitry Andric 
9060b57cec5SDimitry Andric   bool SPRDest = ARM::SPRRegClass.contains(DestReg);
9070b57cec5SDimitry Andric   bool SPRSrc = ARM::SPRRegClass.contains(SrcReg);
9080b57cec5SDimitry Andric 
9090b57cec5SDimitry Andric   unsigned Opc = 0;
9100b57cec5SDimitry Andric   if (SPRDest && SPRSrc)
9110b57cec5SDimitry Andric     Opc = ARM::VMOVS;
9120b57cec5SDimitry Andric   else if (GPRDest && SPRSrc)
9130b57cec5SDimitry Andric     Opc = ARM::VMOVRS;
9140b57cec5SDimitry Andric   else if (SPRDest && GPRSrc)
9150b57cec5SDimitry Andric     Opc = ARM::VMOVSR;
9160b57cec5SDimitry Andric   else if (ARM::DPRRegClass.contains(DestReg, SrcReg) && Subtarget.hasFP64())
9170b57cec5SDimitry Andric     Opc = ARM::VMOVD;
9180b57cec5SDimitry Andric   else if (ARM::QPRRegClass.contains(DestReg, SrcReg))
919349cc55cSDimitry Andric     Opc = Subtarget.hasNEON() ? ARM::VORRq : ARM::MQPRCopy;
9200b57cec5SDimitry Andric 
9210b57cec5SDimitry Andric   if (Opc) {
9220b57cec5SDimitry Andric     MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(Opc), DestReg);
9230b57cec5SDimitry Andric     MIB.addReg(SrcReg, getKillRegState(KillSrc));
9240b57cec5SDimitry Andric     if (Opc == ARM::VORRq || Opc == ARM::MVE_VORR)
9250b57cec5SDimitry Andric       MIB.addReg(SrcReg, getKillRegState(KillSrc));
9260b57cec5SDimitry Andric     if (Opc == ARM::MVE_VORR)
9270b57cec5SDimitry Andric       addUnpredicatedMveVpredROp(MIB, DestReg);
928349cc55cSDimitry Andric     else if (Opc != ARM::MQPRCopy)
9290b57cec5SDimitry Andric       MIB.add(predOps(ARMCC::AL));
9300b57cec5SDimitry Andric     return;
9310b57cec5SDimitry Andric   }
9320b57cec5SDimitry Andric 
9330b57cec5SDimitry Andric   // Handle register classes that require multiple instructions.
9340b57cec5SDimitry Andric   unsigned BeginIdx = 0;
9350b57cec5SDimitry Andric   unsigned SubRegs = 0;
9360b57cec5SDimitry Andric   int Spacing = 1;
9370b57cec5SDimitry Andric 
9380b57cec5SDimitry Andric   // Use VORRq when possible.
9390b57cec5SDimitry Andric   if (ARM::QQPRRegClass.contains(DestReg, SrcReg)) {
9400b57cec5SDimitry Andric     Opc = Subtarget.hasNEON() ? ARM::VORRq : ARM::MVE_VORR;
9410b57cec5SDimitry Andric     BeginIdx = ARM::qsub_0;
9420b57cec5SDimitry Andric     SubRegs = 2;
9430b57cec5SDimitry Andric   } else if (ARM::QQQQPRRegClass.contains(DestReg, SrcReg)) {
9440b57cec5SDimitry Andric     Opc = Subtarget.hasNEON() ? ARM::VORRq : ARM::MVE_VORR;
9450b57cec5SDimitry Andric     BeginIdx = ARM::qsub_0;
9460b57cec5SDimitry Andric     SubRegs = 4;
9470b57cec5SDimitry Andric   // Fall back to VMOVD.
9480b57cec5SDimitry Andric   } else if (ARM::DPairRegClass.contains(DestReg, SrcReg)) {
9490b57cec5SDimitry Andric     Opc = ARM::VMOVD;
9500b57cec5SDimitry Andric     BeginIdx = ARM::dsub_0;
9510b57cec5SDimitry Andric     SubRegs = 2;
9520b57cec5SDimitry Andric   } else if (ARM::DTripleRegClass.contains(DestReg, SrcReg)) {
9530b57cec5SDimitry Andric     Opc = ARM::VMOVD;
9540b57cec5SDimitry Andric     BeginIdx = ARM::dsub_0;
9550b57cec5SDimitry Andric     SubRegs = 3;
9560b57cec5SDimitry Andric   } else if (ARM::DQuadRegClass.contains(DestReg, SrcReg)) {
9570b57cec5SDimitry Andric     Opc = ARM::VMOVD;
9580b57cec5SDimitry Andric     BeginIdx = ARM::dsub_0;
9590b57cec5SDimitry Andric     SubRegs = 4;
9600b57cec5SDimitry Andric   } else if (ARM::GPRPairRegClass.contains(DestReg, SrcReg)) {
9610b57cec5SDimitry Andric     Opc = Subtarget.isThumb2() ? ARM::tMOVr : ARM::MOVr;
9620b57cec5SDimitry Andric     BeginIdx = ARM::gsub_0;
9630b57cec5SDimitry Andric     SubRegs = 2;
9640b57cec5SDimitry Andric   } else if (ARM::DPairSpcRegClass.contains(DestReg, SrcReg)) {
9650b57cec5SDimitry Andric     Opc = ARM::VMOVD;
9660b57cec5SDimitry Andric     BeginIdx = ARM::dsub_0;
9670b57cec5SDimitry Andric     SubRegs = 2;
9680b57cec5SDimitry Andric     Spacing = 2;
9690b57cec5SDimitry Andric   } else if (ARM::DTripleSpcRegClass.contains(DestReg, SrcReg)) {
9700b57cec5SDimitry Andric     Opc = ARM::VMOVD;
9710b57cec5SDimitry Andric     BeginIdx = ARM::dsub_0;
9720b57cec5SDimitry Andric     SubRegs = 3;
9730b57cec5SDimitry Andric     Spacing = 2;
9740b57cec5SDimitry Andric   } else if (ARM::DQuadSpcRegClass.contains(DestReg, SrcReg)) {
9750b57cec5SDimitry Andric     Opc = ARM::VMOVD;
9760b57cec5SDimitry Andric     BeginIdx = ARM::dsub_0;
9770b57cec5SDimitry Andric     SubRegs = 4;
9780b57cec5SDimitry Andric     Spacing = 2;
9790b57cec5SDimitry Andric   } else if (ARM::DPRRegClass.contains(DestReg, SrcReg) &&
9800b57cec5SDimitry Andric              !Subtarget.hasFP64()) {
9810b57cec5SDimitry Andric     Opc = ARM::VMOVS;
9820b57cec5SDimitry Andric     BeginIdx = ARM::ssub_0;
9830b57cec5SDimitry Andric     SubRegs = 2;
9840b57cec5SDimitry Andric   } else if (SrcReg == ARM::CPSR) {
9850b57cec5SDimitry Andric     copyFromCPSR(MBB, I, DestReg, KillSrc, Subtarget);
9860b57cec5SDimitry Andric     return;
9870b57cec5SDimitry Andric   } else if (DestReg == ARM::CPSR) {
9880b57cec5SDimitry Andric     copyToCPSR(MBB, I, SrcReg, KillSrc, Subtarget);
9890b57cec5SDimitry Andric     return;
9900b57cec5SDimitry Andric   } else if (DestReg == ARM::VPR) {
9910b57cec5SDimitry Andric     assert(ARM::GPRRegClass.contains(SrcReg));
9920b57cec5SDimitry Andric     BuildMI(MBB, I, I->getDebugLoc(), get(ARM::VMSR_P0), DestReg)
9930b57cec5SDimitry Andric         .addReg(SrcReg, getKillRegState(KillSrc))
9940b57cec5SDimitry Andric         .add(predOps(ARMCC::AL));
9950b57cec5SDimitry Andric     return;
9960b57cec5SDimitry Andric   } else if (SrcReg == ARM::VPR) {
9970b57cec5SDimitry Andric     assert(ARM::GPRRegClass.contains(DestReg));
9980b57cec5SDimitry Andric     BuildMI(MBB, I, I->getDebugLoc(), get(ARM::VMRS_P0), DestReg)
9990b57cec5SDimitry Andric         .addReg(SrcReg, getKillRegState(KillSrc))
10000b57cec5SDimitry Andric         .add(predOps(ARMCC::AL));
10010b57cec5SDimitry Andric     return;
10020b57cec5SDimitry Andric   } else if (DestReg == ARM::FPSCR_NZCV) {
10030b57cec5SDimitry Andric     assert(ARM::GPRRegClass.contains(SrcReg));
10040b57cec5SDimitry Andric     BuildMI(MBB, I, I->getDebugLoc(), get(ARM::VMSR_FPSCR_NZCVQC), DestReg)
10050b57cec5SDimitry Andric         .addReg(SrcReg, getKillRegState(KillSrc))
10060b57cec5SDimitry Andric         .add(predOps(ARMCC::AL));
10070b57cec5SDimitry Andric     return;
10080b57cec5SDimitry Andric   } else if (SrcReg == ARM::FPSCR_NZCV) {
10090b57cec5SDimitry Andric     assert(ARM::GPRRegClass.contains(DestReg));
10100b57cec5SDimitry Andric     BuildMI(MBB, I, I->getDebugLoc(), get(ARM::VMRS_FPSCR_NZCVQC), DestReg)
10110b57cec5SDimitry Andric         .addReg(SrcReg, getKillRegState(KillSrc))
10120b57cec5SDimitry Andric         .add(predOps(ARMCC::AL));
10130b57cec5SDimitry Andric     return;
10140b57cec5SDimitry Andric   }
10150b57cec5SDimitry Andric 
10160b57cec5SDimitry Andric   assert(Opc && "Impossible reg-to-reg copy");
10170b57cec5SDimitry Andric 
10180b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = &getRegisterInfo();
10190b57cec5SDimitry Andric   MachineInstrBuilder Mov;
10200b57cec5SDimitry Andric 
10210b57cec5SDimitry Andric   // Copy register tuples backward when the first Dest reg overlaps with SrcReg.
10220b57cec5SDimitry Andric   if (TRI->regsOverlap(SrcReg, TRI->getSubReg(DestReg, BeginIdx))) {
10230b57cec5SDimitry Andric     BeginIdx = BeginIdx + ((SubRegs - 1) * Spacing);
10240b57cec5SDimitry Andric     Spacing = -Spacing;
10250b57cec5SDimitry Andric   }
10260b57cec5SDimitry Andric #ifndef NDEBUG
10270b57cec5SDimitry Andric   SmallSet<unsigned, 4> DstRegs;
10280b57cec5SDimitry Andric #endif
10290b57cec5SDimitry Andric   for (unsigned i = 0; i != SubRegs; ++i) {
10308bcb0991SDimitry Andric     Register Dst = TRI->getSubReg(DestReg, BeginIdx + i * Spacing);
10318bcb0991SDimitry Andric     Register Src = TRI->getSubReg(SrcReg, BeginIdx + i * Spacing);
10320b57cec5SDimitry Andric     assert(Dst && Src && "Bad sub-register");
10330b57cec5SDimitry Andric #ifndef NDEBUG
10340b57cec5SDimitry Andric     assert(!DstRegs.count(Src) && "destructive vector copy");
10350b57cec5SDimitry Andric     DstRegs.insert(Dst);
10360b57cec5SDimitry Andric #endif
10370b57cec5SDimitry Andric     Mov = BuildMI(MBB, I, I->getDebugLoc(), get(Opc), Dst).addReg(Src);
10380b57cec5SDimitry Andric     // VORR (NEON or MVE) takes two source operands.
10390b57cec5SDimitry Andric     if (Opc == ARM::VORRq || Opc == ARM::MVE_VORR) {
10400b57cec5SDimitry Andric       Mov.addReg(Src);
10410b57cec5SDimitry Andric     }
10420b57cec5SDimitry Andric     // MVE VORR takes predicate operands in place of an ordinary condition.
10430b57cec5SDimitry Andric     if (Opc == ARM::MVE_VORR)
10440b57cec5SDimitry Andric       addUnpredicatedMveVpredROp(Mov, Dst);
10450b57cec5SDimitry Andric     else
10460b57cec5SDimitry Andric       Mov = Mov.add(predOps(ARMCC::AL));
10470b57cec5SDimitry Andric     // MOVr can set CC.
10480b57cec5SDimitry Andric     if (Opc == ARM::MOVr)
10490b57cec5SDimitry Andric       Mov = Mov.add(condCodeOp());
10500b57cec5SDimitry Andric   }
10510b57cec5SDimitry Andric   // Add implicit super-register defs and kills to the last instruction.
10520b57cec5SDimitry Andric   Mov->addRegisterDefined(DestReg, TRI);
10530b57cec5SDimitry Andric   if (KillSrc)
10540b57cec5SDimitry Andric     Mov->addRegisterKilled(SrcReg, TRI);
10550b57cec5SDimitry Andric }
10560b57cec5SDimitry Andric 
1057bdd1243dSDimitry Andric std::optional<DestSourcePair>
1058480093f4SDimitry Andric ARMBaseInstrInfo::isCopyInstrImpl(const MachineInstr &MI) const {
10590b57cec5SDimitry Andric   // VMOVRRD is also a copy instruction but it requires
10600b57cec5SDimitry Andric   // special way of handling. It is more complex copy version
10610b57cec5SDimitry Andric   // and since that we are not considering it. For recognition
10620b57cec5SDimitry Andric   // of such instruction isExtractSubregLike MI interface fuction
10630b57cec5SDimitry Andric   // could be used.
10640b57cec5SDimitry Andric   // VORRq is considered as a move only if two inputs are
10650b57cec5SDimitry Andric   // the same register.
10660b57cec5SDimitry Andric   if (!MI.isMoveReg() ||
10670b57cec5SDimitry Andric       (MI.getOpcode() == ARM::VORRq &&
10680b57cec5SDimitry Andric        MI.getOperand(1).getReg() != MI.getOperand(2).getReg()))
1069bdd1243dSDimitry Andric     return std::nullopt;
1070480093f4SDimitry Andric   return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
10710b57cec5SDimitry Andric }
10720b57cec5SDimitry Andric 
1073bdd1243dSDimitry Andric std::optional<ParamLoadedValue>
10745ffd83dbSDimitry Andric ARMBaseInstrInfo::describeLoadedValue(const MachineInstr &MI,
10755ffd83dbSDimitry Andric                                       Register Reg) const {
10765ffd83dbSDimitry Andric   if (auto DstSrcPair = isCopyInstrImpl(MI)) {
10775ffd83dbSDimitry Andric     Register DstReg = DstSrcPair->Destination->getReg();
10785ffd83dbSDimitry Andric 
10795ffd83dbSDimitry Andric     // TODO: We don't handle cases where the forwarding reg is narrower/wider
10805ffd83dbSDimitry Andric     // than the copy registers. Consider for example:
10815ffd83dbSDimitry Andric     //
10825ffd83dbSDimitry Andric     //   s16 = VMOVS s0
10835ffd83dbSDimitry Andric     //   s17 = VMOVS s1
10845ffd83dbSDimitry Andric     //   call @callee(d0)
10855ffd83dbSDimitry Andric     //
10865ffd83dbSDimitry Andric     // We'd like to describe the call site value of d0 as d8, but this requires
10875ffd83dbSDimitry Andric     // gathering and merging the descriptions for the two VMOVS instructions.
10885ffd83dbSDimitry Andric     //
10895ffd83dbSDimitry Andric     // We also don't handle the reverse situation, where the forwarding reg is
10905ffd83dbSDimitry Andric     // narrower than the copy destination:
10915ffd83dbSDimitry Andric     //
10925ffd83dbSDimitry Andric     //   d8 = VMOVD d0
10935ffd83dbSDimitry Andric     //   call @callee(s1)
10945ffd83dbSDimitry Andric     //
10955ffd83dbSDimitry Andric     // We need to produce a fragment description (the call site value of s1 is
10965ffd83dbSDimitry Andric     // /not/ just d8).
10975ffd83dbSDimitry Andric     if (DstReg != Reg)
1098bdd1243dSDimitry Andric       return std::nullopt;
10995ffd83dbSDimitry Andric   }
11005ffd83dbSDimitry Andric   return TargetInstrInfo::describeLoadedValue(MI, Reg);
11015ffd83dbSDimitry Andric }
11025ffd83dbSDimitry Andric 
11030b57cec5SDimitry Andric const MachineInstrBuilder &
11040b57cec5SDimitry Andric ARMBaseInstrInfo::AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
11050b57cec5SDimitry Andric                           unsigned SubIdx, unsigned State,
11060b57cec5SDimitry Andric                           const TargetRegisterInfo *TRI) const {
11070b57cec5SDimitry Andric   if (!SubIdx)
11080b57cec5SDimitry Andric     return MIB.addReg(Reg, State);
11090b57cec5SDimitry Andric 
11108bcb0991SDimitry Andric   if (Register::isPhysicalRegister(Reg))
11110b57cec5SDimitry Andric     return MIB.addReg(TRI->getSubReg(Reg, SubIdx), State);
11120b57cec5SDimitry Andric   return MIB.addReg(Reg, State, SubIdx);
11130b57cec5SDimitry Andric }
11140b57cec5SDimitry Andric 
1115bdd1243dSDimitry Andric void ARMBaseInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
1116bdd1243dSDimitry Andric                                            MachineBasicBlock::iterator I,
11175ffd83dbSDimitry Andric                                            Register SrcReg, bool isKill, int FI,
11180b57cec5SDimitry Andric                                            const TargetRegisterClass *RC,
1119bdd1243dSDimitry Andric                                            const TargetRegisterInfo *TRI,
1120bdd1243dSDimitry Andric                                            Register VReg) const {
11210b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
11220b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
11235ffd83dbSDimitry Andric   Align Alignment = MFI.getObjectAlign(FI);
11240b57cec5SDimitry Andric 
11250b57cec5SDimitry Andric   MachineMemOperand *MMO = MF.getMachineMemOperand(
11260b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
11275ffd83dbSDimitry Andric       MFI.getObjectSize(FI), Alignment);
11280b57cec5SDimitry Andric 
11290b57cec5SDimitry Andric   switch (TRI->getSpillSize(*RC)) {
11300b57cec5SDimitry Andric     case 2:
11310b57cec5SDimitry Andric       if (ARM::HPRRegClass.hasSubClassEq(RC)) {
11320b57cec5SDimitry Andric         BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRH))
11330b57cec5SDimitry Andric             .addReg(SrcReg, getKillRegState(isKill))
11340b57cec5SDimitry Andric             .addFrameIndex(FI)
11350b57cec5SDimitry Andric             .addImm(0)
11360b57cec5SDimitry Andric             .addMemOperand(MMO)
11370b57cec5SDimitry Andric             .add(predOps(ARMCC::AL));
11380b57cec5SDimitry Andric       } else
11390b57cec5SDimitry Andric         llvm_unreachable("Unknown reg class!");
11400b57cec5SDimitry Andric       break;
11410b57cec5SDimitry Andric     case 4:
11420b57cec5SDimitry Andric       if (ARM::GPRRegClass.hasSubClassEq(RC)) {
11430b57cec5SDimitry Andric         BuildMI(MBB, I, DebugLoc(), get(ARM::STRi12))
11440b57cec5SDimitry Andric             .addReg(SrcReg, getKillRegState(isKill))
11450b57cec5SDimitry Andric             .addFrameIndex(FI)
11460b57cec5SDimitry Andric             .addImm(0)
11470b57cec5SDimitry Andric             .addMemOperand(MMO)
11480b57cec5SDimitry Andric             .add(predOps(ARMCC::AL));
11490b57cec5SDimitry Andric       } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
11500b57cec5SDimitry Andric         BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRS))
11510b57cec5SDimitry Andric             .addReg(SrcReg, getKillRegState(isKill))
11520b57cec5SDimitry Andric             .addFrameIndex(FI)
11530b57cec5SDimitry Andric             .addImm(0)
11540b57cec5SDimitry Andric             .addMemOperand(MMO)
11550b57cec5SDimitry Andric             .add(predOps(ARMCC::AL));
11560b57cec5SDimitry Andric       } else if (ARM::VCCRRegClass.hasSubClassEq(RC)) {
11570b57cec5SDimitry Andric         BuildMI(MBB, I, DebugLoc(), get(ARM::VSTR_P0_off))
11580b57cec5SDimitry Andric             .addReg(SrcReg, getKillRegState(isKill))
11590b57cec5SDimitry Andric             .addFrameIndex(FI)
11600b57cec5SDimitry Andric             .addImm(0)
11610b57cec5SDimitry Andric             .addMemOperand(MMO)
11620b57cec5SDimitry Andric             .add(predOps(ARMCC::AL));
11630b57cec5SDimitry Andric       } else
11640b57cec5SDimitry Andric         llvm_unreachable("Unknown reg class!");
11650b57cec5SDimitry Andric       break;
11660b57cec5SDimitry Andric     case 8:
11670b57cec5SDimitry Andric       if (ARM::DPRRegClass.hasSubClassEq(RC)) {
11680b57cec5SDimitry Andric         BuildMI(MBB, I, DebugLoc(), get(ARM::VSTRD))
11690b57cec5SDimitry Andric             .addReg(SrcReg, getKillRegState(isKill))
11700b57cec5SDimitry Andric             .addFrameIndex(FI)
11710b57cec5SDimitry Andric             .addImm(0)
11720b57cec5SDimitry Andric             .addMemOperand(MMO)
11730b57cec5SDimitry Andric             .add(predOps(ARMCC::AL));
11740b57cec5SDimitry Andric       } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
11750b57cec5SDimitry Andric         if (Subtarget.hasV5TEOps()) {
11760b57cec5SDimitry Andric           MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::STRD));
11770b57cec5SDimitry Andric           AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
11780b57cec5SDimitry Andric           AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
11790b57cec5SDimitry Andric           MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
11800b57cec5SDimitry Andric              .add(predOps(ARMCC::AL));
11810b57cec5SDimitry Andric         } else {
11820b57cec5SDimitry Andric           // Fallback to STM instruction, which has existed since the dawn of
11830b57cec5SDimitry Andric           // time.
11840b57cec5SDimitry Andric           MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::STMIA))
11850b57cec5SDimitry Andric                                         .addFrameIndex(FI)
11860b57cec5SDimitry Andric                                         .addMemOperand(MMO)
11870b57cec5SDimitry Andric                                         .add(predOps(ARMCC::AL));
11880b57cec5SDimitry Andric           AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
11890b57cec5SDimitry Andric           AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
11900b57cec5SDimitry Andric         }
11910b57cec5SDimitry Andric       } else
11920b57cec5SDimitry Andric         llvm_unreachable("Unknown reg class!");
11930b57cec5SDimitry Andric       break;
11940b57cec5SDimitry Andric     case 16:
11950b57cec5SDimitry Andric       if (ARM::DPairRegClass.hasSubClassEq(RC) && Subtarget.hasNEON()) {
11960b57cec5SDimitry Andric         // Use aligned spills if the stack can be realigned.
11975ffd83dbSDimitry Andric         if (Alignment >= 16 && getRegisterInfo().canRealignStack(MF)) {
11980b57cec5SDimitry Andric           BuildMI(MBB, I, DebugLoc(), get(ARM::VST1q64))
11990b57cec5SDimitry Andric               .addFrameIndex(FI)
12000b57cec5SDimitry Andric               .addImm(16)
12010b57cec5SDimitry Andric               .addReg(SrcReg, getKillRegState(isKill))
12020b57cec5SDimitry Andric               .addMemOperand(MMO)
12030b57cec5SDimitry Andric               .add(predOps(ARMCC::AL));
12040b57cec5SDimitry Andric         } else {
12050b57cec5SDimitry Andric           BuildMI(MBB, I, DebugLoc(), get(ARM::VSTMQIA))
12060b57cec5SDimitry Andric               .addReg(SrcReg, getKillRegState(isKill))
12070b57cec5SDimitry Andric               .addFrameIndex(FI)
12080b57cec5SDimitry Andric               .addMemOperand(MMO)
12090b57cec5SDimitry Andric               .add(predOps(ARMCC::AL));
12100b57cec5SDimitry Andric         }
12110b57cec5SDimitry Andric       } else if (ARM::QPRRegClass.hasSubClassEq(RC) &&
12120b57cec5SDimitry Andric                  Subtarget.hasMVEIntegerOps()) {
12130b57cec5SDimitry Andric         auto MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::MVE_VSTRWU32));
12140b57cec5SDimitry Andric         MIB.addReg(SrcReg, getKillRegState(isKill))
12150b57cec5SDimitry Andric           .addFrameIndex(FI)
12160b57cec5SDimitry Andric           .addImm(0)
12170b57cec5SDimitry Andric           .addMemOperand(MMO);
12180b57cec5SDimitry Andric         addUnpredicatedMveVpredNOp(MIB);
12190b57cec5SDimitry Andric       } else
12200b57cec5SDimitry Andric         llvm_unreachable("Unknown reg class!");
12210b57cec5SDimitry Andric       break;
12220b57cec5SDimitry Andric     case 24:
12230b57cec5SDimitry Andric       if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
12240b57cec5SDimitry Andric         // Use aligned spills if the stack can be realigned.
12255ffd83dbSDimitry Andric         if (Alignment >= 16 && getRegisterInfo().canRealignStack(MF) &&
12268bcb0991SDimitry Andric             Subtarget.hasNEON()) {
12270b57cec5SDimitry Andric           BuildMI(MBB, I, DebugLoc(), get(ARM::VST1d64TPseudo))
12280b57cec5SDimitry Andric               .addFrameIndex(FI)
12290b57cec5SDimitry Andric               .addImm(16)
12300b57cec5SDimitry Andric               .addReg(SrcReg, getKillRegState(isKill))
12310b57cec5SDimitry Andric               .addMemOperand(MMO)
12320b57cec5SDimitry Andric               .add(predOps(ARMCC::AL));
12330b57cec5SDimitry Andric         } else {
12340b57cec5SDimitry Andric           MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(),
12350b57cec5SDimitry Andric                                             get(ARM::VSTMDIA))
12360b57cec5SDimitry Andric                                         .addFrameIndex(FI)
12370b57cec5SDimitry Andric                                         .add(predOps(ARMCC::AL))
12380b57cec5SDimitry Andric                                         .addMemOperand(MMO);
12390b57cec5SDimitry Andric           MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
12400b57cec5SDimitry Andric           MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
12410b57cec5SDimitry Andric           AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
12420b57cec5SDimitry Andric         }
12430b57cec5SDimitry Andric       } else
12440b57cec5SDimitry Andric         llvm_unreachable("Unknown reg class!");
12450b57cec5SDimitry Andric       break;
12460b57cec5SDimitry Andric     case 32:
1247349cc55cSDimitry Andric       if (ARM::QQPRRegClass.hasSubClassEq(RC) ||
1248349cc55cSDimitry Andric           ARM::MQQPRRegClass.hasSubClassEq(RC) ||
1249349cc55cSDimitry Andric           ARM::DQuadRegClass.hasSubClassEq(RC)) {
12505ffd83dbSDimitry Andric         if (Alignment >= 16 && getRegisterInfo().canRealignStack(MF) &&
12518bcb0991SDimitry Andric             Subtarget.hasNEON()) {
12520b57cec5SDimitry Andric           // FIXME: It's possible to only store part of the QQ register if the
12530b57cec5SDimitry Andric           // spilled def has a sub-register index.
12540b57cec5SDimitry Andric           BuildMI(MBB, I, DebugLoc(), get(ARM::VST1d64QPseudo))
12550b57cec5SDimitry Andric               .addFrameIndex(FI)
12560b57cec5SDimitry Andric               .addImm(16)
12570b57cec5SDimitry Andric               .addReg(SrcReg, getKillRegState(isKill))
12580b57cec5SDimitry Andric               .addMemOperand(MMO)
12590b57cec5SDimitry Andric               .add(predOps(ARMCC::AL));
1260349cc55cSDimitry Andric         } else if (Subtarget.hasMVEIntegerOps()) {
1261349cc55cSDimitry Andric           BuildMI(MBB, I, DebugLoc(), get(ARM::MQQPRStore))
1262349cc55cSDimitry Andric               .addReg(SrcReg, getKillRegState(isKill))
1263349cc55cSDimitry Andric               .addFrameIndex(FI)
1264349cc55cSDimitry Andric               .addMemOperand(MMO);
12650b57cec5SDimitry Andric         } else {
12660b57cec5SDimitry Andric           MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(),
12670b57cec5SDimitry Andric                                             get(ARM::VSTMDIA))
12680b57cec5SDimitry Andric                                         .addFrameIndex(FI)
12690b57cec5SDimitry Andric                                         .add(predOps(ARMCC::AL))
12700b57cec5SDimitry Andric                                         .addMemOperand(MMO);
12710b57cec5SDimitry Andric           MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
12720b57cec5SDimitry Andric           MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
12730b57cec5SDimitry Andric           MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
12740b57cec5SDimitry Andric                 AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
12750b57cec5SDimitry Andric         }
12760b57cec5SDimitry Andric       } else
12770b57cec5SDimitry Andric         llvm_unreachable("Unknown reg class!");
12780b57cec5SDimitry Andric       break;
12790b57cec5SDimitry Andric     case 64:
1280349cc55cSDimitry Andric       if (ARM::MQQQQPRRegClass.hasSubClassEq(RC) &&
1281349cc55cSDimitry Andric           Subtarget.hasMVEIntegerOps()) {
1282349cc55cSDimitry Andric         BuildMI(MBB, I, DebugLoc(), get(ARM::MQQQQPRStore))
1283349cc55cSDimitry Andric             .addReg(SrcReg, getKillRegState(isKill))
1284349cc55cSDimitry Andric             .addFrameIndex(FI)
1285349cc55cSDimitry Andric             .addMemOperand(MMO);
1286349cc55cSDimitry Andric       } else if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
12870b57cec5SDimitry Andric         MachineInstrBuilder MIB = BuildMI(MBB, I, DebugLoc(), get(ARM::VSTMDIA))
12880b57cec5SDimitry Andric                                       .addFrameIndex(FI)
12890b57cec5SDimitry Andric                                       .add(predOps(ARMCC::AL))
12900b57cec5SDimitry Andric                                       .addMemOperand(MMO);
12910b57cec5SDimitry Andric         MIB = AddDReg(MIB, SrcReg, ARM::dsub_0, getKillRegState(isKill), TRI);
12920b57cec5SDimitry Andric         MIB = AddDReg(MIB, SrcReg, ARM::dsub_1, 0, TRI);
12930b57cec5SDimitry Andric         MIB = AddDReg(MIB, SrcReg, ARM::dsub_2, 0, TRI);
12940b57cec5SDimitry Andric         MIB = AddDReg(MIB, SrcReg, ARM::dsub_3, 0, TRI);
12950b57cec5SDimitry Andric         MIB = AddDReg(MIB, SrcReg, ARM::dsub_4, 0, TRI);
12960b57cec5SDimitry Andric         MIB = AddDReg(MIB, SrcReg, ARM::dsub_5, 0, TRI);
12970b57cec5SDimitry Andric         MIB = AddDReg(MIB, SrcReg, ARM::dsub_6, 0, TRI);
12980b57cec5SDimitry Andric               AddDReg(MIB, SrcReg, ARM::dsub_7, 0, TRI);
12990b57cec5SDimitry Andric       } else
13000b57cec5SDimitry Andric         llvm_unreachable("Unknown reg class!");
13010b57cec5SDimitry Andric       break;
13020b57cec5SDimitry Andric     default:
13030b57cec5SDimitry Andric       llvm_unreachable("Unknown reg class!");
13040b57cec5SDimitry Andric   }
13050b57cec5SDimitry Andric }
13060b57cec5SDimitry Andric 
13070b57cec5SDimitry Andric unsigned ARMBaseInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
13080b57cec5SDimitry Andric                                               int &FrameIndex) const {
13090b57cec5SDimitry Andric   switch (MI.getOpcode()) {
13100b57cec5SDimitry Andric   default: break;
13110b57cec5SDimitry Andric   case ARM::STRrs:
13120b57cec5SDimitry Andric   case ARM::t2STRs: // FIXME: don't use t2STRs to access frame.
13130b57cec5SDimitry Andric     if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
13140b57cec5SDimitry Andric         MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
13150b57cec5SDimitry Andric         MI.getOperand(3).getImm() == 0) {
13160b57cec5SDimitry Andric       FrameIndex = MI.getOperand(1).getIndex();
13170b57cec5SDimitry Andric       return MI.getOperand(0).getReg();
13180b57cec5SDimitry Andric     }
13190b57cec5SDimitry Andric     break;
13200b57cec5SDimitry Andric   case ARM::STRi12:
13210b57cec5SDimitry Andric   case ARM::t2STRi12:
13220b57cec5SDimitry Andric   case ARM::tSTRspi:
13230b57cec5SDimitry Andric   case ARM::VSTRD:
13240b57cec5SDimitry Andric   case ARM::VSTRS:
1325fe6060f1SDimitry Andric   case ARM::VSTR_P0_off:
1326fe6060f1SDimitry Andric   case ARM::MVE_VSTRWU32:
13270b57cec5SDimitry Andric     if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
13280b57cec5SDimitry Andric         MI.getOperand(2).getImm() == 0) {
13290b57cec5SDimitry Andric       FrameIndex = MI.getOperand(1).getIndex();
13300b57cec5SDimitry Andric       return MI.getOperand(0).getReg();
13310b57cec5SDimitry Andric     }
13320b57cec5SDimitry Andric     break;
13330b57cec5SDimitry Andric   case ARM::VST1q64:
13340b57cec5SDimitry Andric   case ARM::VST1d64TPseudo:
13350b57cec5SDimitry Andric   case ARM::VST1d64QPseudo:
13360b57cec5SDimitry Andric     if (MI.getOperand(0).isFI() && MI.getOperand(2).getSubReg() == 0) {
13370b57cec5SDimitry Andric       FrameIndex = MI.getOperand(0).getIndex();
13380b57cec5SDimitry Andric       return MI.getOperand(2).getReg();
13390b57cec5SDimitry Andric     }
13400b57cec5SDimitry Andric     break;
13410b57cec5SDimitry Andric   case ARM::VSTMQIA:
13420b57cec5SDimitry Andric     if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
13430b57cec5SDimitry Andric       FrameIndex = MI.getOperand(1).getIndex();
13440b57cec5SDimitry Andric       return MI.getOperand(0).getReg();
13450b57cec5SDimitry Andric     }
13460b57cec5SDimitry Andric     break;
1347349cc55cSDimitry Andric   case ARM::MQQPRStore:
1348349cc55cSDimitry Andric   case ARM::MQQQQPRStore:
1349349cc55cSDimitry Andric     if (MI.getOperand(1).isFI()) {
1350349cc55cSDimitry Andric       FrameIndex = MI.getOperand(1).getIndex();
1351349cc55cSDimitry Andric       return MI.getOperand(0).getReg();
1352349cc55cSDimitry Andric     }
1353349cc55cSDimitry Andric     break;
13540b57cec5SDimitry Andric   }
13550b57cec5SDimitry Andric 
13560b57cec5SDimitry Andric   return 0;
13570b57cec5SDimitry Andric }
13580b57cec5SDimitry Andric 
13590b57cec5SDimitry Andric unsigned ARMBaseInstrInfo::isStoreToStackSlotPostFE(const MachineInstr &MI,
13600b57cec5SDimitry Andric                                                     int &FrameIndex) const {
13610b57cec5SDimitry Andric   SmallVector<const MachineMemOperand *, 1> Accesses;
13620b57cec5SDimitry Andric   if (MI.mayStore() && hasStoreToStackSlot(MI, Accesses) &&
13630b57cec5SDimitry Andric       Accesses.size() == 1) {
13640b57cec5SDimitry Andric     FrameIndex =
13650b57cec5SDimitry Andric         cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
13660b57cec5SDimitry Andric             ->getFrameIndex();
13670b57cec5SDimitry Andric     return true;
13680b57cec5SDimitry Andric   }
13690b57cec5SDimitry Andric   return false;
13700b57cec5SDimitry Andric }
13710b57cec5SDimitry Andric 
1372bdd1243dSDimitry Andric void ARMBaseInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
1373bdd1243dSDimitry Andric                                             MachineBasicBlock::iterator I,
13745ffd83dbSDimitry Andric                                             Register DestReg, int FI,
13750b57cec5SDimitry Andric                                             const TargetRegisterClass *RC,
1376bdd1243dSDimitry Andric                                             const TargetRegisterInfo *TRI,
1377bdd1243dSDimitry Andric                                             Register VReg) const {
13780b57cec5SDimitry Andric   DebugLoc DL;
13790b57cec5SDimitry Andric   if (I != MBB.end()) DL = I->getDebugLoc();
13800b57cec5SDimitry Andric   MachineFunction &MF = *MBB.getParent();
13810b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
13825ffd83dbSDimitry Andric   const Align Alignment = MFI.getObjectAlign(FI);
13830b57cec5SDimitry Andric   MachineMemOperand *MMO = MF.getMachineMemOperand(
13840b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
13855ffd83dbSDimitry Andric       MFI.getObjectSize(FI), Alignment);
13860b57cec5SDimitry Andric 
13870b57cec5SDimitry Andric   switch (TRI->getSpillSize(*RC)) {
13880b57cec5SDimitry Andric   case 2:
13890b57cec5SDimitry Andric     if (ARM::HPRRegClass.hasSubClassEq(RC)) {
13900b57cec5SDimitry Andric       BuildMI(MBB, I, DL, get(ARM::VLDRH), DestReg)
13910b57cec5SDimitry Andric           .addFrameIndex(FI)
13920b57cec5SDimitry Andric           .addImm(0)
13930b57cec5SDimitry Andric           .addMemOperand(MMO)
13940b57cec5SDimitry Andric           .add(predOps(ARMCC::AL));
13950b57cec5SDimitry Andric     } else
13960b57cec5SDimitry Andric       llvm_unreachable("Unknown reg class!");
13970b57cec5SDimitry Andric     break;
13980b57cec5SDimitry Andric   case 4:
13990b57cec5SDimitry Andric     if (ARM::GPRRegClass.hasSubClassEq(RC)) {
14000b57cec5SDimitry Andric       BuildMI(MBB, I, DL, get(ARM::LDRi12), DestReg)
14010b57cec5SDimitry Andric           .addFrameIndex(FI)
14020b57cec5SDimitry Andric           .addImm(0)
14030b57cec5SDimitry Andric           .addMemOperand(MMO)
14040b57cec5SDimitry Andric           .add(predOps(ARMCC::AL));
14050b57cec5SDimitry Andric     } else if (ARM::SPRRegClass.hasSubClassEq(RC)) {
14060b57cec5SDimitry Andric       BuildMI(MBB, I, DL, get(ARM::VLDRS), DestReg)
14070b57cec5SDimitry Andric           .addFrameIndex(FI)
14080b57cec5SDimitry Andric           .addImm(0)
14090b57cec5SDimitry Andric           .addMemOperand(MMO)
14100b57cec5SDimitry Andric           .add(predOps(ARMCC::AL));
14110b57cec5SDimitry Andric     } else if (ARM::VCCRRegClass.hasSubClassEq(RC)) {
14120b57cec5SDimitry Andric       BuildMI(MBB, I, DL, get(ARM::VLDR_P0_off), DestReg)
14130b57cec5SDimitry Andric           .addFrameIndex(FI)
14140b57cec5SDimitry Andric           .addImm(0)
14150b57cec5SDimitry Andric           .addMemOperand(MMO)
14160b57cec5SDimitry Andric           .add(predOps(ARMCC::AL));
14170b57cec5SDimitry Andric     } else
14180b57cec5SDimitry Andric       llvm_unreachable("Unknown reg class!");
14190b57cec5SDimitry Andric     break;
14200b57cec5SDimitry Andric   case 8:
14210b57cec5SDimitry Andric     if (ARM::DPRRegClass.hasSubClassEq(RC)) {
14220b57cec5SDimitry Andric       BuildMI(MBB, I, DL, get(ARM::VLDRD), DestReg)
14230b57cec5SDimitry Andric           .addFrameIndex(FI)
14240b57cec5SDimitry Andric           .addImm(0)
14250b57cec5SDimitry Andric           .addMemOperand(MMO)
14260b57cec5SDimitry Andric           .add(predOps(ARMCC::AL));
14270b57cec5SDimitry Andric     } else if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
14280b57cec5SDimitry Andric       MachineInstrBuilder MIB;
14290b57cec5SDimitry Andric 
14300b57cec5SDimitry Andric       if (Subtarget.hasV5TEOps()) {
14310b57cec5SDimitry Andric         MIB = BuildMI(MBB, I, DL, get(ARM::LDRD));
14320b57cec5SDimitry Andric         AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
14330b57cec5SDimitry Andric         AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
14340b57cec5SDimitry Andric         MIB.addFrameIndex(FI).addReg(0).addImm(0).addMemOperand(MMO)
14350b57cec5SDimitry Andric            .add(predOps(ARMCC::AL));
14360b57cec5SDimitry Andric       } else {
14370b57cec5SDimitry Andric         // Fallback to LDM instruction, which has existed since the dawn of
14380b57cec5SDimitry Andric         // time.
14390b57cec5SDimitry Andric         MIB = BuildMI(MBB, I, DL, get(ARM::LDMIA))
14400b57cec5SDimitry Andric                   .addFrameIndex(FI)
14410b57cec5SDimitry Andric                   .addMemOperand(MMO)
14420b57cec5SDimitry Andric                   .add(predOps(ARMCC::AL));
14430b57cec5SDimitry Andric         MIB = AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
14440b57cec5SDimitry Andric         MIB = AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
14450b57cec5SDimitry Andric       }
14460b57cec5SDimitry Andric 
1447bdd1243dSDimitry Andric       if (DestReg.isPhysical())
14480b57cec5SDimitry Andric         MIB.addReg(DestReg, RegState::ImplicitDefine);
14490b57cec5SDimitry Andric     } else
14500b57cec5SDimitry Andric       llvm_unreachable("Unknown reg class!");
14510b57cec5SDimitry Andric     break;
14520b57cec5SDimitry Andric   case 16:
14530b57cec5SDimitry Andric     if (ARM::DPairRegClass.hasSubClassEq(RC) && Subtarget.hasNEON()) {
14545ffd83dbSDimitry Andric       if (Alignment >= 16 && getRegisterInfo().canRealignStack(MF)) {
14550b57cec5SDimitry Andric         BuildMI(MBB, I, DL, get(ARM::VLD1q64), DestReg)
14560b57cec5SDimitry Andric             .addFrameIndex(FI)
14570b57cec5SDimitry Andric             .addImm(16)
14580b57cec5SDimitry Andric             .addMemOperand(MMO)
14590b57cec5SDimitry Andric             .add(predOps(ARMCC::AL));
14600b57cec5SDimitry Andric       } else {
14610b57cec5SDimitry Andric         BuildMI(MBB, I, DL, get(ARM::VLDMQIA), DestReg)
14620b57cec5SDimitry Andric             .addFrameIndex(FI)
14630b57cec5SDimitry Andric             .addMemOperand(MMO)
14640b57cec5SDimitry Andric             .add(predOps(ARMCC::AL));
14650b57cec5SDimitry Andric       }
14660b57cec5SDimitry Andric     } else if (ARM::QPRRegClass.hasSubClassEq(RC) &&
14670b57cec5SDimitry Andric                Subtarget.hasMVEIntegerOps()) {
14680b57cec5SDimitry Andric       auto MIB = BuildMI(MBB, I, DL, get(ARM::MVE_VLDRWU32), DestReg);
14690b57cec5SDimitry Andric       MIB.addFrameIndex(FI)
14700b57cec5SDimitry Andric         .addImm(0)
14710b57cec5SDimitry Andric         .addMemOperand(MMO);
14720b57cec5SDimitry Andric       addUnpredicatedMveVpredNOp(MIB);
14730b57cec5SDimitry Andric     } else
14740b57cec5SDimitry Andric       llvm_unreachable("Unknown reg class!");
14750b57cec5SDimitry Andric     break;
14760b57cec5SDimitry Andric   case 24:
14770b57cec5SDimitry Andric     if (ARM::DTripleRegClass.hasSubClassEq(RC)) {
14785ffd83dbSDimitry Andric       if (Alignment >= 16 && getRegisterInfo().canRealignStack(MF) &&
14798bcb0991SDimitry Andric           Subtarget.hasNEON()) {
14800b57cec5SDimitry Andric         BuildMI(MBB, I, DL, get(ARM::VLD1d64TPseudo), DestReg)
14810b57cec5SDimitry Andric             .addFrameIndex(FI)
14820b57cec5SDimitry Andric             .addImm(16)
14830b57cec5SDimitry Andric             .addMemOperand(MMO)
14840b57cec5SDimitry Andric             .add(predOps(ARMCC::AL));
14850b57cec5SDimitry Andric       } else {
14860b57cec5SDimitry Andric         MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
14870b57cec5SDimitry Andric                                       .addFrameIndex(FI)
14880b57cec5SDimitry Andric                                       .addMemOperand(MMO)
14890b57cec5SDimitry Andric                                       .add(predOps(ARMCC::AL));
14900b57cec5SDimitry Andric         MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
14910b57cec5SDimitry Andric         MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
14920b57cec5SDimitry Andric         MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
1493bdd1243dSDimitry Andric         if (DestReg.isPhysical())
14940b57cec5SDimitry Andric           MIB.addReg(DestReg, RegState::ImplicitDefine);
14950b57cec5SDimitry Andric       }
14960b57cec5SDimitry Andric     } else
14970b57cec5SDimitry Andric       llvm_unreachable("Unknown reg class!");
14980b57cec5SDimitry Andric     break;
14990b57cec5SDimitry Andric    case 32:
1500349cc55cSDimitry Andric      if (ARM::QQPRRegClass.hasSubClassEq(RC) ||
1501349cc55cSDimitry Andric          ARM::MQQPRRegClass.hasSubClassEq(RC) ||
1502349cc55cSDimitry Andric          ARM::DQuadRegClass.hasSubClassEq(RC)) {
15035ffd83dbSDimitry Andric        if (Alignment >= 16 && getRegisterInfo().canRealignStack(MF) &&
15048bcb0991SDimitry Andric            Subtarget.hasNEON()) {
15050b57cec5SDimitry Andric          BuildMI(MBB, I, DL, get(ARM::VLD1d64QPseudo), DestReg)
15060b57cec5SDimitry Andric              .addFrameIndex(FI)
15070b57cec5SDimitry Andric              .addImm(16)
15080b57cec5SDimitry Andric              .addMemOperand(MMO)
15090b57cec5SDimitry Andric              .add(predOps(ARMCC::AL));
1510349cc55cSDimitry Andric        } else if (Subtarget.hasMVEIntegerOps()) {
1511349cc55cSDimitry Andric          BuildMI(MBB, I, DL, get(ARM::MQQPRLoad), DestReg)
1512349cc55cSDimitry Andric              .addFrameIndex(FI)
1513349cc55cSDimitry Andric              .addMemOperand(MMO);
15140b57cec5SDimitry Andric        } else {
15150b57cec5SDimitry Andric          MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
15160b57cec5SDimitry Andric                                        .addFrameIndex(FI)
15170b57cec5SDimitry Andric                                        .add(predOps(ARMCC::AL))
15180b57cec5SDimitry Andric                                        .addMemOperand(MMO);
15190b57cec5SDimitry Andric          MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
15200b57cec5SDimitry Andric          MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
15210b57cec5SDimitry Andric          MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
15220b57cec5SDimitry Andric          MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
1523bdd1243dSDimitry Andric          if (DestReg.isPhysical())
15240b57cec5SDimitry Andric            MIB.addReg(DestReg, RegState::ImplicitDefine);
15250b57cec5SDimitry Andric        }
15260b57cec5SDimitry Andric      } else
15270b57cec5SDimitry Andric        llvm_unreachable("Unknown reg class!");
15280b57cec5SDimitry Andric      break;
15290b57cec5SDimitry Andric   case 64:
1530349cc55cSDimitry Andric     if (ARM::MQQQQPRRegClass.hasSubClassEq(RC) &&
1531349cc55cSDimitry Andric         Subtarget.hasMVEIntegerOps()) {
1532349cc55cSDimitry Andric       BuildMI(MBB, I, DL, get(ARM::MQQQQPRLoad), DestReg)
1533349cc55cSDimitry Andric           .addFrameIndex(FI)
1534349cc55cSDimitry Andric           .addMemOperand(MMO);
1535349cc55cSDimitry Andric     } else if (ARM::QQQQPRRegClass.hasSubClassEq(RC)) {
15360b57cec5SDimitry Andric       MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::VLDMDIA))
15370b57cec5SDimitry Andric                                     .addFrameIndex(FI)
15380b57cec5SDimitry Andric                                     .add(predOps(ARMCC::AL))
15390b57cec5SDimitry Andric                                     .addMemOperand(MMO);
15400b57cec5SDimitry Andric       MIB = AddDReg(MIB, DestReg, ARM::dsub_0, RegState::DefineNoRead, TRI);
15410b57cec5SDimitry Andric       MIB = AddDReg(MIB, DestReg, ARM::dsub_1, RegState::DefineNoRead, TRI);
15420b57cec5SDimitry Andric       MIB = AddDReg(MIB, DestReg, ARM::dsub_2, RegState::DefineNoRead, TRI);
15430b57cec5SDimitry Andric       MIB = AddDReg(MIB, DestReg, ARM::dsub_3, RegState::DefineNoRead, TRI);
15440b57cec5SDimitry Andric       MIB = AddDReg(MIB, DestReg, ARM::dsub_4, RegState::DefineNoRead, TRI);
15450b57cec5SDimitry Andric       MIB = AddDReg(MIB, DestReg, ARM::dsub_5, RegState::DefineNoRead, TRI);
15460b57cec5SDimitry Andric       MIB = AddDReg(MIB, DestReg, ARM::dsub_6, RegState::DefineNoRead, TRI);
15470b57cec5SDimitry Andric       MIB = AddDReg(MIB, DestReg, ARM::dsub_7, RegState::DefineNoRead, TRI);
1548bdd1243dSDimitry Andric       if (DestReg.isPhysical())
15490b57cec5SDimitry Andric         MIB.addReg(DestReg, RegState::ImplicitDefine);
15500b57cec5SDimitry Andric     } else
15510b57cec5SDimitry Andric       llvm_unreachable("Unknown reg class!");
15520b57cec5SDimitry Andric     break;
15530b57cec5SDimitry Andric   default:
15540b57cec5SDimitry Andric     llvm_unreachable("Unknown regclass!");
15550b57cec5SDimitry Andric   }
15560b57cec5SDimitry Andric }
15570b57cec5SDimitry Andric 
15580b57cec5SDimitry Andric unsigned ARMBaseInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
15590b57cec5SDimitry Andric                                                int &FrameIndex) const {
15600b57cec5SDimitry Andric   switch (MI.getOpcode()) {
15610b57cec5SDimitry Andric   default: break;
15620b57cec5SDimitry Andric   case ARM::LDRrs:
15630b57cec5SDimitry Andric   case ARM::t2LDRs:  // FIXME: don't use t2LDRs to access frame.
15640b57cec5SDimitry Andric     if (MI.getOperand(1).isFI() && MI.getOperand(2).isReg() &&
15650b57cec5SDimitry Andric         MI.getOperand(3).isImm() && MI.getOperand(2).getReg() == 0 &&
15660b57cec5SDimitry Andric         MI.getOperand(3).getImm() == 0) {
15670b57cec5SDimitry Andric       FrameIndex = MI.getOperand(1).getIndex();
15680b57cec5SDimitry Andric       return MI.getOperand(0).getReg();
15690b57cec5SDimitry Andric     }
15700b57cec5SDimitry Andric     break;
15710b57cec5SDimitry Andric   case ARM::LDRi12:
15720b57cec5SDimitry Andric   case ARM::t2LDRi12:
15730b57cec5SDimitry Andric   case ARM::tLDRspi:
15740b57cec5SDimitry Andric   case ARM::VLDRD:
15750b57cec5SDimitry Andric   case ARM::VLDRS:
1576fe6060f1SDimitry Andric   case ARM::VLDR_P0_off:
1577fe6060f1SDimitry Andric   case ARM::MVE_VLDRWU32:
15780b57cec5SDimitry Andric     if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
15790b57cec5SDimitry Andric         MI.getOperand(2).getImm() == 0) {
15800b57cec5SDimitry Andric       FrameIndex = MI.getOperand(1).getIndex();
15810b57cec5SDimitry Andric       return MI.getOperand(0).getReg();
15820b57cec5SDimitry Andric     }
15830b57cec5SDimitry Andric     break;
15840b57cec5SDimitry Andric   case ARM::VLD1q64:
15850b57cec5SDimitry Andric   case ARM::VLD1d8TPseudo:
15860b57cec5SDimitry Andric   case ARM::VLD1d16TPseudo:
15870b57cec5SDimitry Andric   case ARM::VLD1d32TPseudo:
15880b57cec5SDimitry Andric   case ARM::VLD1d64TPseudo:
15890b57cec5SDimitry Andric   case ARM::VLD1d8QPseudo:
15900b57cec5SDimitry Andric   case ARM::VLD1d16QPseudo:
15910b57cec5SDimitry Andric   case ARM::VLD1d32QPseudo:
15920b57cec5SDimitry Andric   case ARM::VLD1d64QPseudo:
15930b57cec5SDimitry Andric     if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
15940b57cec5SDimitry Andric       FrameIndex = MI.getOperand(1).getIndex();
15950b57cec5SDimitry Andric       return MI.getOperand(0).getReg();
15960b57cec5SDimitry Andric     }
15970b57cec5SDimitry Andric     break;
15980b57cec5SDimitry Andric   case ARM::VLDMQIA:
15990b57cec5SDimitry Andric     if (MI.getOperand(1).isFI() && MI.getOperand(0).getSubReg() == 0) {
16000b57cec5SDimitry Andric       FrameIndex = MI.getOperand(1).getIndex();
16010b57cec5SDimitry Andric       return MI.getOperand(0).getReg();
16020b57cec5SDimitry Andric     }
16030b57cec5SDimitry Andric     break;
1604349cc55cSDimitry Andric   case ARM::MQQPRLoad:
1605349cc55cSDimitry Andric   case ARM::MQQQQPRLoad:
1606349cc55cSDimitry Andric     if (MI.getOperand(1).isFI()) {
1607349cc55cSDimitry Andric       FrameIndex = MI.getOperand(1).getIndex();
1608349cc55cSDimitry Andric       return MI.getOperand(0).getReg();
1609349cc55cSDimitry Andric     }
1610349cc55cSDimitry Andric     break;
16110b57cec5SDimitry Andric   }
16120b57cec5SDimitry Andric 
16130b57cec5SDimitry Andric   return 0;
16140b57cec5SDimitry Andric }
16150b57cec5SDimitry Andric 
16160b57cec5SDimitry Andric unsigned ARMBaseInstrInfo::isLoadFromStackSlotPostFE(const MachineInstr &MI,
16170b57cec5SDimitry Andric                                                      int &FrameIndex) const {
16180b57cec5SDimitry Andric   SmallVector<const MachineMemOperand *, 1> Accesses;
16190b57cec5SDimitry Andric   if (MI.mayLoad() && hasLoadFromStackSlot(MI, Accesses) &&
16200b57cec5SDimitry Andric       Accesses.size() == 1) {
16210b57cec5SDimitry Andric     FrameIndex =
16220b57cec5SDimitry Andric         cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
16230b57cec5SDimitry Andric             ->getFrameIndex();
16240b57cec5SDimitry Andric     return true;
16250b57cec5SDimitry Andric   }
16260b57cec5SDimitry Andric   return false;
16270b57cec5SDimitry Andric }
16280b57cec5SDimitry Andric 
16290b57cec5SDimitry Andric /// Expands MEMCPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD
16300b57cec5SDimitry Andric /// depending on whether the result is used.
16310b57cec5SDimitry Andric void ARMBaseInstrInfo::expandMEMCPY(MachineBasicBlock::iterator MI) const {
16320b57cec5SDimitry Andric   bool isThumb1 = Subtarget.isThumb1Only();
16330b57cec5SDimitry Andric   bool isThumb2 = Subtarget.isThumb2();
16340b57cec5SDimitry Andric   const ARMBaseInstrInfo *TII = Subtarget.getInstrInfo();
16350b57cec5SDimitry Andric 
16360b57cec5SDimitry Andric   DebugLoc dl = MI->getDebugLoc();
16370b57cec5SDimitry Andric   MachineBasicBlock *BB = MI->getParent();
16380b57cec5SDimitry Andric 
16390b57cec5SDimitry Andric   MachineInstrBuilder LDM, STM;
16400b57cec5SDimitry Andric   if (isThumb1 || !MI->getOperand(1).isDead()) {
16410b57cec5SDimitry Andric     MachineOperand LDWb(MI->getOperand(1));
16420b57cec5SDimitry Andric     LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA_UPD
16430b57cec5SDimitry Andric                                                  : isThumb1 ? ARM::tLDMIA_UPD
16440b57cec5SDimitry Andric                                                             : ARM::LDMIA_UPD))
16450b57cec5SDimitry Andric               .add(LDWb);
16460b57cec5SDimitry Andric   } else {
16470b57cec5SDimitry Andric     LDM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA : ARM::LDMIA));
16480b57cec5SDimitry Andric   }
16490b57cec5SDimitry Andric 
16500b57cec5SDimitry Andric   if (isThumb1 || !MI->getOperand(0).isDead()) {
16510b57cec5SDimitry Andric     MachineOperand STWb(MI->getOperand(0));
16520b57cec5SDimitry Andric     STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA_UPD
16530b57cec5SDimitry Andric                                                  : isThumb1 ? ARM::tSTMIA_UPD
16540b57cec5SDimitry Andric                                                             : ARM::STMIA_UPD))
16550b57cec5SDimitry Andric               .add(STWb);
16560b57cec5SDimitry Andric   } else {
16570b57cec5SDimitry Andric     STM = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA : ARM::STMIA));
16580b57cec5SDimitry Andric   }
16590b57cec5SDimitry Andric 
16600b57cec5SDimitry Andric   MachineOperand LDBase(MI->getOperand(3));
16610b57cec5SDimitry Andric   LDM.add(LDBase).add(predOps(ARMCC::AL));
16620b57cec5SDimitry Andric 
16630b57cec5SDimitry Andric   MachineOperand STBase(MI->getOperand(2));
16640b57cec5SDimitry Andric   STM.add(STBase).add(predOps(ARMCC::AL));
16650b57cec5SDimitry Andric 
16660b57cec5SDimitry Andric   // Sort the scratch registers into ascending order.
16670b57cec5SDimitry Andric   const TargetRegisterInfo &TRI = getRegisterInfo();
16680b57cec5SDimitry Andric   SmallVector<unsigned, 6> ScratchRegs;
166906c3fb27SDimitry Andric   for (MachineOperand &MO : llvm::drop_begin(MI->operands(), 5))
167006c3fb27SDimitry Andric     ScratchRegs.push_back(MO.getReg());
16710b57cec5SDimitry Andric   llvm::sort(ScratchRegs,
16720b57cec5SDimitry Andric              [&TRI](const unsigned &Reg1, const unsigned &Reg2) -> bool {
16730b57cec5SDimitry Andric                return TRI.getEncodingValue(Reg1) <
16740b57cec5SDimitry Andric                       TRI.getEncodingValue(Reg2);
16750b57cec5SDimitry Andric              });
16760b57cec5SDimitry Andric 
16770b57cec5SDimitry Andric   for (const auto &Reg : ScratchRegs) {
16780b57cec5SDimitry Andric     LDM.addReg(Reg, RegState::Define);
16790b57cec5SDimitry Andric     STM.addReg(Reg, RegState::Kill);
16800b57cec5SDimitry Andric   }
16810b57cec5SDimitry Andric 
16820b57cec5SDimitry Andric   BB->erase(MI);
16830b57cec5SDimitry Andric }
16840b57cec5SDimitry Andric 
16850b57cec5SDimitry Andric bool ARMBaseInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
16860b57cec5SDimitry Andric   if (MI.getOpcode() == TargetOpcode::LOAD_STACK_GUARD) {
16870b57cec5SDimitry Andric     expandLoadStackGuard(MI);
16880b57cec5SDimitry Andric     MI.getParent()->erase(MI);
16890b57cec5SDimitry Andric     return true;
16900b57cec5SDimitry Andric   }
16910b57cec5SDimitry Andric 
16920b57cec5SDimitry Andric   if (MI.getOpcode() == ARM::MEMCPY) {
16930b57cec5SDimitry Andric     expandMEMCPY(MI);
16940b57cec5SDimitry Andric     return true;
16950b57cec5SDimitry Andric   }
16960b57cec5SDimitry Andric 
16970b57cec5SDimitry Andric   // This hook gets to expand COPY instructions before they become
16980b57cec5SDimitry Andric   // copyPhysReg() calls.  Look for VMOVS instructions that can legally be
16990b57cec5SDimitry Andric   // widened to VMOVD.  We prefer the VMOVD when possible because it may be
17000b57cec5SDimitry Andric   // changed into a VORR that can go down the NEON pipeline.
17010b57cec5SDimitry Andric   if (!MI.isCopy() || Subtarget.dontWidenVMOVS() || !Subtarget.hasFP64())
17020b57cec5SDimitry Andric     return false;
17030b57cec5SDimitry Andric 
17040b57cec5SDimitry Andric   // Look for a copy between even S-registers.  That is where we keep floats
17050b57cec5SDimitry Andric   // when using NEON v2f32 instructions for f32 arithmetic.
17068bcb0991SDimitry Andric   Register DstRegS = MI.getOperand(0).getReg();
17078bcb0991SDimitry Andric   Register SrcRegS = MI.getOperand(1).getReg();
17080b57cec5SDimitry Andric   if (!ARM::SPRRegClass.contains(DstRegS, SrcRegS))
17090b57cec5SDimitry Andric     return false;
17100b57cec5SDimitry Andric 
17110b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = &getRegisterInfo();
17120b57cec5SDimitry Andric   unsigned DstRegD = TRI->getMatchingSuperReg(DstRegS, ARM::ssub_0,
17130b57cec5SDimitry Andric                                               &ARM::DPRRegClass);
17140b57cec5SDimitry Andric   unsigned SrcRegD = TRI->getMatchingSuperReg(SrcRegS, ARM::ssub_0,
17150b57cec5SDimitry Andric                                               &ARM::DPRRegClass);
17160b57cec5SDimitry Andric   if (!DstRegD || !SrcRegD)
17170b57cec5SDimitry Andric     return false;
17180b57cec5SDimitry Andric 
17190b57cec5SDimitry Andric   // We want to widen this into a DstRegD = VMOVD SrcRegD copy.  This is only
17200b57cec5SDimitry Andric   // legal if the COPY already defines the full DstRegD, and it isn't a
17210b57cec5SDimitry Andric   // sub-register insertion.
17220b57cec5SDimitry Andric   if (!MI.definesRegister(DstRegD, TRI) || MI.readsRegister(DstRegD, TRI))
17230b57cec5SDimitry Andric     return false;
17240b57cec5SDimitry Andric 
17250b57cec5SDimitry Andric   // A dead copy shouldn't show up here, but reject it just in case.
17260b57cec5SDimitry Andric   if (MI.getOperand(0).isDead())
17270b57cec5SDimitry Andric     return false;
17280b57cec5SDimitry Andric 
17290b57cec5SDimitry Andric   // All clear, widen the COPY.
17300b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "widening:    " << MI);
17310b57cec5SDimitry Andric   MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
17320b57cec5SDimitry Andric 
17330b57cec5SDimitry Andric   // Get rid of the old implicit-def of DstRegD.  Leave it if it defines a Q-reg
17340b57cec5SDimitry Andric   // or some other super-register.
17350b57cec5SDimitry Andric   int ImpDefIdx = MI.findRegisterDefOperandIdx(DstRegD);
17360b57cec5SDimitry Andric   if (ImpDefIdx != -1)
173781ad6265SDimitry Andric     MI.removeOperand(ImpDefIdx);
17380b57cec5SDimitry Andric 
17390b57cec5SDimitry Andric   // Change the opcode and operands.
17400b57cec5SDimitry Andric   MI.setDesc(get(ARM::VMOVD));
17410b57cec5SDimitry Andric   MI.getOperand(0).setReg(DstRegD);
17420b57cec5SDimitry Andric   MI.getOperand(1).setReg(SrcRegD);
17430b57cec5SDimitry Andric   MIB.add(predOps(ARMCC::AL));
17440b57cec5SDimitry Andric 
17450b57cec5SDimitry Andric   // We are now reading SrcRegD instead of SrcRegS.  This may upset the
17460b57cec5SDimitry Andric   // register scavenger and machine verifier, so we need to indicate that we
17470b57cec5SDimitry Andric   // are reading an undefined value from SrcRegD, but a proper value from
17480b57cec5SDimitry Andric   // SrcRegS.
17490b57cec5SDimitry Andric   MI.getOperand(1).setIsUndef();
17500b57cec5SDimitry Andric   MIB.addReg(SrcRegS, RegState::Implicit);
17510b57cec5SDimitry Andric 
17520b57cec5SDimitry Andric   // SrcRegD may actually contain an unrelated value in the ssub_1
17530b57cec5SDimitry Andric   // sub-register.  Don't kill it.  Only kill the ssub_0 sub-register.
17540b57cec5SDimitry Andric   if (MI.getOperand(1).isKill()) {
17550b57cec5SDimitry Andric     MI.getOperand(1).setIsKill(false);
17560b57cec5SDimitry Andric     MI.addRegisterKilled(SrcRegS, TRI, true);
17570b57cec5SDimitry Andric   }
17580b57cec5SDimitry Andric 
17590b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "replaced by: " << MI);
17600b57cec5SDimitry Andric   return true;
17610b57cec5SDimitry Andric }
17620b57cec5SDimitry Andric 
17630b57cec5SDimitry Andric /// Create a copy of a const pool value. Update CPI to the new index and return
17640b57cec5SDimitry Andric /// the label UID.
17650b57cec5SDimitry Andric static unsigned duplicateCPV(MachineFunction &MF, unsigned &CPI) {
17660b57cec5SDimitry Andric   MachineConstantPool *MCP = MF.getConstantPool();
17670b57cec5SDimitry Andric   ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
17680b57cec5SDimitry Andric 
17690b57cec5SDimitry Andric   const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPI];
17700b57cec5SDimitry Andric   assert(MCPE.isMachineConstantPoolEntry() &&
17710b57cec5SDimitry Andric          "Expecting a machine constantpool entry!");
17720b57cec5SDimitry Andric   ARMConstantPoolValue *ACPV =
17730b57cec5SDimitry Andric     static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal);
17740b57cec5SDimitry Andric 
17750b57cec5SDimitry Andric   unsigned PCLabelId = AFI->createPICLabelUId();
17760b57cec5SDimitry Andric   ARMConstantPoolValue *NewCPV = nullptr;
17770b57cec5SDimitry Andric 
17780b57cec5SDimitry Andric   // FIXME: The below assumes PIC relocation model and that the function
17790b57cec5SDimitry Andric   // is Thumb mode (t1 or t2). PCAdjustment would be 8 for ARM mode PIC, and
17800b57cec5SDimitry Andric   // zero for non-PIC in ARM or Thumb. The callers are all of thumb LDR
17810b57cec5SDimitry Andric   // instructions, so that's probably OK, but is PIC always correct when
17820b57cec5SDimitry Andric   // we get here?
17830b57cec5SDimitry Andric   if (ACPV->isGlobalValue())
17840b57cec5SDimitry Andric     NewCPV = ARMConstantPoolConstant::Create(
17850b57cec5SDimitry Andric         cast<ARMConstantPoolConstant>(ACPV)->getGV(), PCLabelId, ARMCP::CPValue,
17860b57cec5SDimitry Andric         4, ACPV->getModifier(), ACPV->mustAddCurrentAddress());
17870b57cec5SDimitry Andric   else if (ACPV->isExtSymbol())
17880b57cec5SDimitry Andric     NewCPV = ARMConstantPoolSymbol::
17890b57cec5SDimitry Andric       Create(MF.getFunction().getContext(),
17900b57cec5SDimitry Andric              cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(), PCLabelId, 4);
17910b57cec5SDimitry Andric   else if (ACPV->isBlockAddress())
17920b57cec5SDimitry Andric     NewCPV = ARMConstantPoolConstant::
17930b57cec5SDimitry Andric       Create(cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress(), PCLabelId,
17940b57cec5SDimitry Andric              ARMCP::CPBlockAddress, 4);
17950b57cec5SDimitry Andric   else if (ACPV->isLSDA())
17960b57cec5SDimitry Andric     NewCPV = ARMConstantPoolConstant::Create(&MF.getFunction(), PCLabelId,
17970b57cec5SDimitry Andric                                              ARMCP::CPLSDA, 4);
17980b57cec5SDimitry Andric   else if (ACPV->isMachineBasicBlock())
17990b57cec5SDimitry Andric     NewCPV = ARMConstantPoolMBB::
18000b57cec5SDimitry Andric       Create(MF.getFunction().getContext(),
18010b57cec5SDimitry Andric              cast<ARMConstantPoolMBB>(ACPV)->getMBB(), PCLabelId, 4);
18020b57cec5SDimitry Andric   else
18030b57cec5SDimitry Andric     llvm_unreachable("Unexpected ARM constantpool value type!!");
18045ffd83dbSDimitry Andric   CPI = MCP->getConstantPoolIndex(NewCPV, MCPE.getAlign());
18050b57cec5SDimitry Andric   return PCLabelId;
18060b57cec5SDimitry Andric }
18070b57cec5SDimitry Andric 
18080b57cec5SDimitry Andric void ARMBaseInstrInfo::reMaterialize(MachineBasicBlock &MBB,
18090b57cec5SDimitry Andric                                      MachineBasicBlock::iterator I,
18105ffd83dbSDimitry Andric                                      Register DestReg, unsigned SubIdx,
18110b57cec5SDimitry Andric                                      const MachineInstr &Orig,
18120b57cec5SDimitry Andric                                      const TargetRegisterInfo &TRI) const {
18130b57cec5SDimitry Andric   unsigned Opcode = Orig.getOpcode();
18140b57cec5SDimitry Andric   switch (Opcode) {
18150b57cec5SDimitry Andric   default: {
18160b57cec5SDimitry Andric     MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
18170b57cec5SDimitry Andric     MI->substituteRegister(Orig.getOperand(0).getReg(), DestReg, SubIdx, TRI);
18180b57cec5SDimitry Andric     MBB.insert(I, MI);
18190b57cec5SDimitry Andric     break;
18200b57cec5SDimitry Andric   }
18210b57cec5SDimitry Andric   case ARM::tLDRpci_pic:
18220b57cec5SDimitry Andric   case ARM::t2LDRpci_pic: {
18230b57cec5SDimitry Andric     MachineFunction &MF = *MBB.getParent();
18240b57cec5SDimitry Andric     unsigned CPI = Orig.getOperand(1).getIndex();
18250b57cec5SDimitry Andric     unsigned PCLabelId = duplicateCPV(MF, CPI);
18260b57cec5SDimitry Andric     BuildMI(MBB, I, Orig.getDebugLoc(), get(Opcode), DestReg)
18270b57cec5SDimitry Andric         .addConstantPoolIndex(CPI)
18280b57cec5SDimitry Andric         .addImm(PCLabelId)
18290b57cec5SDimitry Andric         .cloneMemRefs(Orig);
18300b57cec5SDimitry Andric     break;
18310b57cec5SDimitry Andric   }
18320b57cec5SDimitry Andric   }
18330b57cec5SDimitry Andric }
18340b57cec5SDimitry Andric 
18350b57cec5SDimitry Andric MachineInstr &
18360b57cec5SDimitry Andric ARMBaseInstrInfo::duplicate(MachineBasicBlock &MBB,
18370b57cec5SDimitry Andric     MachineBasicBlock::iterator InsertBefore,
18380b57cec5SDimitry Andric     const MachineInstr &Orig) const {
18390b57cec5SDimitry Andric   MachineInstr &Cloned = TargetInstrInfo::duplicate(MBB, InsertBefore, Orig);
18400b57cec5SDimitry Andric   MachineBasicBlock::instr_iterator I = Cloned.getIterator();
18410b57cec5SDimitry Andric   for (;;) {
18420b57cec5SDimitry Andric     switch (I->getOpcode()) {
18430b57cec5SDimitry Andric     case ARM::tLDRpci_pic:
18440b57cec5SDimitry Andric     case ARM::t2LDRpci_pic: {
18450b57cec5SDimitry Andric       MachineFunction &MF = *MBB.getParent();
18460b57cec5SDimitry Andric       unsigned CPI = I->getOperand(1).getIndex();
18470b57cec5SDimitry Andric       unsigned PCLabelId = duplicateCPV(MF, CPI);
18480b57cec5SDimitry Andric       I->getOperand(1).setIndex(CPI);
18490b57cec5SDimitry Andric       I->getOperand(2).setImm(PCLabelId);
18500b57cec5SDimitry Andric       break;
18510b57cec5SDimitry Andric     }
18520b57cec5SDimitry Andric     }
18530b57cec5SDimitry Andric     if (!I->isBundledWithSucc())
18540b57cec5SDimitry Andric       break;
18550b57cec5SDimitry Andric     ++I;
18560b57cec5SDimitry Andric   }
18570b57cec5SDimitry Andric   return Cloned;
18580b57cec5SDimitry Andric }
18590b57cec5SDimitry Andric 
18600b57cec5SDimitry Andric bool ARMBaseInstrInfo::produceSameValue(const MachineInstr &MI0,
18610b57cec5SDimitry Andric                                         const MachineInstr &MI1,
18620b57cec5SDimitry Andric                                         const MachineRegisterInfo *MRI) const {
18630b57cec5SDimitry Andric   unsigned Opcode = MI0.getOpcode();
18644824e7fdSDimitry Andric   if (Opcode == ARM::t2LDRpci || Opcode == ARM::t2LDRpci_pic ||
18654824e7fdSDimitry Andric       Opcode == ARM::tLDRpci || Opcode == ARM::tLDRpci_pic ||
18664824e7fdSDimitry Andric       Opcode == ARM::LDRLIT_ga_pcrel || Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
18674824e7fdSDimitry Andric       Opcode == ARM::tLDRLIT_ga_pcrel || Opcode == ARM::t2LDRLIT_ga_pcrel ||
18684824e7fdSDimitry Andric       Opcode == ARM::MOV_ga_pcrel || Opcode == ARM::MOV_ga_pcrel_ldr ||
18690b57cec5SDimitry Andric       Opcode == ARM::t2MOV_ga_pcrel) {
18700b57cec5SDimitry Andric     if (MI1.getOpcode() != Opcode)
18710b57cec5SDimitry Andric       return false;
18720b57cec5SDimitry Andric     if (MI0.getNumOperands() != MI1.getNumOperands())
18730b57cec5SDimitry Andric       return false;
18740b57cec5SDimitry Andric 
18750b57cec5SDimitry Andric     const MachineOperand &MO0 = MI0.getOperand(1);
18760b57cec5SDimitry Andric     const MachineOperand &MO1 = MI1.getOperand(1);
18770b57cec5SDimitry Andric     if (MO0.getOffset() != MO1.getOffset())
18780b57cec5SDimitry Andric       return false;
18790b57cec5SDimitry Andric 
18804824e7fdSDimitry Andric     if (Opcode == ARM::LDRLIT_ga_pcrel || Opcode == ARM::LDRLIT_ga_pcrel_ldr ||
18814824e7fdSDimitry Andric         Opcode == ARM::tLDRLIT_ga_pcrel || Opcode == ARM::t2LDRLIT_ga_pcrel ||
18824824e7fdSDimitry Andric         Opcode == ARM::MOV_ga_pcrel || Opcode == ARM::MOV_ga_pcrel_ldr ||
18830b57cec5SDimitry Andric         Opcode == ARM::t2MOV_ga_pcrel)
18840b57cec5SDimitry Andric       // Ignore the PC labels.
18850b57cec5SDimitry Andric       return MO0.getGlobal() == MO1.getGlobal();
18860b57cec5SDimitry Andric 
18870b57cec5SDimitry Andric     const MachineFunction *MF = MI0.getParent()->getParent();
18880b57cec5SDimitry Andric     const MachineConstantPool *MCP = MF->getConstantPool();
18890b57cec5SDimitry Andric     int CPI0 = MO0.getIndex();
18900b57cec5SDimitry Andric     int CPI1 = MO1.getIndex();
18910b57cec5SDimitry Andric     const MachineConstantPoolEntry &MCPE0 = MCP->getConstants()[CPI0];
18920b57cec5SDimitry Andric     const MachineConstantPoolEntry &MCPE1 = MCP->getConstants()[CPI1];
18930b57cec5SDimitry Andric     bool isARMCP0 = MCPE0.isMachineConstantPoolEntry();
18940b57cec5SDimitry Andric     bool isARMCP1 = MCPE1.isMachineConstantPoolEntry();
18950b57cec5SDimitry Andric     if (isARMCP0 && isARMCP1) {
18960b57cec5SDimitry Andric       ARMConstantPoolValue *ACPV0 =
18970b57cec5SDimitry Andric         static_cast<ARMConstantPoolValue*>(MCPE0.Val.MachineCPVal);
18980b57cec5SDimitry Andric       ARMConstantPoolValue *ACPV1 =
18990b57cec5SDimitry Andric         static_cast<ARMConstantPoolValue*>(MCPE1.Val.MachineCPVal);
19000b57cec5SDimitry Andric       return ACPV0->hasSameValue(ACPV1);
19010b57cec5SDimitry Andric     } else if (!isARMCP0 && !isARMCP1) {
19020b57cec5SDimitry Andric       return MCPE0.Val.ConstVal == MCPE1.Val.ConstVal;
19030b57cec5SDimitry Andric     }
19040b57cec5SDimitry Andric     return false;
19050b57cec5SDimitry Andric   } else if (Opcode == ARM::PICLDR) {
19060b57cec5SDimitry Andric     if (MI1.getOpcode() != Opcode)
19070b57cec5SDimitry Andric       return false;
19080b57cec5SDimitry Andric     if (MI0.getNumOperands() != MI1.getNumOperands())
19090b57cec5SDimitry Andric       return false;
19100b57cec5SDimitry Andric 
19118bcb0991SDimitry Andric     Register Addr0 = MI0.getOperand(1).getReg();
19128bcb0991SDimitry Andric     Register Addr1 = MI1.getOperand(1).getReg();
19130b57cec5SDimitry Andric     if (Addr0 != Addr1) {
1914bdd1243dSDimitry Andric       if (!MRI || !Addr0.isVirtual() || !Addr1.isVirtual())
19150b57cec5SDimitry Andric         return false;
19160b57cec5SDimitry Andric 
19170b57cec5SDimitry Andric       // This assumes SSA form.
19180b57cec5SDimitry Andric       MachineInstr *Def0 = MRI->getVRegDef(Addr0);
19190b57cec5SDimitry Andric       MachineInstr *Def1 = MRI->getVRegDef(Addr1);
19200b57cec5SDimitry Andric       // Check if the loaded value, e.g. a constantpool of a global address, are
19210b57cec5SDimitry Andric       // the same.
19220b57cec5SDimitry Andric       if (!produceSameValue(*Def0, *Def1, MRI))
19230b57cec5SDimitry Andric         return false;
19240b57cec5SDimitry Andric     }
19250b57cec5SDimitry Andric 
19260b57cec5SDimitry Andric     for (unsigned i = 3, e = MI0.getNumOperands(); i != e; ++i) {
19270b57cec5SDimitry Andric       // %12 = PICLDR %11, 0, 14, %noreg
19280b57cec5SDimitry Andric       const MachineOperand &MO0 = MI0.getOperand(i);
19290b57cec5SDimitry Andric       const MachineOperand &MO1 = MI1.getOperand(i);
19300b57cec5SDimitry Andric       if (!MO0.isIdenticalTo(MO1))
19310b57cec5SDimitry Andric         return false;
19320b57cec5SDimitry Andric     }
19330b57cec5SDimitry Andric     return true;
19340b57cec5SDimitry Andric   }
19350b57cec5SDimitry Andric 
19360b57cec5SDimitry Andric   return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
19370b57cec5SDimitry Andric }
19380b57cec5SDimitry Andric 
19390b57cec5SDimitry Andric /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
19400b57cec5SDimitry Andric /// determine if two loads are loading from the same base address. It should
19410b57cec5SDimitry Andric /// only return true if the base pointers are the same and the only differences
19420b57cec5SDimitry Andric /// between the two addresses is the offset. It also returns the offsets by
19430b57cec5SDimitry Andric /// reference.
19440b57cec5SDimitry Andric ///
19450b57cec5SDimitry Andric /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
19460b57cec5SDimitry Andric /// is permanently disabled.
19470b57cec5SDimitry Andric bool ARMBaseInstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
19480b57cec5SDimitry Andric                                                int64_t &Offset1,
19490b57cec5SDimitry Andric                                                int64_t &Offset2) const {
19500b57cec5SDimitry Andric   // Don't worry about Thumb: just ARM and Thumb2.
19510b57cec5SDimitry Andric   if (Subtarget.isThumb1Only()) return false;
19520b57cec5SDimitry Andric 
19530b57cec5SDimitry Andric   if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
19540b57cec5SDimitry Andric     return false;
19550b57cec5SDimitry Andric 
1956*5f757f3fSDimitry Andric   auto IsLoadOpcode = [&](unsigned Opcode) {
1957*5f757f3fSDimitry Andric     switch (Opcode) {
19580b57cec5SDimitry Andric     default:
19590b57cec5SDimitry Andric       return false;
19600b57cec5SDimitry Andric     case ARM::LDRi12:
19610b57cec5SDimitry Andric     case ARM::LDRBi12:
19620b57cec5SDimitry Andric     case ARM::LDRD:
19630b57cec5SDimitry Andric     case ARM::LDRH:
19640b57cec5SDimitry Andric     case ARM::LDRSB:
19650b57cec5SDimitry Andric     case ARM::LDRSH:
19660b57cec5SDimitry Andric     case ARM::VLDRD:
19670b57cec5SDimitry Andric     case ARM::VLDRS:
19680b57cec5SDimitry Andric     case ARM::t2LDRi8:
19690b57cec5SDimitry Andric     case ARM::t2LDRBi8:
19700b57cec5SDimitry Andric     case ARM::t2LDRDi8:
19710b57cec5SDimitry Andric     case ARM::t2LDRSHi8:
19720b57cec5SDimitry Andric     case ARM::t2LDRi12:
19730b57cec5SDimitry Andric     case ARM::t2LDRBi12:
19740b57cec5SDimitry Andric     case ARM::t2LDRSHi12:
1975*5f757f3fSDimitry Andric       return true;
19760b57cec5SDimitry Andric     }
1977*5f757f3fSDimitry Andric   };
19780b57cec5SDimitry Andric 
1979*5f757f3fSDimitry Andric   if (!IsLoadOpcode(Load1->getMachineOpcode()) ||
1980*5f757f3fSDimitry Andric       !IsLoadOpcode(Load2->getMachineOpcode()))
19810b57cec5SDimitry Andric     return false;
19820b57cec5SDimitry Andric 
19830b57cec5SDimitry Andric   // Check if base addresses and chain operands match.
19840b57cec5SDimitry Andric   if (Load1->getOperand(0) != Load2->getOperand(0) ||
19850b57cec5SDimitry Andric       Load1->getOperand(4) != Load2->getOperand(4))
19860b57cec5SDimitry Andric     return false;
19870b57cec5SDimitry Andric 
19880b57cec5SDimitry Andric   // Index should be Reg0.
19890b57cec5SDimitry Andric   if (Load1->getOperand(3) != Load2->getOperand(3))
19900b57cec5SDimitry Andric     return false;
19910b57cec5SDimitry Andric 
19920b57cec5SDimitry Andric   // Determine the offsets.
19930b57cec5SDimitry Andric   if (isa<ConstantSDNode>(Load1->getOperand(1)) &&
19940b57cec5SDimitry Andric       isa<ConstantSDNode>(Load2->getOperand(1))) {
19950b57cec5SDimitry Andric     Offset1 = cast<ConstantSDNode>(Load1->getOperand(1))->getSExtValue();
19960b57cec5SDimitry Andric     Offset2 = cast<ConstantSDNode>(Load2->getOperand(1))->getSExtValue();
19970b57cec5SDimitry Andric     return true;
19980b57cec5SDimitry Andric   }
19990b57cec5SDimitry Andric 
20000b57cec5SDimitry Andric   return false;
20010b57cec5SDimitry Andric }
20020b57cec5SDimitry Andric 
20030b57cec5SDimitry Andric /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
20040b57cec5SDimitry Andric /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads should
20050b57cec5SDimitry Andric /// be scheduled togther. On some targets if two loads are loading from
20060b57cec5SDimitry Andric /// addresses in the same cache line, it's better if they are scheduled
20070b57cec5SDimitry Andric /// together. This function takes two integers that represent the load offsets
20080b57cec5SDimitry Andric /// from the common base address. It returns true if it decides it's desirable
20090b57cec5SDimitry Andric /// to schedule the two loads together. "NumLoads" is the number of loads that
20100b57cec5SDimitry Andric /// have already been scheduled after Load1.
20110b57cec5SDimitry Andric ///
20120b57cec5SDimitry Andric /// FIXME: remove this in favor of the MachineInstr interface once pre-RA-sched
20130b57cec5SDimitry Andric /// is permanently disabled.
20140b57cec5SDimitry Andric bool ARMBaseInstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
20150b57cec5SDimitry Andric                                                int64_t Offset1, int64_t Offset2,
20160b57cec5SDimitry Andric                                                unsigned NumLoads) const {
20170b57cec5SDimitry Andric   // Don't worry about Thumb: just ARM and Thumb2.
20180b57cec5SDimitry Andric   if (Subtarget.isThumb1Only()) return false;
20190b57cec5SDimitry Andric 
20200b57cec5SDimitry Andric   assert(Offset2 > Offset1);
20210b57cec5SDimitry Andric 
20220b57cec5SDimitry Andric   if ((Offset2 - Offset1) / 8 > 64)
20230b57cec5SDimitry Andric     return false;
20240b57cec5SDimitry Andric 
20250b57cec5SDimitry Andric   // Check if the machine opcodes are different. If they are different
20260b57cec5SDimitry Andric   // then we consider them to not be of the same base address,
20270b57cec5SDimitry Andric   // EXCEPT in the case of Thumb2 byte loads where one is LDRBi8 and the other LDRBi12.
20280b57cec5SDimitry Andric   // In this case, they are considered to be the same because they are different
20290b57cec5SDimitry Andric   // encoding forms of the same basic instruction.
20300b57cec5SDimitry Andric   if ((Load1->getMachineOpcode() != Load2->getMachineOpcode()) &&
20310b57cec5SDimitry Andric       !((Load1->getMachineOpcode() == ARM::t2LDRBi8 &&
20320b57cec5SDimitry Andric          Load2->getMachineOpcode() == ARM::t2LDRBi12) ||
20330b57cec5SDimitry Andric         (Load1->getMachineOpcode() == ARM::t2LDRBi12 &&
20340b57cec5SDimitry Andric          Load2->getMachineOpcode() == ARM::t2LDRBi8)))
20350b57cec5SDimitry Andric     return false;  // FIXME: overly conservative?
20360b57cec5SDimitry Andric 
20370b57cec5SDimitry Andric   // Four loads in a row should be sufficient.
20380b57cec5SDimitry Andric   if (NumLoads >= 3)
20390b57cec5SDimitry Andric     return false;
20400b57cec5SDimitry Andric 
20410b57cec5SDimitry Andric   return true;
20420b57cec5SDimitry Andric }
20430b57cec5SDimitry Andric 
20440b57cec5SDimitry Andric bool ARMBaseInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
20450b57cec5SDimitry Andric                                             const MachineBasicBlock *MBB,
20460b57cec5SDimitry Andric                                             const MachineFunction &MF) const {
20470b57cec5SDimitry Andric   // Debug info is never a scheduling boundary. It's necessary to be explicit
20480b57cec5SDimitry Andric   // due to the special treatment of IT instructions below, otherwise a
20490b57cec5SDimitry Andric   // dbg_value followed by an IT will result in the IT instruction being
20500b57cec5SDimitry Andric   // considered a scheduling hazard, which is wrong. It should be the actual
20510b57cec5SDimitry Andric   // instruction preceding the dbg_value instruction(s), just like it is
20520b57cec5SDimitry Andric   // when debug info is not present.
20530b57cec5SDimitry Andric   if (MI.isDebugInstr())
20540b57cec5SDimitry Andric     return false;
20550b57cec5SDimitry Andric 
20560b57cec5SDimitry Andric   // Terminators and labels can't be scheduled around.
20570b57cec5SDimitry Andric   if (MI.isTerminator() || MI.isPosition())
20580b57cec5SDimitry Andric     return true;
20590b57cec5SDimitry Andric 
20605ffd83dbSDimitry Andric   // INLINEASM_BR can jump to another block
20615ffd83dbSDimitry Andric   if (MI.getOpcode() == TargetOpcode::INLINEASM_BR)
20625ffd83dbSDimitry Andric     return true;
20635ffd83dbSDimitry Andric 
206481ad6265SDimitry Andric   if (isSEHInstruction(MI))
206581ad6265SDimitry Andric     return true;
206681ad6265SDimitry Andric 
20670b57cec5SDimitry Andric   // Treat the start of the IT block as a scheduling boundary, but schedule
20680b57cec5SDimitry Andric   // t2IT along with all instructions following it.
20690b57cec5SDimitry Andric   // FIXME: This is a big hammer. But the alternative is to add all potential
20700b57cec5SDimitry Andric   // true and anti dependencies to IT block instructions as implicit operands
20710b57cec5SDimitry Andric   // to the t2IT instruction. The added compile time and complexity does not
20720b57cec5SDimitry Andric   // seem worth it.
20730b57cec5SDimitry Andric   MachineBasicBlock::const_iterator I = MI;
20740b57cec5SDimitry Andric   // Make sure to skip any debug instructions
20750b57cec5SDimitry Andric   while (++I != MBB->end() && I->isDebugInstr())
20760b57cec5SDimitry Andric     ;
20770b57cec5SDimitry Andric   if (I != MBB->end() && I->getOpcode() == ARM::t2IT)
20780b57cec5SDimitry Andric     return true;
20790b57cec5SDimitry Andric 
20800b57cec5SDimitry Andric   // Don't attempt to schedule around any instruction that defines
20810b57cec5SDimitry Andric   // a stack-oriented pointer, as it's unlikely to be profitable. This
20820b57cec5SDimitry Andric   // saves compile time, because it doesn't require every single
20830b57cec5SDimitry Andric   // stack slot reference to depend on the instruction that does the
20840b57cec5SDimitry Andric   // modification.
20850b57cec5SDimitry Andric   // Calls don't actually change the stack pointer, even if they have imp-defs.
20860b57cec5SDimitry Andric   // No ARM calling conventions change the stack pointer. (X86 calling
20870b57cec5SDimitry Andric   // conventions sometimes do).
20880b57cec5SDimitry Andric   if (!MI.isCall() && MI.definesRegister(ARM::SP))
20890b57cec5SDimitry Andric     return true;
20900b57cec5SDimitry Andric 
20910b57cec5SDimitry Andric   return false;
20920b57cec5SDimitry Andric }
20930b57cec5SDimitry Andric 
20940b57cec5SDimitry Andric bool ARMBaseInstrInfo::
20950b57cec5SDimitry Andric isProfitableToIfCvt(MachineBasicBlock &MBB,
20960b57cec5SDimitry Andric                     unsigned NumCycles, unsigned ExtraPredCycles,
20970b57cec5SDimitry Andric                     BranchProbability Probability) const {
20980b57cec5SDimitry Andric   if (!NumCycles)
20990b57cec5SDimitry Andric     return false;
21000b57cec5SDimitry Andric 
21010b57cec5SDimitry Andric   // If we are optimizing for size, see if the branch in the predecessor can be
21020b57cec5SDimitry Andric   // lowered to cbn?z by the constant island lowering pass, and return false if
21030b57cec5SDimitry Andric   // so. This results in a shorter instruction sequence.
21040b57cec5SDimitry Andric   if (MBB.getParent()->getFunction().hasOptSize()) {
21050b57cec5SDimitry Andric     MachineBasicBlock *Pred = *MBB.pred_begin();
21060b57cec5SDimitry Andric     if (!Pred->empty()) {
21070b57cec5SDimitry Andric       MachineInstr *LastMI = &*Pred->rbegin();
21080b57cec5SDimitry Andric       if (LastMI->getOpcode() == ARM::t2Bcc) {
21090b57cec5SDimitry Andric         const TargetRegisterInfo *TRI = &getRegisterInfo();
21100b57cec5SDimitry Andric         MachineInstr *CmpMI = findCMPToFoldIntoCBZ(LastMI, TRI);
21110b57cec5SDimitry Andric         if (CmpMI)
21120b57cec5SDimitry Andric           return false;
21130b57cec5SDimitry Andric       }
21140b57cec5SDimitry Andric     }
21150b57cec5SDimitry Andric   }
21160b57cec5SDimitry Andric   return isProfitableToIfCvt(MBB, NumCycles, ExtraPredCycles,
21170b57cec5SDimitry Andric                              MBB, 0, 0, Probability);
21180b57cec5SDimitry Andric }
21190b57cec5SDimitry Andric 
21200b57cec5SDimitry Andric bool ARMBaseInstrInfo::
21210b57cec5SDimitry Andric isProfitableToIfCvt(MachineBasicBlock &TBB,
21220b57cec5SDimitry Andric                     unsigned TCycles, unsigned TExtra,
21230b57cec5SDimitry Andric                     MachineBasicBlock &FBB,
21240b57cec5SDimitry Andric                     unsigned FCycles, unsigned FExtra,
21250b57cec5SDimitry Andric                     BranchProbability Probability) const {
21260b57cec5SDimitry Andric   if (!TCycles)
21270b57cec5SDimitry Andric     return false;
21280b57cec5SDimitry Andric 
21290b57cec5SDimitry Andric   // In thumb code we often end up trading one branch for a IT block, and
21300b57cec5SDimitry Andric   // if we are cloning the instruction can increase code size. Prevent
21310b57cec5SDimitry Andric   // blocks with multiple predecesors from being ifcvted to prevent this
21320b57cec5SDimitry Andric   // cloning.
21330b57cec5SDimitry Andric   if (Subtarget.isThumb2() && TBB.getParent()->getFunction().hasMinSize()) {
21340b57cec5SDimitry Andric     if (TBB.pred_size() != 1 || FBB.pred_size() != 1)
21350b57cec5SDimitry Andric       return false;
21360b57cec5SDimitry Andric   }
21370b57cec5SDimitry Andric 
21380b57cec5SDimitry Andric   // Attempt to estimate the relative costs of predication versus branching.
21390b57cec5SDimitry Andric   // Here we scale up each component of UnpredCost to avoid precision issue when
21400b57cec5SDimitry Andric   // scaling TCycles/FCycles by Probability.
21410b57cec5SDimitry Andric   const unsigned ScalingUpFactor = 1024;
21420b57cec5SDimitry Andric 
21430b57cec5SDimitry Andric   unsigned PredCost = (TCycles + FCycles + TExtra + FExtra) * ScalingUpFactor;
21440b57cec5SDimitry Andric   unsigned UnpredCost;
21450b57cec5SDimitry Andric   if (!Subtarget.hasBranchPredictor()) {
21460b57cec5SDimitry Andric     // When we don't have a branch predictor it's always cheaper to not take a
21470b57cec5SDimitry Andric     // branch than take it, so we have to take that into account.
21480b57cec5SDimitry Andric     unsigned NotTakenBranchCost = 1;
21490b57cec5SDimitry Andric     unsigned TakenBranchCost = Subtarget.getMispredictionPenalty();
21500b57cec5SDimitry Andric     unsigned TUnpredCycles, FUnpredCycles;
21510b57cec5SDimitry Andric     if (!FCycles) {
21520b57cec5SDimitry Andric       // Triangle: TBB is the fallthrough
21530b57cec5SDimitry Andric       TUnpredCycles = TCycles + NotTakenBranchCost;
21540b57cec5SDimitry Andric       FUnpredCycles = TakenBranchCost;
21550b57cec5SDimitry Andric     } else {
21560b57cec5SDimitry Andric       // Diamond: TBB is the block that is branched to, FBB is the fallthrough
21570b57cec5SDimitry Andric       TUnpredCycles = TCycles + TakenBranchCost;
21580b57cec5SDimitry Andric       FUnpredCycles = FCycles + NotTakenBranchCost;
21590b57cec5SDimitry Andric       // The branch at the end of FBB will disappear when it's predicated, so
21600b57cec5SDimitry Andric       // discount it from PredCost.
21610b57cec5SDimitry Andric       PredCost -= 1 * ScalingUpFactor;
21620b57cec5SDimitry Andric     }
21630b57cec5SDimitry Andric     // The total cost is the cost of each path scaled by their probabilites
21640b57cec5SDimitry Andric     unsigned TUnpredCost = Probability.scale(TUnpredCycles * ScalingUpFactor);
21650b57cec5SDimitry Andric     unsigned FUnpredCost = Probability.getCompl().scale(FUnpredCycles * ScalingUpFactor);
21660b57cec5SDimitry Andric     UnpredCost = TUnpredCost + FUnpredCost;
21670b57cec5SDimitry Andric     // When predicating assume that the first IT can be folded away but later
21680b57cec5SDimitry Andric     // ones cost one cycle each
21690b57cec5SDimitry Andric     if (Subtarget.isThumb2() && TCycles + FCycles > 4) {
21700b57cec5SDimitry Andric       PredCost += ((TCycles + FCycles - 4) / 4) * ScalingUpFactor;
21710b57cec5SDimitry Andric     }
21720b57cec5SDimitry Andric   } else {
21730b57cec5SDimitry Andric     unsigned TUnpredCost = Probability.scale(TCycles * ScalingUpFactor);
21740b57cec5SDimitry Andric     unsigned FUnpredCost =
21750b57cec5SDimitry Andric       Probability.getCompl().scale(FCycles * ScalingUpFactor);
21760b57cec5SDimitry Andric     UnpredCost = TUnpredCost + FUnpredCost;
21770b57cec5SDimitry Andric     UnpredCost += 1 * ScalingUpFactor; // The branch itself
21780b57cec5SDimitry Andric     UnpredCost += Subtarget.getMispredictionPenalty() * ScalingUpFactor / 10;
21790b57cec5SDimitry Andric   }
21800b57cec5SDimitry Andric 
21810b57cec5SDimitry Andric   return PredCost <= UnpredCost;
21820b57cec5SDimitry Andric }
21830b57cec5SDimitry Andric 
21848bcb0991SDimitry Andric unsigned
21858bcb0991SDimitry Andric ARMBaseInstrInfo::extraSizeToPredicateInstructions(const MachineFunction &MF,
21868bcb0991SDimitry Andric                                                    unsigned NumInsts) const {
21878bcb0991SDimitry Andric   // Thumb2 needs a 2-byte IT instruction to predicate up to 4 instructions.
21888bcb0991SDimitry Andric   // ARM has a condition code field in every predicable instruction, using it
21898bcb0991SDimitry Andric   // doesn't change code size.
2190e8d8bef9SDimitry Andric   if (!Subtarget.isThumb2())
2191e8d8bef9SDimitry Andric     return 0;
2192e8d8bef9SDimitry Andric 
2193e8d8bef9SDimitry Andric   // It's possible that the size of the IT is restricted to a single block.
2194e8d8bef9SDimitry Andric   unsigned MaxInsts = Subtarget.restrictIT() ? 1 : 4;
2195e8d8bef9SDimitry Andric   return divideCeil(NumInsts, MaxInsts) * 2;
21968bcb0991SDimitry Andric }
21978bcb0991SDimitry Andric 
21988bcb0991SDimitry Andric unsigned
21998bcb0991SDimitry Andric ARMBaseInstrInfo::predictBranchSizeForIfCvt(MachineInstr &MI) const {
22008bcb0991SDimitry Andric   // If this branch is likely to be folded into the comparison to form a
22018bcb0991SDimitry Andric   // CB(N)Z, then removing it won't reduce code size at all, because that will
22028bcb0991SDimitry Andric   // just replace the CB(N)Z with a CMP.
22038bcb0991SDimitry Andric   if (MI.getOpcode() == ARM::t2Bcc &&
22048bcb0991SDimitry Andric       findCMPToFoldIntoCBZ(&MI, &getRegisterInfo()))
22058bcb0991SDimitry Andric     return 0;
22068bcb0991SDimitry Andric 
22078bcb0991SDimitry Andric   unsigned Size = getInstSizeInBytes(MI);
22088bcb0991SDimitry Andric 
22098bcb0991SDimitry Andric   // For Thumb2, all branches are 32-bit instructions during the if conversion
22108bcb0991SDimitry Andric   // pass, but may be replaced with 16-bit instructions during size reduction.
22118bcb0991SDimitry Andric   // Since the branches considered by if conversion tend to be forward branches
22128bcb0991SDimitry Andric   // over small basic blocks, they are very likely to be in range for the
22138bcb0991SDimitry Andric   // narrow instructions, so we assume the final code size will be half what it
22148bcb0991SDimitry Andric   // currently is.
22158bcb0991SDimitry Andric   if (Subtarget.isThumb2())
22168bcb0991SDimitry Andric     Size /= 2;
22178bcb0991SDimitry Andric 
22188bcb0991SDimitry Andric   return Size;
22198bcb0991SDimitry Andric }
22208bcb0991SDimitry Andric 
22210b57cec5SDimitry Andric bool
22220b57cec5SDimitry Andric ARMBaseInstrInfo::isProfitableToUnpredicate(MachineBasicBlock &TMBB,
22230b57cec5SDimitry Andric                                             MachineBasicBlock &FMBB) const {
22240b57cec5SDimitry Andric   // Reduce false anti-dependencies to let the target's out-of-order execution
22250b57cec5SDimitry Andric   // engine do its thing.
22260b57cec5SDimitry Andric   return Subtarget.isProfitableToUnpredicate();
22270b57cec5SDimitry Andric }
22280b57cec5SDimitry Andric 
22290b57cec5SDimitry Andric /// getInstrPredicate - If instruction is predicated, returns its predicate
22300b57cec5SDimitry Andric /// condition, otherwise returns AL. It also returns the condition code
22310b57cec5SDimitry Andric /// register by reference.
22320b57cec5SDimitry Andric ARMCC::CondCodes llvm::getInstrPredicate(const MachineInstr &MI,
22335ffd83dbSDimitry Andric                                          Register &PredReg) {
22340b57cec5SDimitry Andric   int PIdx = MI.findFirstPredOperandIdx();
22350b57cec5SDimitry Andric   if (PIdx == -1) {
22360b57cec5SDimitry Andric     PredReg = 0;
22370b57cec5SDimitry Andric     return ARMCC::AL;
22380b57cec5SDimitry Andric   }
22390b57cec5SDimitry Andric 
22400b57cec5SDimitry Andric   PredReg = MI.getOperand(PIdx+1).getReg();
22410b57cec5SDimitry Andric   return (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
22420b57cec5SDimitry Andric }
22430b57cec5SDimitry Andric 
22440b57cec5SDimitry Andric unsigned llvm::getMatchingCondBranchOpcode(unsigned Opc) {
22450b57cec5SDimitry Andric   if (Opc == ARM::B)
22460b57cec5SDimitry Andric     return ARM::Bcc;
22470b57cec5SDimitry Andric   if (Opc == ARM::tB)
22480b57cec5SDimitry Andric     return ARM::tBcc;
22490b57cec5SDimitry Andric   if (Opc == ARM::t2B)
22500b57cec5SDimitry Andric     return ARM::t2Bcc;
22510b57cec5SDimitry Andric 
22520b57cec5SDimitry Andric   llvm_unreachable("Unknown unconditional branch opcode!");
22530b57cec5SDimitry Andric }
22540b57cec5SDimitry Andric 
22550b57cec5SDimitry Andric MachineInstr *ARMBaseInstrInfo::commuteInstructionImpl(MachineInstr &MI,
22560b57cec5SDimitry Andric                                                        bool NewMI,
22570b57cec5SDimitry Andric                                                        unsigned OpIdx1,
22580b57cec5SDimitry Andric                                                        unsigned OpIdx2) const {
22590b57cec5SDimitry Andric   switch (MI.getOpcode()) {
22600b57cec5SDimitry Andric   case ARM::MOVCCr:
22610b57cec5SDimitry Andric   case ARM::t2MOVCCr: {
22620b57cec5SDimitry Andric     // MOVCC can be commuted by inverting the condition.
22635ffd83dbSDimitry Andric     Register PredReg;
22640b57cec5SDimitry Andric     ARMCC::CondCodes CC = getInstrPredicate(MI, PredReg);
22650b57cec5SDimitry Andric     // MOVCC AL can't be inverted. Shouldn't happen.
22660b57cec5SDimitry Andric     if (CC == ARMCC::AL || PredReg != ARM::CPSR)
22670b57cec5SDimitry Andric       return nullptr;
22680b57cec5SDimitry Andric     MachineInstr *CommutedMI =
22690b57cec5SDimitry Andric         TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
22700b57cec5SDimitry Andric     if (!CommutedMI)
22710b57cec5SDimitry Andric       return nullptr;
22720b57cec5SDimitry Andric     // After swapping the MOVCC operands, also invert the condition.
22730b57cec5SDimitry Andric     CommutedMI->getOperand(CommutedMI->findFirstPredOperandIdx())
22740b57cec5SDimitry Andric         .setImm(ARMCC::getOppositeCondition(CC));
22750b57cec5SDimitry Andric     return CommutedMI;
22760b57cec5SDimitry Andric   }
22770b57cec5SDimitry Andric   }
22780b57cec5SDimitry Andric   return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
22790b57cec5SDimitry Andric }
22800b57cec5SDimitry Andric 
22810b57cec5SDimitry Andric /// Identify instructions that can be folded into a MOVCC instruction, and
22820b57cec5SDimitry Andric /// return the defining instruction.
22830b57cec5SDimitry Andric MachineInstr *
22845ffd83dbSDimitry Andric ARMBaseInstrInfo::canFoldIntoMOVCC(Register Reg, const MachineRegisterInfo &MRI,
22850b57cec5SDimitry Andric                                    const TargetInstrInfo *TII) const {
22865ffd83dbSDimitry Andric   if (!Reg.isVirtual())
22870b57cec5SDimitry Andric     return nullptr;
22880b57cec5SDimitry Andric   if (!MRI.hasOneNonDBGUse(Reg))
22890b57cec5SDimitry Andric     return nullptr;
22900b57cec5SDimitry Andric   MachineInstr *MI = MRI.getVRegDef(Reg);
22910b57cec5SDimitry Andric   if (!MI)
22920b57cec5SDimitry Andric     return nullptr;
22930b57cec5SDimitry Andric   // Check if MI can be predicated and folded into the MOVCC.
22940b57cec5SDimitry Andric   if (!isPredicable(*MI))
22950b57cec5SDimitry Andric     return nullptr;
22960b57cec5SDimitry Andric   // Check if MI has any non-dead defs or physreg uses. This also detects
22970b57cec5SDimitry Andric   // predicated instructions which will be reading CPSR.
22984824e7fdSDimitry Andric   for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 1)) {
22990b57cec5SDimitry Andric     // Reject frame index operands, PEI can't handle the predicated pseudos.
23000b57cec5SDimitry Andric     if (MO.isFI() || MO.isCPI() || MO.isJTI())
23010b57cec5SDimitry Andric       return nullptr;
23020b57cec5SDimitry Andric     if (!MO.isReg())
23030b57cec5SDimitry Andric       continue;
23040b57cec5SDimitry Andric     // MI can't have any tied operands, that would conflict with predication.
23050b57cec5SDimitry Andric     if (MO.isTied())
23060b57cec5SDimitry Andric       return nullptr;
2307bdd1243dSDimitry Andric     if (MO.getReg().isPhysical())
23080b57cec5SDimitry Andric       return nullptr;
23090b57cec5SDimitry Andric     if (MO.isDef() && !MO.isDead())
23100b57cec5SDimitry Andric       return nullptr;
23110b57cec5SDimitry Andric   }
23120b57cec5SDimitry Andric   bool DontMoveAcrossStores = true;
23130b57cec5SDimitry Andric   if (!MI->isSafeToMove(/* AliasAnalysis = */ nullptr, DontMoveAcrossStores))
23140b57cec5SDimitry Andric     return nullptr;
23150b57cec5SDimitry Andric   return MI;
23160b57cec5SDimitry Andric }
23170b57cec5SDimitry Andric 
23180b57cec5SDimitry Andric bool ARMBaseInstrInfo::analyzeSelect(const MachineInstr &MI,
23190b57cec5SDimitry Andric                                      SmallVectorImpl<MachineOperand> &Cond,
23200b57cec5SDimitry Andric                                      unsigned &TrueOp, unsigned &FalseOp,
23210b57cec5SDimitry Andric                                      bool &Optimizable) const {
23220b57cec5SDimitry Andric   assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
23230b57cec5SDimitry Andric          "Unknown select instruction");
23240b57cec5SDimitry Andric   // MOVCC operands:
23250b57cec5SDimitry Andric   // 0: Def.
23260b57cec5SDimitry Andric   // 1: True use.
23270b57cec5SDimitry Andric   // 2: False use.
23280b57cec5SDimitry Andric   // 3: Condition code.
23290b57cec5SDimitry Andric   // 4: CPSR use.
23300b57cec5SDimitry Andric   TrueOp = 1;
23310b57cec5SDimitry Andric   FalseOp = 2;
23320b57cec5SDimitry Andric   Cond.push_back(MI.getOperand(3));
23330b57cec5SDimitry Andric   Cond.push_back(MI.getOperand(4));
23340b57cec5SDimitry Andric   // We can always fold a def.
23350b57cec5SDimitry Andric   Optimizable = true;
23360b57cec5SDimitry Andric   return false;
23370b57cec5SDimitry Andric }
23380b57cec5SDimitry Andric 
23390b57cec5SDimitry Andric MachineInstr *
23400b57cec5SDimitry Andric ARMBaseInstrInfo::optimizeSelect(MachineInstr &MI,
23410b57cec5SDimitry Andric                                  SmallPtrSetImpl<MachineInstr *> &SeenMIs,
23420b57cec5SDimitry Andric                                  bool PreferFalse) const {
23430b57cec5SDimitry Andric   assert((MI.getOpcode() == ARM::MOVCCr || MI.getOpcode() == ARM::t2MOVCCr) &&
23440b57cec5SDimitry Andric          "Unknown select instruction");
23450b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
23460b57cec5SDimitry Andric   MachineInstr *DefMI = canFoldIntoMOVCC(MI.getOperand(2).getReg(), MRI, this);
23470b57cec5SDimitry Andric   bool Invert = !DefMI;
23480b57cec5SDimitry Andric   if (!DefMI)
23490b57cec5SDimitry Andric     DefMI = canFoldIntoMOVCC(MI.getOperand(1).getReg(), MRI, this);
23500b57cec5SDimitry Andric   if (!DefMI)
23510b57cec5SDimitry Andric     return nullptr;
23520b57cec5SDimitry Andric 
23530b57cec5SDimitry Andric   // Find new register class to use.
23540b57cec5SDimitry Andric   MachineOperand FalseReg = MI.getOperand(Invert ? 2 : 1);
2355349cc55cSDimitry Andric   MachineOperand TrueReg = MI.getOperand(Invert ? 1 : 2);
23568bcb0991SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
2357349cc55cSDimitry Andric   const TargetRegisterClass *FalseClass = MRI.getRegClass(FalseReg.getReg());
2358349cc55cSDimitry Andric   const TargetRegisterClass *TrueClass = MRI.getRegClass(TrueReg.getReg());
2359349cc55cSDimitry Andric   if (!MRI.constrainRegClass(DestReg, FalseClass))
2360349cc55cSDimitry Andric     return nullptr;
2361349cc55cSDimitry Andric   if (!MRI.constrainRegClass(DestReg, TrueClass))
23620b57cec5SDimitry Andric     return nullptr;
23630b57cec5SDimitry Andric 
23640b57cec5SDimitry Andric   // Create a new predicated version of DefMI.
23650b57cec5SDimitry Andric   // Rfalse is the first use.
23660b57cec5SDimitry Andric   MachineInstrBuilder NewMI =
23670b57cec5SDimitry Andric       BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), DefMI->getDesc(), DestReg);
23680b57cec5SDimitry Andric 
23690b57cec5SDimitry Andric   // Copy all the DefMI operands, excluding its (null) predicate.
23700b57cec5SDimitry Andric   const MCInstrDesc &DefDesc = DefMI->getDesc();
23710b57cec5SDimitry Andric   for (unsigned i = 1, e = DefDesc.getNumOperands();
2372bdd1243dSDimitry Andric        i != e && !DefDesc.operands()[i].isPredicate(); ++i)
23730b57cec5SDimitry Andric     NewMI.add(DefMI->getOperand(i));
23740b57cec5SDimitry Andric 
23750b57cec5SDimitry Andric   unsigned CondCode = MI.getOperand(3).getImm();
23760b57cec5SDimitry Andric   if (Invert)
23770b57cec5SDimitry Andric     NewMI.addImm(ARMCC::getOppositeCondition(ARMCC::CondCodes(CondCode)));
23780b57cec5SDimitry Andric   else
23790b57cec5SDimitry Andric     NewMI.addImm(CondCode);
23800b57cec5SDimitry Andric   NewMI.add(MI.getOperand(4));
23810b57cec5SDimitry Andric 
23820b57cec5SDimitry Andric   // DefMI is not the -S version that sets CPSR, so add an optional %noreg.
23830b57cec5SDimitry Andric   if (NewMI->hasOptionalDef())
23840b57cec5SDimitry Andric     NewMI.add(condCodeOp());
23850b57cec5SDimitry Andric 
23860b57cec5SDimitry Andric   // The output register value when the predicate is false is an implicit
23870b57cec5SDimitry Andric   // register operand tied to the first def.
23880b57cec5SDimitry Andric   // The tie makes the register allocator ensure the FalseReg is allocated the
23890b57cec5SDimitry Andric   // same register as operand 0.
23900b57cec5SDimitry Andric   FalseReg.setImplicit();
23910b57cec5SDimitry Andric   NewMI.add(FalseReg);
23920b57cec5SDimitry Andric   NewMI->tieOperands(0, NewMI->getNumOperands() - 1);
23930b57cec5SDimitry Andric 
23940b57cec5SDimitry Andric   // Update SeenMIs set: register newly created MI and erase removed DefMI.
23950b57cec5SDimitry Andric   SeenMIs.insert(NewMI);
23960b57cec5SDimitry Andric   SeenMIs.erase(DefMI);
23970b57cec5SDimitry Andric 
23980b57cec5SDimitry Andric   // If MI is inside a loop, and DefMI is outside the loop, then kill flags on
23990b57cec5SDimitry Andric   // DefMI would be invalid when tranferred inside the loop.  Checking for a
24000b57cec5SDimitry Andric   // loop is expensive, but at least remove kill flags if they are in different
24010b57cec5SDimitry Andric   // BBs.
24020b57cec5SDimitry Andric   if (DefMI->getParent() != MI.getParent())
24030b57cec5SDimitry Andric     NewMI->clearKillInfo();
24040b57cec5SDimitry Andric 
24050b57cec5SDimitry Andric   // The caller will erase MI, but not DefMI.
24060b57cec5SDimitry Andric   DefMI->eraseFromParent();
24070b57cec5SDimitry Andric   return NewMI;
24080b57cec5SDimitry Andric }
24090b57cec5SDimitry Andric 
24100b57cec5SDimitry Andric /// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether the
24110b57cec5SDimitry Andric /// instruction is encoded with an 'S' bit is determined by the optional CPSR
24120b57cec5SDimitry Andric /// def operand.
24130b57cec5SDimitry Andric ///
24140b57cec5SDimitry Andric /// This will go away once we can teach tblgen how to set the optional CPSR def
24150b57cec5SDimitry Andric /// operand itself.
24160b57cec5SDimitry Andric struct AddSubFlagsOpcodePair {
24170b57cec5SDimitry Andric   uint16_t PseudoOpc;
24180b57cec5SDimitry Andric   uint16_t MachineOpc;
24190b57cec5SDimitry Andric };
24200b57cec5SDimitry Andric 
24210b57cec5SDimitry Andric static const AddSubFlagsOpcodePair AddSubFlagsOpcodeMap[] = {
24220b57cec5SDimitry Andric   {ARM::ADDSri, ARM::ADDri},
24230b57cec5SDimitry Andric   {ARM::ADDSrr, ARM::ADDrr},
24240b57cec5SDimitry Andric   {ARM::ADDSrsi, ARM::ADDrsi},
24250b57cec5SDimitry Andric   {ARM::ADDSrsr, ARM::ADDrsr},
24260b57cec5SDimitry Andric 
24270b57cec5SDimitry Andric   {ARM::SUBSri, ARM::SUBri},
24280b57cec5SDimitry Andric   {ARM::SUBSrr, ARM::SUBrr},
24290b57cec5SDimitry Andric   {ARM::SUBSrsi, ARM::SUBrsi},
24300b57cec5SDimitry Andric   {ARM::SUBSrsr, ARM::SUBrsr},
24310b57cec5SDimitry Andric 
24320b57cec5SDimitry Andric   {ARM::RSBSri, ARM::RSBri},
24330b57cec5SDimitry Andric   {ARM::RSBSrsi, ARM::RSBrsi},
24340b57cec5SDimitry Andric   {ARM::RSBSrsr, ARM::RSBrsr},
24350b57cec5SDimitry Andric 
24360b57cec5SDimitry Andric   {ARM::tADDSi3, ARM::tADDi3},
24370b57cec5SDimitry Andric   {ARM::tADDSi8, ARM::tADDi8},
24380b57cec5SDimitry Andric   {ARM::tADDSrr, ARM::tADDrr},
24390b57cec5SDimitry Andric   {ARM::tADCS, ARM::tADC},
24400b57cec5SDimitry Andric 
24410b57cec5SDimitry Andric   {ARM::tSUBSi3, ARM::tSUBi3},
24420b57cec5SDimitry Andric   {ARM::tSUBSi8, ARM::tSUBi8},
24430b57cec5SDimitry Andric   {ARM::tSUBSrr, ARM::tSUBrr},
24440b57cec5SDimitry Andric   {ARM::tSBCS, ARM::tSBC},
24450b57cec5SDimitry Andric   {ARM::tRSBS, ARM::tRSB},
24468bcb0991SDimitry Andric   {ARM::tLSLSri, ARM::tLSLri},
24470b57cec5SDimitry Andric 
24480b57cec5SDimitry Andric   {ARM::t2ADDSri, ARM::t2ADDri},
24490b57cec5SDimitry Andric   {ARM::t2ADDSrr, ARM::t2ADDrr},
24500b57cec5SDimitry Andric   {ARM::t2ADDSrs, ARM::t2ADDrs},
24510b57cec5SDimitry Andric 
24520b57cec5SDimitry Andric   {ARM::t2SUBSri, ARM::t2SUBri},
24530b57cec5SDimitry Andric   {ARM::t2SUBSrr, ARM::t2SUBrr},
24540b57cec5SDimitry Andric   {ARM::t2SUBSrs, ARM::t2SUBrs},
24550b57cec5SDimitry Andric 
24560b57cec5SDimitry Andric   {ARM::t2RSBSri, ARM::t2RSBri},
24570b57cec5SDimitry Andric   {ARM::t2RSBSrs, ARM::t2RSBrs},
24580b57cec5SDimitry Andric };
24590b57cec5SDimitry Andric 
24600b57cec5SDimitry Andric unsigned llvm::convertAddSubFlagsOpcode(unsigned OldOpc) {
2461bdd1243dSDimitry Andric   for (const auto &Entry : AddSubFlagsOpcodeMap)
2462bdd1243dSDimitry Andric     if (OldOpc == Entry.PseudoOpc)
2463bdd1243dSDimitry Andric       return Entry.MachineOpc;
24640b57cec5SDimitry Andric   return 0;
24650b57cec5SDimitry Andric }
24660b57cec5SDimitry Andric 
24670b57cec5SDimitry Andric void llvm::emitARMRegPlusImmediate(MachineBasicBlock &MBB,
24680b57cec5SDimitry Andric                                    MachineBasicBlock::iterator &MBBI,
24695ffd83dbSDimitry Andric                                    const DebugLoc &dl, Register DestReg,
24705ffd83dbSDimitry Andric                                    Register BaseReg, int NumBytes,
24715ffd83dbSDimitry Andric                                    ARMCC::CondCodes Pred, Register PredReg,
24720b57cec5SDimitry Andric                                    const ARMBaseInstrInfo &TII,
24730b57cec5SDimitry Andric                                    unsigned MIFlags) {
24740b57cec5SDimitry Andric   if (NumBytes == 0 && DestReg != BaseReg) {
24750b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), DestReg)
24760b57cec5SDimitry Andric         .addReg(BaseReg, RegState::Kill)
24770b57cec5SDimitry Andric         .add(predOps(Pred, PredReg))
24780b57cec5SDimitry Andric         .add(condCodeOp())
24790b57cec5SDimitry Andric         .setMIFlags(MIFlags);
24800b57cec5SDimitry Andric     return;
24810b57cec5SDimitry Andric   }
24820b57cec5SDimitry Andric 
24830b57cec5SDimitry Andric   bool isSub = NumBytes < 0;
24840b57cec5SDimitry Andric   if (isSub) NumBytes = -NumBytes;
24850b57cec5SDimitry Andric 
24860b57cec5SDimitry Andric   while (NumBytes) {
24870b57cec5SDimitry Andric     unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
248806c3fb27SDimitry Andric     unsigned ThisVal = NumBytes & llvm::rotr<uint32_t>(0xFF, RotAmt);
24890b57cec5SDimitry Andric     assert(ThisVal && "Didn't extract field correctly");
24900b57cec5SDimitry Andric 
24910b57cec5SDimitry Andric     // We will handle these bits from offset, clear them.
24920b57cec5SDimitry Andric     NumBytes &= ~ThisVal;
24930b57cec5SDimitry Andric 
24940b57cec5SDimitry Andric     assert(ARM_AM::getSOImmVal(ThisVal) != -1 && "Bit extraction didn't work?");
24950b57cec5SDimitry Andric 
24960b57cec5SDimitry Andric     // Build the new ADD / SUB.
24970b57cec5SDimitry Andric     unsigned Opc = isSub ? ARM::SUBri : ARM::ADDri;
24980b57cec5SDimitry Andric     BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
24990b57cec5SDimitry Andric         .addReg(BaseReg, RegState::Kill)
25000b57cec5SDimitry Andric         .addImm(ThisVal)
25010b57cec5SDimitry Andric         .add(predOps(Pred, PredReg))
25020b57cec5SDimitry Andric         .add(condCodeOp())
25030b57cec5SDimitry Andric         .setMIFlags(MIFlags);
25040b57cec5SDimitry Andric     BaseReg = DestReg;
25050b57cec5SDimitry Andric   }
25060b57cec5SDimitry Andric }
25070b57cec5SDimitry Andric 
25080b57cec5SDimitry Andric bool llvm::tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
25090b57cec5SDimitry Andric                                       MachineFunction &MF, MachineInstr *MI,
25100b57cec5SDimitry Andric                                       unsigned NumBytes) {
25110b57cec5SDimitry Andric   // This optimisation potentially adds lots of load and store
25120b57cec5SDimitry Andric   // micro-operations, it's only really a great benefit to code-size.
25130b57cec5SDimitry Andric   if (!Subtarget.hasMinSize())
25140b57cec5SDimitry Andric     return false;
25150b57cec5SDimitry Andric 
25160b57cec5SDimitry Andric   // If only one register is pushed/popped, LLVM can use an LDR/STR
25170b57cec5SDimitry Andric   // instead. We can't modify those so make sure we're dealing with an
25180b57cec5SDimitry Andric   // instruction we understand.
25190b57cec5SDimitry Andric   bool IsPop = isPopOpcode(MI->getOpcode());
25200b57cec5SDimitry Andric   bool IsPush = isPushOpcode(MI->getOpcode());
25210b57cec5SDimitry Andric   if (!IsPush && !IsPop)
25220b57cec5SDimitry Andric     return false;
25230b57cec5SDimitry Andric 
25240b57cec5SDimitry Andric   bool IsVFPPushPop = MI->getOpcode() == ARM::VSTMDDB_UPD ||
25250b57cec5SDimitry Andric                       MI->getOpcode() == ARM::VLDMDIA_UPD;
25260b57cec5SDimitry Andric   bool IsT1PushPop = MI->getOpcode() == ARM::tPUSH ||
25270b57cec5SDimitry Andric                      MI->getOpcode() == ARM::tPOP ||
25280b57cec5SDimitry Andric                      MI->getOpcode() == ARM::tPOP_RET;
25290b57cec5SDimitry Andric 
25300b57cec5SDimitry Andric   assert((IsT1PushPop || (MI->getOperand(0).getReg() == ARM::SP &&
25310b57cec5SDimitry Andric                           MI->getOperand(1).getReg() == ARM::SP)) &&
25320b57cec5SDimitry Andric          "trying to fold sp update into non-sp-updating push/pop");
25330b57cec5SDimitry Andric 
25340b57cec5SDimitry Andric   // The VFP push & pop act on D-registers, so we can only fold an adjustment
25350b57cec5SDimitry Andric   // by a multiple of 8 bytes in correctly. Similarly rN is 4-bytes. Don't try
25360b57cec5SDimitry Andric   // if this is violated.
25370b57cec5SDimitry Andric   if (NumBytes % (IsVFPPushPop ? 8 : 4) != 0)
25380b57cec5SDimitry Andric     return false;
25390b57cec5SDimitry Andric 
25400b57cec5SDimitry Andric   // ARM and Thumb2 push/pop insts have explicit "sp, sp" operands (+
25410b57cec5SDimitry Andric   // pred) so the list starts at 4. Thumb1 starts after the predicate.
25420b57cec5SDimitry Andric   int RegListIdx = IsT1PushPop ? 2 : 4;
25430b57cec5SDimitry Andric 
25440b57cec5SDimitry Andric   // Calculate the space we'll need in terms of registers.
25450b57cec5SDimitry Andric   unsigned RegsNeeded;
25460b57cec5SDimitry Andric   const TargetRegisterClass *RegClass;
25470b57cec5SDimitry Andric   if (IsVFPPushPop) {
25480b57cec5SDimitry Andric     RegsNeeded = NumBytes / 8;
25490b57cec5SDimitry Andric     RegClass = &ARM::DPRRegClass;
25500b57cec5SDimitry Andric   } else {
25510b57cec5SDimitry Andric     RegsNeeded = NumBytes / 4;
25520b57cec5SDimitry Andric     RegClass = &ARM::GPRRegClass;
25530b57cec5SDimitry Andric   }
25540b57cec5SDimitry Andric 
25550b57cec5SDimitry Andric   // We're going to have to strip all list operands off before
25560b57cec5SDimitry Andric   // re-adding them since the order matters, so save the existing ones
25570b57cec5SDimitry Andric   // for later.
25580b57cec5SDimitry Andric   SmallVector<MachineOperand, 4> RegList;
25590b57cec5SDimitry Andric 
25600b57cec5SDimitry Andric   // We're also going to need the first register transferred by this
25610b57cec5SDimitry Andric   // instruction, which won't necessarily be the first register in the list.
25620b57cec5SDimitry Andric   unsigned FirstRegEnc = -1;
25630b57cec5SDimitry Andric 
25640b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = MF.getRegInfo().getTargetRegisterInfo();
25650b57cec5SDimitry Andric   for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i) {
25660b57cec5SDimitry Andric     MachineOperand &MO = MI->getOperand(i);
25670b57cec5SDimitry Andric     RegList.push_back(MO);
25680b57cec5SDimitry Andric 
25698bcb0991SDimitry Andric     if (MO.isReg() && !MO.isImplicit() &&
25708bcb0991SDimitry Andric         TRI->getEncodingValue(MO.getReg()) < FirstRegEnc)
25710b57cec5SDimitry Andric       FirstRegEnc = TRI->getEncodingValue(MO.getReg());
25720b57cec5SDimitry Andric   }
25730b57cec5SDimitry Andric 
25740b57cec5SDimitry Andric   const MCPhysReg *CSRegs = TRI->getCalleeSavedRegs(&MF);
25750b57cec5SDimitry Andric 
25760b57cec5SDimitry Andric   // Now try to find enough space in the reglist to allocate NumBytes.
25770b57cec5SDimitry Andric   for (int CurRegEnc = FirstRegEnc - 1; CurRegEnc >= 0 && RegsNeeded;
25780b57cec5SDimitry Andric        --CurRegEnc) {
25790b57cec5SDimitry Andric     unsigned CurReg = RegClass->getRegister(CurRegEnc);
25808bcb0991SDimitry Andric     if (IsT1PushPop && CurRegEnc > TRI->getEncodingValue(ARM::R7))
25810b57cec5SDimitry Andric       continue;
25820b57cec5SDimitry Andric     if (!IsPop) {
25830b57cec5SDimitry Andric       // Pushing any register is completely harmless, mark the register involved
25840b57cec5SDimitry Andric       // as undef since we don't care about its value and must not restore it
25850b57cec5SDimitry Andric       // during stack unwinding.
25860b57cec5SDimitry Andric       RegList.push_back(MachineOperand::CreateReg(CurReg, false, false,
25870b57cec5SDimitry Andric                                                   false, false, true));
25880b57cec5SDimitry Andric       --RegsNeeded;
25890b57cec5SDimitry Andric       continue;
25900b57cec5SDimitry Andric     }
25910b57cec5SDimitry Andric 
25920b57cec5SDimitry Andric     // However, we can only pop an extra register if it's not live. For
25930b57cec5SDimitry Andric     // registers live within the function we might clobber a return value
25940b57cec5SDimitry Andric     // register; the other way a register can be live here is if it's
25950b57cec5SDimitry Andric     // callee-saved.
25960b57cec5SDimitry Andric     if (isCalleeSavedRegister(CurReg, CSRegs) ||
25970b57cec5SDimitry Andric         MI->getParent()->computeRegisterLiveness(TRI, CurReg, MI) !=
25980b57cec5SDimitry Andric         MachineBasicBlock::LQR_Dead) {
25990b57cec5SDimitry Andric       // VFP pops don't allow holes in the register list, so any skip is fatal
26000b57cec5SDimitry Andric       // for our transformation. GPR pops do, so we should just keep looking.
26010b57cec5SDimitry Andric       if (IsVFPPushPop)
26020b57cec5SDimitry Andric         return false;
26030b57cec5SDimitry Andric       else
26040b57cec5SDimitry Andric         continue;
26050b57cec5SDimitry Andric     }
26060b57cec5SDimitry Andric 
26070b57cec5SDimitry Andric     // Mark the unimportant registers as <def,dead> in the POP.
26080b57cec5SDimitry Andric     RegList.push_back(MachineOperand::CreateReg(CurReg, true, false, false,
26090b57cec5SDimitry Andric                                                 true));
26100b57cec5SDimitry Andric     --RegsNeeded;
26110b57cec5SDimitry Andric   }
26120b57cec5SDimitry Andric 
26130b57cec5SDimitry Andric   if (RegsNeeded > 0)
26140b57cec5SDimitry Andric     return false;
26150b57cec5SDimitry Andric 
26160b57cec5SDimitry Andric   // Finally we know we can profitably perform the optimisation so go
26170b57cec5SDimitry Andric   // ahead: strip all existing registers off and add them back again
26180b57cec5SDimitry Andric   // in the right order.
26190b57cec5SDimitry Andric   for (int i = MI->getNumOperands() - 1; i >= RegListIdx; --i)
262081ad6265SDimitry Andric     MI->removeOperand(i);
26210b57cec5SDimitry Andric 
26220b57cec5SDimitry Andric   // Add the complete list back in.
26230b57cec5SDimitry Andric   MachineInstrBuilder MIB(MF, &*MI);
26240eae32dcSDimitry Andric   for (const MachineOperand &MO : llvm::reverse(RegList))
26250eae32dcSDimitry Andric     MIB.add(MO);
26260b57cec5SDimitry Andric 
26270b57cec5SDimitry Andric   return true;
26280b57cec5SDimitry Andric }
26290b57cec5SDimitry Andric 
26300b57cec5SDimitry Andric bool llvm::rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
26315ffd83dbSDimitry Andric                                 Register FrameReg, int &Offset,
26320b57cec5SDimitry Andric                                 const ARMBaseInstrInfo &TII) {
26330b57cec5SDimitry Andric   unsigned Opcode = MI.getOpcode();
26340b57cec5SDimitry Andric   const MCInstrDesc &Desc = MI.getDesc();
26350b57cec5SDimitry Andric   unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
26360b57cec5SDimitry Andric   bool isSub = false;
26370b57cec5SDimitry Andric 
26380b57cec5SDimitry Andric   // Memory operands in inline assembly always use AddrMode2.
26390b57cec5SDimitry Andric   if (Opcode == ARM::INLINEASM || Opcode == ARM::INLINEASM_BR)
26400b57cec5SDimitry Andric     AddrMode = ARMII::AddrMode2;
26410b57cec5SDimitry Andric 
26420b57cec5SDimitry Andric   if (Opcode == ARM::ADDri) {
26430b57cec5SDimitry Andric     Offset += MI.getOperand(FrameRegIdx+1).getImm();
26440b57cec5SDimitry Andric     if (Offset == 0) {
26450b57cec5SDimitry Andric       // Turn it into a move.
26460b57cec5SDimitry Andric       MI.setDesc(TII.get(ARM::MOVr));
26470b57cec5SDimitry Andric       MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
264881ad6265SDimitry Andric       MI.removeOperand(FrameRegIdx+1);
26490b57cec5SDimitry Andric       Offset = 0;
26500b57cec5SDimitry Andric       return true;
26510b57cec5SDimitry Andric     } else if (Offset < 0) {
26520b57cec5SDimitry Andric       Offset = -Offset;
26530b57cec5SDimitry Andric       isSub = true;
26540b57cec5SDimitry Andric       MI.setDesc(TII.get(ARM::SUBri));
26550b57cec5SDimitry Andric     }
26560b57cec5SDimitry Andric 
26570b57cec5SDimitry Andric     // Common case: small offset, fits into instruction.
26580b57cec5SDimitry Andric     if (ARM_AM::getSOImmVal(Offset) != -1) {
26590b57cec5SDimitry Andric       // Replace the FrameIndex with sp / fp
26600b57cec5SDimitry Andric       MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
26610b57cec5SDimitry Andric       MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
26620b57cec5SDimitry Andric       Offset = 0;
26630b57cec5SDimitry Andric       return true;
26640b57cec5SDimitry Andric     }
26650b57cec5SDimitry Andric 
26660b57cec5SDimitry Andric     // Otherwise, pull as much of the immedidate into this ADDri/SUBri
26670b57cec5SDimitry Andric     // as possible.
26680b57cec5SDimitry Andric     unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
266906c3fb27SDimitry Andric     unsigned ThisImmVal = Offset & llvm::rotr<uint32_t>(0xFF, RotAmt);
26700b57cec5SDimitry Andric 
26710b57cec5SDimitry Andric     // We will handle these bits from offset, clear them.
26720b57cec5SDimitry Andric     Offset &= ~ThisImmVal;
26730b57cec5SDimitry Andric 
26740b57cec5SDimitry Andric     // Get the properly encoded SOImmVal field.
26750b57cec5SDimitry Andric     assert(ARM_AM::getSOImmVal(ThisImmVal) != -1 &&
26760b57cec5SDimitry Andric            "Bit extraction didn't work?");
26770b57cec5SDimitry Andric     MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
26780b57cec5SDimitry Andric  } else {
26790b57cec5SDimitry Andric     unsigned ImmIdx = 0;
26800b57cec5SDimitry Andric     int InstrOffs = 0;
26810b57cec5SDimitry Andric     unsigned NumBits = 0;
26820b57cec5SDimitry Andric     unsigned Scale = 1;
26830b57cec5SDimitry Andric     switch (AddrMode) {
26840b57cec5SDimitry Andric     case ARMII::AddrMode_i12:
26850b57cec5SDimitry Andric       ImmIdx = FrameRegIdx + 1;
26860b57cec5SDimitry Andric       InstrOffs = MI.getOperand(ImmIdx).getImm();
26870b57cec5SDimitry Andric       NumBits = 12;
26880b57cec5SDimitry Andric       break;
26890b57cec5SDimitry Andric     case ARMII::AddrMode2:
26900b57cec5SDimitry Andric       ImmIdx = FrameRegIdx+2;
26910b57cec5SDimitry Andric       InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
26920b57cec5SDimitry Andric       if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
26930b57cec5SDimitry Andric         InstrOffs *= -1;
26940b57cec5SDimitry Andric       NumBits = 12;
26950b57cec5SDimitry Andric       break;
26960b57cec5SDimitry Andric     case ARMII::AddrMode3:
26970b57cec5SDimitry Andric       ImmIdx = FrameRegIdx+2;
26980b57cec5SDimitry Andric       InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
26990b57cec5SDimitry Andric       if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
27000b57cec5SDimitry Andric         InstrOffs *= -1;
27010b57cec5SDimitry Andric       NumBits = 8;
27020b57cec5SDimitry Andric       break;
27030b57cec5SDimitry Andric     case ARMII::AddrMode4:
27040b57cec5SDimitry Andric     case ARMII::AddrMode6:
27050b57cec5SDimitry Andric       // Can't fold any offset even if it's zero.
27060b57cec5SDimitry Andric       return false;
27070b57cec5SDimitry Andric     case ARMII::AddrMode5:
27080b57cec5SDimitry Andric       ImmIdx = FrameRegIdx+1;
27090b57cec5SDimitry Andric       InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
27100b57cec5SDimitry Andric       if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
27110b57cec5SDimitry Andric         InstrOffs *= -1;
27120b57cec5SDimitry Andric       NumBits = 8;
27130b57cec5SDimitry Andric       Scale = 4;
27140b57cec5SDimitry Andric       break;
27150b57cec5SDimitry Andric     case ARMII::AddrMode5FP16:
27160b57cec5SDimitry Andric       ImmIdx = FrameRegIdx+1;
27170b57cec5SDimitry Andric       InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
27180b57cec5SDimitry Andric       if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
27190b57cec5SDimitry Andric         InstrOffs *= -1;
27200b57cec5SDimitry Andric       NumBits = 8;
27210b57cec5SDimitry Andric       Scale = 2;
27220b57cec5SDimitry Andric       break;
27230b57cec5SDimitry Andric     case ARMII::AddrModeT2_i7:
27240b57cec5SDimitry Andric     case ARMII::AddrModeT2_i7s2:
27250b57cec5SDimitry Andric     case ARMII::AddrModeT2_i7s4:
27260b57cec5SDimitry Andric       ImmIdx = FrameRegIdx+1;
27270b57cec5SDimitry Andric       InstrOffs = MI.getOperand(ImmIdx).getImm();
27280b57cec5SDimitry Andric       NumBits = 7;
27290b57cec5SDimitry Andric       Scale = (AddrMode == ARMII::AddrModeT2_i7s2 ? 2 :
27300b57cec5SDimitry Andric                AddrMode == ARMII::AddrModeT2_i7s4 ? 4 : 1);
27310b57cec5SDimitry Andric       break;
27320b57cec5SDimitry Andric     default:
27330b57cec5SDimitry Andric       llvm_unreachable("Unsupported addressing mode!");
27340b57cec5SDimitry Andric     }
27350b57cec5SDimitry Andric 
27360b57cec5SDimitry Andric     Offset += InstrOffs * Scale;
27370b57cec5SDimitry Andric     assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
27380b57cec5SDimitry Andric     if (Offset < 0) {
27390b57cec5SDimitry Andric       Offset = -Offset;
27400b57cec5SDimitry Andric       isSub = true;
27410b57cec5SDimitry Andric     }
27420b57cec5SDimitry Andric 
27430b57cec5SDimitry Andric     // Attempt to fold address comp. if opcode has offset bits
27440b57cec5SDimitry Andric     if (NumBits > 0) {
27450b57cec5SDimitry Andric       // Common case: small offset, fits into instruction.
27460b57cec5SDimitry Andric       MachineOperand &ImmOp = MI.getOperand(ImmIdx);
27470b57cec5SDimitry Andric       int ImmedOffset = Offset / Scale;
27480b57cec5SDimitry Andric       unsigned Mask = (1 << NumBits) - 1;
27490b57cec5SDimitry Andric       if ((unsigned)Offset <= Mask * Scale) {
27500b57cec5SDimitry Andric         // Replace the FrameIndex with sp
27510b57cec5SDimitry Andric         MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
27520b57cec5SDimitry Andric         // FIXME: When addrmode2 goes away, this will simplify (like the
27530b57cec5SDimitry Andric         // T2 version), as the LDR.i12 versions don't need the encoding
27540b57cec5SDimitry Andric         // tricks for the offset value.
27550b57cec5SDimitry Andric         if (isSub) {
27560b57cec5SDimitry Andric           if (AddrMode == ARMII::AddrMode_i12)
27570b57cec5SDimitry Andric             ImmedOffset = -ImmedOffset;
27580b57cec5SDimitry Andric           else
27590b57cec5SDimitry Andric             ImmedOffset |= 1 << NumBits;
27600b57cec5SDimitry Andric         }
27610b57cec5SDimitry Andric         ImmOp.ChangeToImmediate(ImmedOffset);
27620b57cec5SDimitry Andric         Offset = 0;
27630b57cec5SDimitry Andric         return true;
27640b57cec5SDimitry Andric       }
27650b57cec5SDimitry Andric 
27660b57cec5SDimitry Andric       // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
27670b57cec5SDimitry Andric       ImmedOffset = ImmedOffset & Mask;
27680b57cec5SDimitry Andric       if (isSub) {
27690b57cec5SDimitry Andric         if (AddrMode == ARMII::AddrMode_i12)
27700b57cec5SDimitry Andric           ImmedOffset = -ImmedOffset;
27710b57cec5SDimitry Andric         else
27720b57cec5SDimitry Andric           ImmedOffset |= 1 << NumBits;
27730b57cec5SDimitry Andric       }
27740b57cec5SDimitry Andric       ImmOp.ChangeToImmediate(ImmedOffset);
27750b57cec5SDimitry Andric       Offset &= ~(Mask*Scale);
27760b57cec5SDimitry Andric     }
27770b57cec5SDimitry Andric   }
27780b57cec5SDimitry Andric 
27790b57cec5SDimitry Andric   Offset = (isSub) ? -Offset : Offset;
27800b57cec5SDimitry Andric   return Offset == 0;
27810b57cec5SDimitry Andric }
27820b57cec5SDimitry Andric 
27830b57cec5SDimitry Andric /// analyzeCompare - For a comparison instruction, return the source registers
27840b57cec5SDimitry Andric /// in SrcReg and SrcReg2 if having two register operands, and the value it
27850b57cec5SDimitry Andric /// compares against in CmpValue. Return true if the comparison instruction
27860b57cec5SDimitry Andric /// can be analyzed.
27875ffd83dbSDimitry Andric bool ARMBaseInstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg,
2788349cc55cSDimitry Andric                                       Register &SrcReg2, int64_t &CmpMask,
2789349cc55cSDimitry Andric                                       int64_t &CmpValue) const {
27900b57cec5SDimitry Andric   switch (MI.getOpcode()) {
27910b57cec5SDimitry Andric   default: break;
27920b57cec5SDimitry Andric   case ARM::CMPri:
27930b57cec5SDimitry Andric   case ARM::t2CMPri:
27940b57cec5SDimitry Andric   case ARM::tCMPi8:
27950b57cec5SDimitry Andric     SrcReg = MI.getOperand(0).getReg();
27960b57cec5SDimitry Andric     SrcReg2 = 0;
27970b57cec5SDimitry Andric     CmpMask = ~0;
27980b57cec5SDimitry Andric     CmpValue = MI.getOperand(1).getImm();
27990b57cec5SDimitry Andric     return true;
28000b57cec5SDimitry Andric   case ARM::CMPrr:
28010b57cec5SDimitry Andric   case ARM::t2CMPrr:
28020b57cec5SDimitry Andric   case ARM::tCMPr:
28030b57cec5SDimitry Andric     SrcReg = MI.getOperand(0).getReg();
28040b57cec5SDimitry Andric     SrcReg2 = MI.getOperand(1).getReg();
28050b57cec5SDimitry Andric     CmpMask = ~0;
28060b57cec5SDimitry Andric     CmpValue = 0;
28070b57cec5SDimitry Andric     return true;
28080b57cec5SDimitry Andric   case ARM::TSTri:
28090b57cec5SDimitry Andric   case ARM::t2TSTri:
28100b57cec5SDimitry Andric     SrcReg = MI.getOperand(0).getReg();
28110b57cec5SDimitry Andric     SrcReg2 = 0;
28120b57cec5SDimitry Andric     CmpMask = MI.getOperand(1).getImm();
28130b57cec5SDimitry Andric     CmpValue = 0;
28140b57cec5SDimitry Andric     return true;
28150b57cec5SDimitry Andric   }
28160b57cec5SDimitry Andric 
28170b57cec5SDimitry Andric   return false;
28180b57cec5SDimitry Andric }
28190b57cec5SDimitry Andric 
28200b57cec5SDimitry Andric /// isSuitableForMask - Identify a suitable 'and' instruction that
28210b57cec5SDimitry Andric /// operates on the given source register and applies the same mask
28220b57cec5SDimitry Andric /// as a 'tst' instruction. Provide a limited look-through for copies.
28230b57cec5SDimitry Andric /// When successful, MI will hold the found instruction.
28245ffd83dbSDimitry Andric static bool isSuitableForMask(MachineInstr *&MI, Register SrcReg,
28250b57cec5SDimitry Andric                               int CmpMask, bool CommonUse) {
28260b57cec5SDimitry Andric   switch (MI->getOpcode()) {
28270b57cec5SDimitry Andric     case ARM::ANDri:
28280b57cec5SDimitry Andric     case ARM::t2ANDri:
28290b57cec5SDimitry Andric       if (CmpMask != MI->getOperand(2).getImm())
28300b57cec5SDimitry Andric         return false;
28310b57cec5SDimitry Andric       if (SrcReg == MI->getOperand(CommonUse ? 1 : 0).getReg())
28320b57cec5SDimitry Andric         return true;
28330b57cec5SDimitry Andric       break;
28340b57cec5SDimitry Andric   }
28350b57cec5SDimitry Andric 
28360b57cec5SDimitry Andric   return false;
28370b57cec5SDimitry Andric }
28380b57cec5SDimitry Andric 
28390b57cec5SDimitry Andric /// getCmpToAddCondition - assume the flags are set by CMP(a,b), return
28400b57cec5SDimitry Andric /// the condition code if we modify the instructions such that flags are
28410b57cec5SDimitry Andric /// set by ADD(a,b,X).
28420b57cec5SDimitry Andric inline static ARMCC::CondCodes getCmpToAddCondition(ARMCC::CondCodes CC) {
28430b57cec5SDimitry Andric   switch (CC) {
28440b57cec5SDimitry Andric   default: return ARMCC::AL;
28450b57cec5SDimitry Andric   case ARMCC::HS: return ARMCC::LO;
28460b57cec5SDimitry Andric   case ARMCC::LO: return ARMCC::HS;
28470b57cec5SDimitry Andric   case ARMCC::VS: return ARMCC::VS;
28480b57cec5SDimitry Andric   case ARMCC::VC: return ARMCC::VC;
28490b57cec5SDimitry Andric   }
28500b57cec5SDimitry Andric }
28510b57cec5SDimitry Andric 
28520b57cec5SDimitry Andric /// isRedundantFlagInstr - check whether the first instruction, whose only
28530b57cec5SDimitry Andric /// purpose is to update flags, can be made redundant.
28540b57cec5SDimitry Andric /// CMPrr can be made redundant by SUBrr if the operands are the same.
28550b57cec5SDimitry Andric /// CMPri can be made redundant by SUBri if the operands are the same.
28560b57cec5SDimitry Andric /// CMPrr(r0, r1) can be made redundant by ADDr[ri](r0, r1, X).
28570b57cec5SDimitry Andric /// This function can be extended later on.
28580b57cec5SDimitry Andric inline static bool isRedundantFlagInstr(const MachineInstr *CmpI,
28595ffd83dbSDimitry Andric                                         Register SrcReg, Register SrcReg2,
2860349cc55cSDimitry Andric                                         int64_t ImmValue,
2861349cc55cSDimitry Andric                                         const MachineInstr *OI,
28620b57cec5SDimitry Andric                                         bool &IsThumb1) {
28630b57cec5SDimitry Andric   if ((CmpI->getOpcode() == ARM::CMPrr || CmpI->getOpcode() == ARM::t2CMPrr) &&
28640b57cec5SDimitry Andric       (OI->getOpcode() == ARM::SUBrr || OI->getOpcode() == ARM::t2SUBrr) &&
28650b57cec5SDimitry Andric       ((OI->getOperand(1).getReg() == SrcReg &&
28660b57cec5SDimitry Andric         OI->getOperand(2).getReg() == SrcReg2) ||
28670b57cec5SDimitry Andric        (OI->getOperand(1).getReg() == SrcReg2 &&
28680b57cec5SDimitry Andric         OI->getOperand(2).getReg() == SrcReg))) {
28690b57cec5SDimitry Andric     IsThumb1 = false;
28700b57cec5SDimitry Andric     return true;
28710b57cec5SDimitry Andric   }
28720b57cec5SDimitry Andric 
28730b57cec5SDimitry Andric   if (CmpI->getOpcode() == ARM::tCMPr && OI->getOpcode() == ARM::tSUBrr &&
28740b57cec5SDimitry Andric       ((OI->getOperand(2).getReg() == SrcReg &&
28750b57cec5SDimitry Andric         OI->getOperand(3).getReg() == SrcReg2) ||
28760b57cec5SDimitry Andric        (OI->getOperand(2).getReg() == SrcReg2 &&
28770b57cec5SDimitry Andric         OI->getOperand(3).getReg() == SrcReg))) {
28780b57cec5SDimitry Andric     IsThumb1 = true;
28790b57cec5SDimitry Andric     return true;
28800b57cec5SDimitry Andric   }
28810b57cec5SDimitry Andric 
28820b57cec5SDimitry Andric   if ((CmpI->getOpcode() == ARM::CMPri || CmpI->getOpcode() == ARM::t2CMPri) &&
28830b57cec5SDimitry Andric       (OI->getOpcode() == ARM::SUBri || OI->getOpcode() == ARM::t2SUBri) &&
28840b57cec5SDimitry Andric       OI->getOperand(1).getReg() == SrcReg &&
28850b57cec5SDimitry Andric       OI->getOperand(2).getImm() == ImmValue) {
28860b57cec5SDimitry Andric     IsThumb1 = false;
28870b57cec5SDimitry Andric     return true;
28880b57cec5SDimitry Andric   }
28890b57cec5SDimitry Andric 
28900b57cec5SDimitry Andric   if (CmpI->getOpcode() == ARM::tCMPi8 &&
28910b57cec5SDimitry Andric       (OI->getOpcode() == ARM::tSUBi8 || OI->getOpcode() == ARM::tSUBi3) &&
28920b57cec5SDimitry Andric       OI->getOperand(2).getReg() == SrcReg &&
28930b57cec5SDimitry Andric       OI->getOperand(3).getImm() == ImmValue) {
28940b57cec5SDimitry Andric     IsThumb1 = true;
28950b57cec5SDimitry Andric     return true;
28960b57cec5SDimitry Andric   }
28970b57cec5SDimitry Andric 
28980b57cec5SDimitry Andric   if ((CmpI->getOpcode() == ARM::CMPrr || CmpI->getOpcode() == ARM::t2CMPrr) &&
28990b57cec5SDimitry Andric       (OI->getOpcode() == ARM::ADDrr || OI->getOpcode() == ARM::t2ADDrr ||
29000b57cec5SDimitry Andric        OI->getOpcode() == ARM::ADDri || OI->getOpcode() == ARM::t2ADDri) &&
29010b57cec5SDimitry Andric       OI->getOperand(0).isReg() && OI->getOperand(1).isReg() &&
29020b57cec5SDimitry Andric       OI->getOperand(0).getReg() == SrcReg &&
29030b57cec5SDimitry Andric       OI->getOperand(1).getReg() == SrcReg2) {
29040b57cec5SDimitry Andric     IsThumb1 = false;
29050b57cec5SDimitry Andric     return true;
29060b57cec5SDimitry Andric   }
29070b57cec5SDimitry Andric 
29080b57cec5SDimitry Andric   if (CmpI->getOpcode() == ARM::tCMPr &&
29090b57cec5SDimitry Andric       (OI->getOpcode() == ARM::tADDi3 || OI->getOpcode() == ARM::tADDi8 ||
29100b57cec5SDimitry Andric        OI->getOpcode() == ARM::tADDrr) &&
29110b57cec5SDimitry Andric       OI->getOperand(0).getReg() == SrcReg &&
29120b57cec5SDimitry Andric       OI->getOperand(2).getReg() == SrcReg2) {
29130b57cec5SDimitry Andric     IsThumb1 = true;
29140b57cec5SDimitry Andric     return true;
29150b57cec5SDimitry Andric   }
29160b57cec5SDimitry Andric 
29170b57cec5SDimitry Andric   return false;
29180b57cec5SDimitry Andric }
29190b57cec5SDimitry Andric 
29200b57cec5SDimitry Andric static bool isOptimizeCompareCandidate(MachineInstr *MI, bool &IsThumb1) {
29210b57cec5SDimitry Andric   switch (MI->getOpcode()) {
29220b57cec5SDimitry Andric   default: return false;
29230b57cec5SDimitry Andric   case ARM::tLSLri:
29240b57cec5SDimitry Andric   case ARM::tLSRri:
29250b57cec5SDimitry Andric   case ARM::tLSLrr:
29260b57cec5SDimitry Andric   case ARM::tLSRrr:
29270b57cec5SDimitry Andric   case ARM::tSUBrr:
29280b57cec5SDimitry Andric   case ARM::tADDrr:
29290b57cec5SDimitry Andric   case ARM::tADDi3:
29300b57cec5SDimitry Andric   case ARM::tADDi8:
29310b57cec5SDimitry Andric   case ARM::tSUBi3:
29320b57cec5SDimitry Andric   case ARM::tSUBi8:
29330b57cec5SDimitry Andric   case ARM::tMUL:
29340b57cec5SDimitry Andric   case ARM::tADC:
29350b57cec5SDimitry Andric   case ARM::tSBC:
29360b57cec5SDimitry Andric   case ARM::tRSB:
29370b57cec5SDimitry Andric   case ARM::tAND:
29380b57cec5SDimitry Andric   case ARM::tORR:
29390b57cec5SDimitry Andric   case ARM::tEOR:
29400b57cec5SDimitry Andric   case ARM::tBIC:
29410b57cec5SDimitry Andric   case ARM::tMVN:
29420b57cec5SDimitry Andric   case ARM::tASRri:
29430b57cec5SDimitry Andric   case ARM::tASRrr:
29440b57cec5SDimitry Andric   case ARM::tROR:
29450b57cec5SDimitry Andric     IsThumb1 = true;
2946bdd1243dSDimitry Andric     [[fallthrough]];
29470b57cec5SDimitry Andric   case ARM::RSBrr:
29480b57cec5SDimitry Andric   case ARM::RSBri:
29490b57cec5SDimitry Andric   case ARM::RSCrr:
29500b57cec5SDimitry Andric   case ARM::RSCri:
29510b57cec5SDimitry Andric   case ARM::ADDrr:
29520b57cec5SDimitry Andric   case ARM::ADDri:
29530b57cec5SDimitry Andric   case ARM::ADCrr:
29540b57cec5SDimitry Andric   case ARM::ADCri:
29550b57cec5SDimitry Andric   case ARM::SUBrr:
29560b57cec5SDimitry Andric   case ARM::SUBri:
29570b57cec5SDimitry Andric   case ARM::SBCrr:
29580b57cec5SDimitry Andric   case ARM::SBCri:
29590b57cec5SDimitry Andric   case ARM::t2RSBri:
29600b57cec5SDimitry Andric   case ARM::t2ADDrr:
29610b57cec5SDimitry Andric   case ARM::t2ADDri:
29620b57cec5SDimitry Andric   case ARM::t2ADCrr:
29630b57cec5SDimitry Andric   case ARM::t2ADCri:
29640b57cec5SDimitry Andric   case ARM::t2SUBrr:
29650b57cec5SDimitry Andric   case ARM::t2SUBri:
29660b57cec5SDimitry Andric   case ARM::t2SBCrr:
29670b57cec5SDimitry Andric   case ARM::t2SBCri:
29680b57cec5SDimitry Andric   case ARM::ANDrr:
29690b57cec5SDimitry Andric   case ARM::ANDri:
2970bdd1243dSDimitry Andric   case ARM::ANDrsr:
2971bdd1243dSDimitry Andric   case ARM::ANDrsi:
29720b57cec5SDimitry Andric   case ARM::t2ANDrr:
29730b57cec5SDimitry Andric   case ARM::t2ANDri:
2974bdd1243dSDimitry Andric   case ARM::t2ANDrs:
29750b57cec5SDimitry Andric   case ARM::ORRrr:
29760b57cec5SDimitry Andric   case ARM::ORRri:
2977bdd1243dSDimitry Andric   case ARM::ORRrsr:
2978bdd1243dSDimitry Andric   case ARM::ORRrsi:
29790b57cec5SDimitry Andric   case ARM::t2ORRrr:
29800b57cec5SDimitry Andric   case ARM::t2ORRri:
2981bdd1243dSDimitry Andric   case ARM::t2ORRrs:
29820b57cec5SDimitry Andric   case ARM::EORrr:
29830b57cec5SDimitry Andric   case ARM::EORri:
2984bdd1243dSDimitry Andric   case ARM::EORrsr:
2985bdd1243dSDimitry Andric   case ARM::EORrsi:
29860b57cec5SDimitry Andric   case ARM::t2EORrr:
29870b57cec5SDimitry Andric   case ARM::t2EORri:
2988bdd1243dSDimitry Andric   case ARM::t2EORrs:
2989bdd1243dSDimitry Andric   case ARM::BICri:
2990bdd1243dSDimitry Andric   case ARM::BICrr:
2991bdd1243dSDimitry Andric   case ARM::BICrsi:
2992bdd1243dSDimitry Andric   case ARM::BICrsr:
2993bdd1243dSDimitry Andric   case ARM::t2BICri:
2994bdd1243dSDimitry Andric   case ARM::t2BICrr:
2995bdd1243dSDimitry Andric   case ARM::t2BICrs:
29960b57cec5SDimitry Andric   case ARM::t2LSRri:
29970b57cec5SDimitry Andric   case ARM::t2LSRrr:
29980b57cec5SDimitry Andric   case ARM::t2LSLri:
29990b57cec5SDimitry Andric   case ARM::t2LSLrr:
3000bdd1243dSDimitry Andric   case ARM::MOVsr:
3001bdd1243dSDimitry Andric   case ARM::MOVsi:
30020b57cec5SDimitry Andric     return true;
30030b57cec5SDimitry Andric   }
30040b57cec5SDimitry Andric }
30050b57cec5SDimitry Andric 
30060b57cec5SDimitry Andric /// optimizeCompareInstr - Convert the instruction supplying the argument to the
30070b57cec5SDimitry Andric /// comparison into one that sets the zero bit in the flags register;
30080b57cec5SDimitry Andric /// Remove a redundant Compare instruction if an earlier instruction can set the
30090b57cec5SDimitry Andric /// flags in the same way as Compare.
30100b57cec5SDimitry Andric /// E.g. SUBrr(r1,r2) and CMPrr(r1,r2). We also handle the case where two
30110b57cec5SDimitry Andric /// operands are swapped: SUBrr(r1,r2) and CMPrr(r2,r1), by updating the
30120b57cec5SDimitry Andric /// condition code of instructions which use the flags.
30130b57cec5SDimitry Andric bool ARMBaseInstrInfo::optimizeCompareInstr(
3014349cc55cSDimitry Andric     MachineInstr &CmpInstr, Register SrcReg, Register SrcReg2, int64_t CmpMask,
3015349cc55cSDimitry Andric     int64_t CmpValue, const MachineRegisterInfo *MRI) const {
30160b57cec5SDimitry Andric   // Get the unique definition of SrcReg.
30170b57cec5SDimitry Andric   MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
30180b57cec5SDimitry Andric   if (!MI) return false;
30190b57cec5SDimitry Andric 
30200b57cec5SDimitry Andric   // Masked compares sometimes use the same register as the corresponding 'and'.
30210b57cec5SDimitry Andric   if (CmpMask != ~0) {
30220b57cec5SDimitry Andric     if (!isSuitableForMask(MI, SrcReg, CmpMask, false) || isPredicated(*MI)) {
30230b57cec5SDimitry Andric       MI = nullptr;
30240b57cec5SDimitry Andric       for (MachineRegisterInfo::use_instr_iterator
30250b57cec5SDimitry Andric            UI = MRI->use_instr_begin(SrcReg), UE = MRI->use_instr_end();
30260b57cec5SDimitry Andric            UI != UE; ++UI) {
30270b57cec5SDimitry Andric         if (UI->getParent() != CmpInstr.getParent())
30280b57cec5SDimitry Andric           continue;
30290b57cec5SDimitry Andric         MachineInstr *PotentialAND = &*UI;
30300b57cec5SDimitry Andric         if (!isSuitableForMask(PotentialAND, SrcReg, CmpMask, true) ||
30310b57cec5SDimitry Andric             isPredicated(*PotentialAND))
30320b57cec5SDimitry Andric           continue;
30330b57cec5SDimitry Andric         MI = PotentialAND;
30340b57cec5SDimitry Andric         break;
30350b57cec5SDimitry Andric       }
30360b57cec5SDimitry Andric       if (!MI) return false;
30370b57cec5SDimitry Andric     }
30380b57cec5SDimitry Andric   }
30390b57cec5SDimitry Andric 
30400b57cec5SDimitry Andric   // Get ready to iterate backward from CmpInstr.
30410b57cec5SDimitry Andric   MachineBasicBlock::iterator I = CmpInstr, E = MI,
30420b57cec5SDimitry Andric                               B = CmpInstr.getParent()->begin();
30430b57cec5SDimitry Andric 
30440b57cec5SDimitry Andric   // Early exit if CmpInstr is at the beginning of the BB.
30450b57cec5SDimitry Andric   if (I == B) return false;
30460b57cec5SDimitry Andric 
30470b57cec5SDimitry Andric   // There are two possible candidates which can be changed to set CPSR:
30480b57cec5SDimitry Andric   // One is MI, the other is a SUB or ADD instruction.
30490b57cec5SDimitry Andric   // For CMPrr(r1,r2), we are looking for SUB(r1,r2), SUB(r2,r1), or
30500b57cec5SDimitry Andric   // ADDr[ri](r1, r2, X).
30510b57cec5SDimitry Andric   // For CMPri(r1, CmpValue), we are looking for SUBri(r1, CmpValue).
30520b57cec5SDimitry Andric   MachineInstr *SubAdd = nullptr;
30530b57cec5SDimitry Andric   if (SrcReg2 != 0)
30540b57cec5SDimitry Andric     // MI is not a candidate for CMPrr.
30550b57cec5SDimitry Andric     MI = nullptr;
30560b57cec5SDimitry Andric   else if (MI->getParent() != CmpInstr.getParent() || CmpValue != 0) {
30570b57cec5SDimitry Andric     // Conservatively refuse to convert an instruction which isn't in the same
30580b57cec5SDimitry Andric     // BB as the comparison.
30590b57cec5SDimitry Andric     // For CMPri w/ CmpValue != 0, a SubAdd may still be a candidate.
30600b57cec5SDimitry Andric     // Thus we cannot return here.
30610b57cec5SDimitry Andric     if (CmpInstr.getOpcode() == ARM::CMPri ||
30620b57cec5SDimitry Andric         CmpInstr.getOpcode() == ARM::t2CMPri ||
30630b57cec5SDimitry Andric         CmpInstr.getOpcode() == ARM::tCMPi8)
30640b57cec5SDimitry Andric       MI = nullptr;
30650b57cec5SDimitry Andric     else
30660b57cec5SDimitry Andric       return false;
30670b57cec5SDimitry Andric   }
30680b57cec5SDimitry Andric 
30690b57cec5SDimitry Andric   bool IsThumb1 = false;
30700b57cec5SDimitry Andric   if (MI && !isOptimizeCompareCandidate(MI, IsThumb1))
30710b57cec5SDimitry Andric     return false;
30720b57cec5SDimitry Andric 
30730b57cec5SDimitry Andric   // We also want to do this peephole for cases like this: if (a*b == 0),
30740b57cec5SDimitry Andric   // and optimise away the CMP instruction from the generated code sequence:
30750b57cec5SDimitry Andric   // MULS, MOVS, MOVS, CMP. Here the MOVS instructions load the boolean values
30760b57cec5SDimitry Andric   // resulting from the select instruction, but these MOVS instructions for
30770b57cec5SDimitry Andric   // Thumb1 (V6M) are flag setting and are thus preventing this optimisation.
30780b57cec5SDimitry Andric   // However, if we only have MOVS instructions in between the CMP and the
30790b57cec5SDimitry Andric   // other instruction (the MULS in this example), then the CPSR is dead so we
30800b57cec5SDimitry Andric   // can safely reorder the sequence into: MOVS, MOVS, MULS, CMP. We do this
30810b57cec5SDimitry Andric   // reordering and then continue the analysis hoping we can eliminate the
30820b57cec5SDimitry Andric   // CMP. This peephole works on the vregs, so is still in SSA form. As a
30830b57cec5SDimitry Andric   // consequence, the movs won't redefine/kill the MUL operands which would
30840b57cec5SDimitry Andric   // make this reordering illegal.
30850b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = &getRegisterInfo();
30860b57cec5SDimitry Andric   if (MI && IsThumb1) {
30870b57cec5SDimitry Andric     --I;
30880b57cec5SDimitry Andric     if (I != E && !MI->readsRegister(ARM::CPSR, TRI)) {
30890b57cec5SDimitry Andric       bool CanReorder = true;
30900b57cec5SDimitry Andric       for (; I != E; --I) {
30910b57cec5SDimitry Andric         if (I->getOpcode() != ARM::tMOVi8) {
30920b57cec5SDimitry Andric           CanReorder = false;
30930b57cec5SDimitry Andric           break;
30940b57cec5SDimitry Andric         }
30950b57cec5SDimitry Andric       }
30960b57cec5SDimitry Andric       if (CanReorder) {
30970b57cec5SDimitry Andric         MI = MI->removeFromParent();
30980b57cec5SDimitry Andric         E = CmpInstr;
30990b57cec5SDimitry Andric         CmpInstr.getParent()->insert(E, MI);
31000b57cec5SDimitry Andric       }
31010b57cec5SDimitry Andric     }
31020b57cec5SDimitry Andric     I = CmpInstr;
31030b57cec5SDimitry Andric     E = MI;
31040b57cec5SDimitry Andric   }
31050b57cec5SDimitry Andric 
31060b57cec5SDimitry Andric   // Check that CPSR isn't set between the comparison instruction and the one we
31070b57cec5SDimitry Andric   // want to change. At the same time, search for SubAdd.
31080b57cec5SDimitry Andric   bool SubAddIsThumb1 = false;
31090b57cec5SDimitry Andric   do {
31100b57cec5SDimitry Andric     const MachineInstr &Instr = *--I;
31110b57cec5SDimitry Andric 
31120b57cec5SDimitry Andric     // Check whether CmpInstr can be made redundant by the current instruction.
31130b57cec5SDimitry Andric     if (isRedundantFlagInstr(&CmpInstr, SrcReg, SrcReg2, CmpValue, &Instr,
31140b57cec5SDimitry Andric                              SubAddIsThumb1)) {
31150b57cec5SDimitry Andric       SubAdd = &*I;
31160b57cec5SDimitry Andric       break;
31170b57cec5SDimitry Andric     }
31180b57cec5SDimitry Andric 
31190b57cec5SDimitry Andric     // Allow E (which was initially MI) to be SubAdd but do not search before E.
31200b57cec5SDimitry Andric     if (I == E)
31210b57cec5SDimitry Andric       break;
31220b57cec5SDimitry Andric 
31230b57cec5SDimitry Andric     if (Instr.modifiesRegister(ARM::CPSR, TRI) ||
31240b57cec5SDimitry Andric         Instr.readsRegister(ARM::CPSR, TRI))
31250b57cec5SDimitry Andric       // This instruction modifies or uses CPSR after the one we want to
31260b57cec5SDimitry Andric       // change. We can't do this transformation.
31270b57cec5SDimitry Andric       return false;
31280b57cec5SDimitry Andric 
31290b57cec5SDimitry Andric     if (I == B) {
31300b57cec5SDimitry Andric       // In some cases, we scan the use-list of an instruction for an AND;
31310b57cec5SDimitry Andric       // that AND is in the same BB, but may not be scheduled before the
31320b57cec5SDimitry Andric       // corresponding TST.  In that case, bail out.
31330b57cec5SDimitry Andric       //
31340b57cec5SDimitry Andric       // FIXME: We could try to reschedule the AND.
31350b57cec5SDimitry Andric       return false;
31360b57cec5SDimitry Andric     }
31370b57cec5SDimitry Andric   } while (true);
31380b57cec5SDimitry Andric 
31390b57cec5SDimitry Andric   // Return false if no candidates exist.
31400b57cec5SDimitry Andric   if (!MI && !SubAdd)
31410b57cec5SDimitry Andric     return false;
31420b57cec5SDimitry Andric 
31430b57cec5SDimitry Andric   // If we found a SubAdd, use it as it will be closer to the CMP
31440b57cec5SDimitry Andric   if (SubAdd) {
31450b57cec5SDimitry Andric     MI = SubAdd;
31460b57cec5SDimitry Andric     IsThumb1 = SubAddIsThumb1;
31470b57cec5SDimitry Andric   }
31480b57cec5SDimitry Andric 
31490b57cec5SDimitry Andric   // We can't use a predicated instruction - it doesn't always write the flags.
31500b57cec5SDimitry Andric   if (isPredicated(*MI))
31510b57cec5SDimitry Andric     return false;
31520b57cec5SDimitry Andric 
31530b57cec5SDimitry Andric   // Scan forward for the use of CPSR
31540b57cec5SDimitry Andric   // When checking against MI: if it's a conditional code that requires
31550b57cec5SDimitry Andric   // checking of the V bit or C bit, then this is not safe to do.
31560b57cec5SDimitry Andric   // It is safe to remove CmpInstr if CPSR is redefined or killed.
31570b57cec5SDimitry Andric   // If we are done with the basic block, we need to check whether CPSR is
31580b57cec5SDimitry Andric   // live-out.
31590b57cec5SDimitry Andric   SmallVector<std::pair<MachineOperand*, ARMCC::CondCodes>, 4>
31600b57cec5SDimitry Andric       OperandsToUpdate;
31610b57cec5SDimitry Andric   bool isSafe = false;
31620b57cec5SDimitry Andric   I = CmpInstr;
31630b57cec5SDimitry Andric   E = CmpInstr.getParent()->end();
31640b57cec5SDimitry Andric   while (!isSafe && ++I != E) {
31650b57cec5SDimitry Andric     const MachineInstr &Instr = *I;
31660b57cec5SDimitry Andric     for (unsigned IO = 0, EO = Instr.getNumOperands();
31670b57cec5SDimitry Andric          !isSafe && IO != EO; ++IO) {
31680b57cec5SDimitry Andric       const MachineOperand &MO = Instr.getOperand(IO);
31690b57cec5SDimitry Andric       if (MO.isRegMask() && MO.clobbersPhysReg(ARM::CPSR)) {
31700b57cec5SDimitry Andric         isSafe = true;
31710b57cec5SDimitry Andric         break;
31720b57cec5SDimitry Andric       }
31730b57cec5SDimitry Andric       if (!MO.isReg() || MO.getReg() != ARM::CPSR)
31740b57cec5SDimitry Andric         continue;
31750b57cec5SDimitry Andric       if (MO.isDef()) {
31760b57cec5SDimitry Andric         isSafe = true;
31770b57cec5SDimitry Andric         break;
31780b57cec5SDimitry Andric       }
31790b57cec5SDimitry Andric       // Condition code is after the operand before CPSR except for VSELs.
31800b57cec5SDimitry Andric       ARMCC::CondCodes CC;
31810b57cec5SDimitry Andric       bool IsInstrVSel = true;
31820b57cec5SDimitry Andric       switch (Instr.getOpcode()) {
31830b57cec5SDimitry Andric       default:
31840b57cec5SDimitry Andric         IsInstrVSel = false;
31850b57cec5SDimitry Andric         CC = (ARMCC::CondCodes)Instr.getOperand(IO - 1).getImm();
31860b57cec5SDimitry Andric         break;
31870b57cec5SDimitry Andric       case ARM::VSELEQD:
31880b57cec5SDimitry Andric       case ARM::VSELEQS:
31898bcb0991SDimitry Andric       case ARM::VSELEQH:
31900b57cec5SDimitry Andric         CC = ARMCC::EQ;
31910b57cec5SDimitry Andric         break;
31920b57cec5SDimitry Andric       case ARM::VSELGTD:
31930b57cec5SDimitry Andric       case ARM::VSELGTS:
31948bcb0991SDimitry Andric       case ARM::VSELGTH:
31950b57cec5SDimitry Andric         CC = ARMCC::GT;
31960b57cec5SDimitry Andric         break;
31970b57cec5SDimitry Andric       case ARM::VSELGED:
31980b57cec5SDimitry Andric       case ARM::VSELGES:
31998bcb0991SDimitry Andric       case ARM::VSELGEH:
32000b57cec5SDimitry Andric         CC = ARMCC::GE;
32010b57cec5SDimitry Andric         break;
32020b57cec5SDimitry Andric       case ARM::VSELVSD:
32038bcb0991SDimitry Andric       case ARM::VSELVSS:
32048bcb0991SDimitry Andric       case ARM::VSELVSH:
32050b57cec5SDimitry Andric         CC = ARMCC::VS;
32060b57cec5SDimitry Andric         break;
32070b57cec5SDimitry Andric       }
32080b57cec5SDimitry Andric 
32090b57cec5SDimitry Andric       if (SubAdd) {
32100b57cec5SDimitry Andric         // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based
32110b57cec5SDimitry Andric         // on CMP needs to be updated to be based on SUB.
32120b57cec5SDimitry Andric         // If we have ADD(r1, r2, X) and CMP(r1, r2), the condition code also
32130b57cec5SDimitry Andric         // needs to be modified.
32140b57cec5SDimitry Andric         // Push the condition code operands to OperandsToUpdate.
32150b57cec5SDimitry Andric         // If it is safe to remove CmpInstr, the condition code of these
32160b57cec5SDimitry Andric         // operands will be modified.
32170b57cec5SDimitry Andric         unsigned Opc = SubAdd->getOpcode();
32180b57cec5SDimitry Andric         bool IsSub = Opc == ARM::SUBrr || Opc == ARM::t2SUBrr ||
32190b57cec5SDimitry Andric                      Opc == ARM::SUBri || Opc == ARM::t2SUBri ||
32200b57cec5SDimitry Andric                      Opc == ARM::tSUBrr || Opc == ARM::tSUBi3 ||
32210b57cec5SDimitry Andric                      Opc == ARM::tSUBi8;
32220b57cec5SDimitry Andric         unsigned OpI = Opc != ARM::tSUBrr ? 1 : 2;
32230b57cec5SDimitry Andric         if (!IsSub ||
32240b57cec5SDimitry Andric             (SrcReg2 != 0 && SubAdd->getOperand(OpI).getReg() == SrcReg2 &&
32250b57cec5SDimitry Andric              SubAdd->getOperand(OpI + 1).getReg() == SrcReg)) {
32260b57cec5SDimitry Andric           // VSel doesn't support condition code update.
32270b57cec5SDimitry Andric           if (IsInstrVSel)
32280b57cec5SDimitry Andric             return false;
32290b57cec5SDimitry Andric           // Ensure we can swap the condition.
32300b57cec5SDimitry Andric           ARMCC::CondCodes NewCC = (IsSub ? getSwappedCondition(CC) : getCmpToAddCondition(CC));
32310b57cec5SDimitry Andric           if (NewCC == ARMCC::AL)
32320b57cec5SDimitry Andric             return false;
32330b57cec5SDimitry Andric           OperandsToUpdate.push_back(
32340b57cec5SDimitry Andric               std::make_pair(&((*I).getOperand(IO - 1)), NewCC));
32350b57cec5SDimitry Andric         }
32360b57cec5SDimitry Andric       } else {
32370b57cec5SDimitry Andric         // No SubAdd, so this is x = <op> y, z; cmp x, 0.
32380b57cec5SDimitry Andric         switch (CC) {
32390b57cec5SDimitry Andric         case ARMCC::EQ: // Z
32400b57cec5SDimitry Andric         case ARMCC::NE: // Z
32410b57cec5SDimitry Andric         case ARMCC::MI: // N
32420b57cec5SDimitry Andric         case ARMCC::PL: // N
32430b57cec5SDimitry Andric         case ARMCC::AL: // none
32440b57cec5SDimitry Andric           // CPSR can be used multiple times, we should continue.
32450b57cec5SDimitry Andric           break;
32460b57cec5SDimitry Andric         case ARMCC::HS: // C
32470b57cec5SDimitry Andric         case ARMCC::LO: // C
32480b57cec5SDimitry Andric         case ARMCC::VS: // V
32490b57cec5SDimitry Andric         case ARMCC::VC: // V
32500b57cec5SDimitry Andric         case ARMCC::HI: // C Z
32510b57cec5SDimitry Andric         case ARMCC::LS: // C Z
32520b57cec5SDimitry Andric         case ARMCC::GE: // N V
32530b57cec5SDimitry Andric         case ARMCC::LT: // N V
32540b57cec5SDimitry Andric         case ARMCC::GT: // Z N V
32550b57cec5SDimitry Andric         case ARMCC::LE: // Z N V
32560b57cec5SDimitry Andric           // The instruction uses the V bit or C bit which is not safe.
32570b57cec5SDimitry Andric           return false;
32580b57cec5SDimitry Andric         }
32590b57cec5SDimitry Andric       }
32600b57cec5SDimitry Andric     }
32610b57cec5SDimitry Andric   }
32620b57cec5SDimitry Andric 
32630b57cec5SDimitry Andric   // If CPSR is not killed nor re-defined, we should check whether it is
32640b57cec5SDimitry Andric   // live-out. If it is live-out, do not optimize.
32650b57cec5SDimitry Andric   if (!isSafe) {
32660b57cec5SDimitry Andric     MachineBasicBlock *MBB = CmpInstr.getParent();
3267349cc55cSDimitry Andric     for (MachineBasicBlock *Succ : MBB->successors())
3268349cc55cSDimitry Andric       if (Succ->isLiveIn(ARM::CPSR))
32690b57cec5SDimitry Andric         return false;
32700b57cec5SDimitry Andric   }
32710b57cec5SDimitry Andric 
32720b57cec5SDimitry Andric   // Toggle the optional operand to CPSR (if it exists - in Thumb1 we always
32730b57cec5SDimitry Andric   // set CPSR so this is represented as an explicit output)
32740b57cec5SDimitry Andric   if (!IsThumb1) {
3275bdd1243dSDimitry Andric     unsigned CPSRRegNum = MI->getNumExplicitOperands() - 1;
3276bdd1243dSDimitry Andric     MI->getOperand(CPSRRegNum).setReg(ARM::CPSR);
3277bdd1243dSDimitry Andric     MI->getOperand(CPSRRegNum).setIsDef(true);
32780b57cec5SDimitry Andric   }
32790b57cec5SDimitry Andric   assert(!isPredicated(*MI) && "Can't use flags from predicated instruction");
32800b57cec5SDimitry Andric   CmpInstr.eraseFromParent();
32810b57cec5SDimitry Andric 
32820b57cec5SDimitry Andric   // Modify the condition code of operands in OperandsToUpdate.
32830b57cec5SDimitry Andric   // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
32840b57cec5SDimitry Andric   // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
32850b57cec5SDimitry Andric   for (unsigned i = 0, e = OperandsToUpdate.size(); i < e; i++)
32860b57cec5SDimitry Andric     OperandsToUpdate[i].first->setImm(OperandsToUpdate[i].second);
32870b57cec5SDimitry Andric 
32880b57cec5SDimitry Andric   MI->clearRegisterDeads(ARM::CPSR);
32890b57cec5SDimitry Andric 
32900b57cec5SDimitry Andric   return true;
32910b57cec5SDimitry Andric }
32920b57cec5SDimitry Andric 
32930b57cec5SDimitry Andric bool ARMBaseInstrInfo::shouldSink(const MachineInstr &MI) const {
32940b57cec5SDimitry Andric   // Do not sink MI if it might be used to optimize a redundant compare.
32950b57cec5SDimitry Andric   // We heuristically only look at the instruction immediately following MI to
32960b57cec5SDimitry Andric   // avoid potentially searching the entire basic block.
32970b57cec5SDimitry Andric   if (isPredicated(MI))
32980b57cec5SDimitry Andric     return true;
32990b57cec5SDimitry Andric   MachineBasicBlock::const_iterator Next = &MI;
33000b57cec5SDimitry Andric   ++Next;
33015ffd83dbSDimitry Andric   Register SrcReg, SrcReg2;
3302349cc55cSDimitry Andric   int64_t CmpMask, CmpValue;
33030b57cec5SDimitry Andric   bool IsThumb1;
33040b57cec5SDimitry Andric   if (Next != MI.getParent()->end() &&
33050b57cec5SDimitry Andric       analyzeCompare(*Next, SrcReg, SrcReg2, CmpMask, CmpValue) &&
33060b57cec5SDimitry Andric       isRedundantFlagInstr(&*Next, SrcReg, SrcReg2, CmpValue, &MI, IsThumb1))
33070b57cec5SDimitry Andric     return false;
33080b57cec5SDimitry Andric   return true;
33090b57cec5SDimitry Andric }
33100b57cec5SDimitry Andric 
33110b57cec5SDimitry Andric bool ARMBaseInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
33125ffd83dbSDimitry Andric                                      Register Reg,
33130b57cec5SDimitry Andric                                      MachineRegisterInfo *MRI) const {
33140b57cec5SDimitry Andric   // Fold large immediates into add, sub, or, xor.
33150b57cec5SDimitry Andric   unsigned DefOpc = DefMI.getOpcode();
331606c3fb27SDimitry Andric   if (DefOpc != ARM::t2MOVi32imm && DefOpc != ARM::MOVi32imm &&
331706c3fb27SDimitry Andric       DefOpc != ARM::tMOVi32imm)
33180b57cec5SDimitry Andric     return false;
33190b57cec5SDimitry Andric   if (!DefMI.getOperand(1).isImm())
33200b57cec5SDimitry Andric     // Could be t2MOVi32imm @xx
33210b57cec5SDimitry Andric     return false;
33220b57cec5SDimitry Andric 
33230b57cec5SDimitry Andric   if (!MRI->hasOneNonDBGUse(Reg))
33240b57cec5SDimitry Andric     return false;
33250b57cec5SDimitry Andric 
33260b57cec5SDimitry Andric   const MCInstrDesc &DefMCID = DefMI.getDesc();
33270b57cec5SDimitry Andric   if (DefMCID.hasOptionalDef()) {
33280b57cec5SDimitry Andric     unsigned NumOps = DefMCID.getNumOperands();
33290b57cec5SDimitry Andric     const MachineOperand &MO = DefMI.getOperand(NumOps - 1);
33300b57cec5SDimitry Andric     if (MO.getReg() == ARM::CPSR && !MO.isDead())
33310b57cec5SDimitry Andric       // If DefMI defines CPSR and it is not dead, it's obviously not safe
33320b57cec5SDimitry Andric       // to delete DefMI.
33330b57cec5SDimitry Andric       return false;
33340b57cec5SDimitry Andric   }
33350b57cec5SDimitry Andric 
33360b57cec5SDimitry Andric   const MCInstrDesc &UseMCID = UseMI.getDesc();
33370b57cec5SDimitry Andric   if (UseMCID.hasOptionalDef()) {
33380b57cec5SDimitry Andric     unsigned NumOps = UseMCID.getNumOperands();
33390b57cec5SDimitry Andric     if (UseMI.getOperand(NumOps - 1).getReg() == ARM::CPSR)
33400b57cec5SDimitry Andric       // If the instruction sets the flag, do not attempt this optimization
33410b57cec5SDimitry Andric       // since it may change the semantics of the code.
33420b57cec5SDimitry Andric       return false;
33430b57cec5SDimitry Andric   }
33440b57cec5SDimitry Andric 
33450b57cec5SDimitry Andric   unsigned UseOpc = UseMI.getOpcode();
33460b57cec5SDimitry Andric   unsigned NewUseOpc = 0;
33470b57cec5SDimitry Andric   uint32_t ImmVal = (uint32_t)DefMI.getOperand(1).getImm();
33480b57cec5SDimitry Andric   uint32_t SOImmValV1 = 0, SOImmValV2 = 0;
33490b57cec5SDimitry Andric   bool Commute = false;
33500b57cec5SDimitry Andric   switch (UseOpc) {
33510b57cec5SDimitry Andric   default: return false;
33520b57cec5SDimitry Andric   case ARM::SUBrr:
33530b57cec5SDimitry Andric   case ARM::ADDrr:
33540b57cec5SDimitry Andric   case ARM::ORRrr:
33550b57cec5SDimitry Andric   case ARM::EORrr:
33560b57cec5SDimitry Andric   case ARM::t2SUBrr:
33570b57cec5SDimitry Andric   case ARM::t2ADDrr:
33580b57cec5SDimitry Andric   case ARM::t2ORRrr:
33590b57cec5SDimitry Andric   case ARM::t2EORrr: {
33600b57cec5SDimitry Andric     Commute = UseMI.getOperand(2).getReg() != Reg;
33610b57cec5SDimitry Andric     switch (UseOpc) {
33620b57cec5SDimitry Andric     default: break;
33630b57cec5SDimitry Andric     case ARM::ADDrr:
33640b57cec5SDimitry Andric     case ARM::SUBrr:
33650b57cec5SDimitry Andric       if (UseOpc == ARM::SUBrr && Commute)
33660b57cec5SDimitry Andric         return false;
33670b57cec5SDimitry Andric 
33680b57cec5SDimitry Andric       // ADD/SUB are special because they're essentially the same operation, so
33690b57cec5SDimitry Andric       // we can handle a larger range of immediates.
33700b57cec5SDimitry Andric       if (ARM_AM::isSOImmTwoPartVal(ImmVal))
33710b57cec5SDimitry Andric         NewUseOpc = UseOpc == ARM::ADDrr ? ARM::ADDri : ARM::SUBri;
33720b57cec5SDimitry Andric       else if (ARM_AM::isSOImmTwoPartVal(-ImmVal)) {
33730b57cec5SDimitry Andric         ImmVal = -ImmVal;
33740b57cec5SDimitry Andric         NewUseOpc = UseOpc == ARM::ADDrr ? ARM::SUBri : ARM::ADDri;
33750b57cec5SDimitry Andric       } else
33760b57cec5SDimitry Andric         return false;
33770b57cec5SDimitry Andric       SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
33780b57cec5SDimitry Andric       SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
33790b57cec5SDimitry Andric       break;
33800b57cec5SDimitry Andric     case ARM::ORRrr:
33810b57cec5SDimitry Andric     case ARM::EORrr:
33820b57cec5SDimitry Andric       if (!ARM_AM::isSOImmTwoPartVal(ImmVal))
33830b57cec5SDimitry Andric         return false;
33840b57cec5SDimitry Andric       SOImmValV1 = (uint32_t)ARM_AM::getSOImmTwoPartFirst(ImmVal);
33850b57cec5SDimitry Andric       SOImmValV2 = (uint32_t)ARM_AM::getSOImmTwoPartSecond(ImmVal);
33860b57cec5SDimitry Andric       switch (UseOpc) {
33870b57cec5SDimitry Andric       default: break;
33880b57cec5SDimitry Andric       case ARM::ORRrr: NewUseOpc = ARM::ORRri; break;
33890b57cec5SDimitry Andric       case ARM::EORrr: NewUseOpc = ARM::EORri; break;
33900b57cec5SDimitry Andric       }
33910b57cec5SDimitry Andric       break;
33920b57cec5SDimitry Andric     case ARM::t2ADDrr:
3393480093f4SDimitry Andric     case ARM::t2SUBrr: {
33940b57cec5SDimitry Andric       if (UseOpc == ARM::t2SUBrr && Commute)
33950b57cec5SDimitry Andric         return false;
33960b57cec5SDimitry Andric 
33970b57cec5SDimitry Andric       // ADD/SUB are special because they're essentially the same operation, so
33980b57cec5SDimitry Andric       // we can handle a larger range of immediates.
3399480093f4SDimitry Andric       const bool ToSP = DefMI.getOperand(0).getReg() == ARM::SP;
3400480093f4SDimitry Andric       const unsigned t2ADD = ToSP ? ARM::t2ADDspImm : ARM::t2ADDri;
3401480093f4SDimitry Andric       const unsigned t2SUB = ToSP ? ARM::t2SUBspImm : ARM::t2SUBri;
34020b57cec5SDimitry Andric       if (ARM_AM::isT2SOImmTwoPartVal(ImmVal))
3403480093f4SDimitry Andric         NewUseOpc = UseOpc == ARM::t2ADDrr ? t2ADD : t2SUB;
34040b57cec5SDimitry Andric       else if (ARM_AM::isT2SOImmTwoPartVal(-ImmVal)) {
34050b57cec5SDimitry Andric         ImmVal = -ImmVal;
3406480093f4SDimitry Andric         NewUseOpc = UseOpc == ARM::t2ADDrr ? t2SUB : t2ADD;
34070b57cec5SDimitry Andric       } else
34080b57cec5SDimitry Andric         return false;
34090b57cec5SDimitry Andric       SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
34100b57cec5SDimitry Andric       SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
34110b57cec5SDimitry Andric       break;
3412480093f4SDimitry Andric     }
34130b57cec5SDimitry Andric     case ARM::t2ORRrr:
34140b57cec5SDimitry Andric     case ARM::t2EORrr:
34150b57cec5SDimitry Andric       if (!ARM_AM::isT2SOImmTwoPartVal(ImmVal))
34160b57cec5SDimitry Andric         return false;
34170b57cec5SDimitry Andric       SOImmValV1 = (uint32_t)ARM_AM::getT2SOImmTwoPartFirst(ImmVal);
34180b57cec5SDimitry Andric       SOImmValV2 = (uint32_t)ARM_AM::getT2SOImmTwoPartSecond(ImmVal);
34190b57cec5SDimitry Andric       switch (UseOpc) {
34200b57cec5SDimitry Andric       default: break;
34210b57cec5SDimitry Andric       case ARM::t2ORRrr: NewUseOpc = ARM::t2ORRri; break;
34220b57cec5SDimitry Andric       case ARM::t2EORrr: NewUseOpc = ARM::t2EORri; break;
34230b57cec5SDimitry Andric       }
34240b57cec5SDimitry Andric       break;
34250b57cec5SDimitry Andric     }
34260b57cec5SDimitry Andric   }
34270b57cec5SDimitry Andric   }
34280b57cec5SDimitry Andric 
34290b57cec5SDimitry Andric   unsigned OpIdx = Commute ? 2 : 1;
34308bcb0991SDimitry Andric   Register Reg1 = UseMI.getOperand(OpIdx).getReg();
34310b57cec5SDimitry Andric   bool isKill = UseMI.getOperand(OpIdx).isKill();
3432480093f4SDimitry Andric   const TargetRegisterClass *TRC = MRI->getRegClass(Reg);
3433480093f4SDimitry Andric   Register NewReg = MRI->createVirtualRegister(TRC);
34340b57cec5SDimitry Andric   BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), get(NewUseOpc),
34350b57cec5SDimitry Andric           NewReg)
34360b57cec5SDimitry Andric       .addReg(Reg1, getKillRegState(isKill))
34370b57cec5SDimitry Andric       .addImm(SOImmValV1)
34380b57cec5SDimitry Andric       .add(predOps(ARMCC::AL))
34390b57cec5SDimitry Andric       .add(condCodeOp());
34400b57cec5SDimitry Andric   UseMI.setDesc(get(NewUseOpc));
34410b57cec5SDimitry Andric   UseMI.getOperand(1).setReg(NewReg);
34420b57cec5SDimitry Andric   UseMI.getOperand(1).setIsKill();
34430b57cec5SDimitry Andric   UseMI.getOperand(2).ChangeToImmediate(SOImmValV2);
34440b57cec5SDimitry Andric   DefMI.eraseFromParent();
3445480093f4SDimitry Andric   // FIXME: t2ADDrr should be split, as different rulles apply when writing to SP.
3446480093f4SDimitry Andric   // Just as t2ADDri, that was split to [t2ADDri, t2ADDspImm].
3447480093f4SDimitry Andric   // Then the below code will not be needed, as the input/output register
3448480093f4SDimitry Andric   // classes will be rgpr or gprSP.
3449480093f4SDimitry Andric   // For now, we fix the UseMI operand explicitly here:
3450480093f4SDimitry Andric   switch(NewUseOpc){
3451480093f4SDimitry Andric     case ARM::t2ADDspImm:
3452480093f4SDimitry Andric     case ARM::t2SUBspImm:
3453480093f4SDimitry Andric     case ARM::t2ADDri:
3454480093f4SDimitry Andric     case ARM::t2SUBri:
3455e8d8bef9SDimitry Andric       MRI->constrainRegClass(UseMI.getOperand(0).getReg(), TRC);
3456480093f4SDimitry Andric   }
34570b57cec5SDimitry Andric   return true;
34580b57cec5SDimitry Andric }
34590b57cec5SDimitry Andric 
34600b57cec5SDimitry Andric static unsigned getNumMicroOpsSwiftLdSt(const InstrItineraryData *ItinData,
34610b57cec5SDimitry Andric                                         const MachineInstr &MI) {
34620b57cec5SDimitry Andric   switch (MI.getOpcode()) {
34630b57cec5SDimitry Andric   default: {
34640b57cec5SDimitry Andric     const MCInstrDesc &Desc = MI.getDesc();
34650b57cec5SDimitry Andric     int UOps = ItinData->getNumMicroOps(Desc.getSchedClass());
34660b57cec5SDimitry Andric     assert(UOps >= 0 && "bad # UOps");
34670b57cec5SDimitry Andric     return UOps;
34680b57cec5SDimitry Andric   }
34690b57cec5SDimitry Andric 
34700b57cec5SDimitry Andric   case ARM::LDRrs:
34710b57cec5SDimitry Andric   case ARM::LDRBrs:
34720b57cec5SDimitry Andric   case ARM::STRrs:
34730b57cec5SDimitry Andric   case ARM::STRBrs: {
34740b57cec5SDimitry Andric     unsigned ShOpVal = MI.getOperand(3).getImm();
34750b57cec5SDimitry Andric     bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
34760b57cec5SDimitry Andric     unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
34770b57cec5SDimitry Andric     if (!isSub &&
34780b57cec5SDimitry Andric         (ShImm == 0 ||
34790b57cec5SDimitry Andric          ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
34800b57cec5SDimitry Andric           ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
34810b57cec5SDimitry Andric       return 1;
34820b57cec5SDimitry Andric     return 2;
34830b57cec5SDimitry Andric   }
34840b57cec5SDimitry Andric 
34850b57cec5SDimitry Andric   case ARM::LDRH:
34860b57cec5SDimitry Andric   case ARM::STRH: {
34870b57cec5SDimitry Andric     if (!MI.getOperand(2).getReg())
34880b57cec5SDimitry Andric       return 1;
34890b57cec5SDimitry Andric 
34900b57cec5SDimitry Andric     unsigned ShOpVal = MI.getOperand(3).getImm();
34910b57cec5SDimitry Andric     bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
34920b57cec5SDimitry Andric     unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
34930b57cec5SDimitry Andric     if (!isSub &&
34940b57cec5SDimitry Andric         (ShImm == 0 ||
34950b57cec5SDimitry Andric          ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
34960b57cec5SDimitry Andric           ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
34970b57cec5SDimitry Andric       return 1;
34980b57cec5SDimitry Andric     return 2;
34990b57cec5SDimitry Andric   }
35000b57cec5SDimitry Andric 
35010b57cec5SDimitry Andric   case ARM::LDRSB:
35020b57cec5SDimitry Andric   case ARM::LDRSH:
35030b57cec5SDimitry Andric     return (ARM_AM::getAM3Op(MI.getOperand(3).getImm()) == ARM_AM::sub) ? 3 : 2;
35040b57cec5SDimitry Andric 
35050b57cec5SDimitry Andric   case ARM::LDRSB_POST:
35060b57cec5SDimitry Andric   case ARM::LDRSH_POST: {
35078bcb0991SDimitry Andric     Register Rt = MI.getOperand(0).getReg();
35088bcb0991SDimitry Andric     Register Rm = MI.getOperand(3).getReg();
35090b57cec5SDimitry Andric     return (Rt == Rm) ? 4 : 3;
35100b57cec5SDimitry Andric   }
35110b57cec5SDimitry Andric 
35120b57cec5SDimitry Andric   case ARM::LDR_PRE_REG:
35130b57cec5SDimitry Andric   case ARM::LDRB_PRE_REG: {
35148bcb0991SDimitry Andric     Register Rt = MI.getOperand(0).getReg();
35158bcb0991SDimitry Andric     Register Rm = MI.getOperand(3).getReg();
35160b57cec5SDimitry Andric     if (Rt == Rm)
35170b57cec5SDimitry Andric       return 3;
35180b57cec5SDimitry Andric     unsigned ShOpVal = MI.getOperand(4).getImm();
35190b57cec5SDimitry Andric     bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
35200b57cec5SDimitry Andric     unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
35210b57cec5SDimitry Andric     if (!isSub &&
35220b57cec5SDimitry Andric         (ShImm == 0 ||
35230b57cec5SDimitry Andric          ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
35240b57cec5SDimitry Andric           ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
35250b57cec5SDimitry Andric       return 2;
35260b57cec5SDimitry Andric     return 3;
35270b57cec5SDimitry Andric   }
35280b57cec5SDimitry Andric 
35290b57cec5SDimitry Andric   case ARM::STR_PRE_REG:
35300b57cec5SDimitry Andric   case ARM::STRB_PRE_REG: {
35310b57cec5SDimitry Andric     unsigned ShOpVal = MI.getOperand(4).getImm();
35320b57cec5SDimitry Andric     bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
35330b57cec5SDimitry Andric     unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
35340b57cec5SDimitry Andric     if (!isSub &&
35350b57cec5SDimitry Andric         (ShImm == 0 ||
35360b57cec5SDimitry Andric          ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
35370b57cec5SDimitry Andric           ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
35380b57cec5SDimitry Andric       return 2;
35390b57cec5SDimitry Andric     return 3;
35400b57cec5SDimitry Andric   }
35410b57cec5SDimitry Andric 
35420b57cec5SDimitry Andric   case ARM::LDRH_PRE:
35430b57cec5SDimitry Andric   case ARM::STRH_PRE: {
35448bcb0991SDimitry Andric     Register Rt = MI.getOperand(0).getReg();
35458bcb0991SDimitry Andric     Register Rm = MI.getOperand(3).getReg();
35460b57cec5SDimitry Andric     if (!Rm)
35470b57cec5SDimitry Andric       return 2;
35480b57cec5SDimitry Andric     if (Rt == Rm)
35490b57cec5SDimitry Andric       return 3;
35500b57cec5SDimitry Andric     return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 3 : 2;
35510b57cec5SDimitry Andric   }
35520b57cec5SDimitry Andric 
35530b57cec5SDimitry Andric   case ARM::LDR_POST_REG:
35540b57cec5SDimitry Andric   case ARM::LDRB_POST_REG:
35550b57cec5SDimitry Andric   case ARM::LDRH_POST: {
35568bcb0991SDimitry Andric     Register Rt = MI.getOperand(0).getReg();
35578bcb0991SDimitry Andric     Register Rm = MI.getOperand(3).getReg();
35580b57cec5SDimitry Andric     return (Rt == Rm) ? 3 : 2;
35590b57cec5SDimitry Andric   }
35600b57cec5SDimitry Andric 
35610b57cec5SDimitry Andric   case ARM::LDR_PRE_IMM:
35620b57cec5SDimitry Andric   case ARM::LDRB_PRE_IMM:
35630b57cec5SDimitry Andric   case ARM::LDR_POST_IMM:
35640b57cec5SDimitry Andric   case ARM::LDRB_POST_IMM:
35650b57cec5SDimitry Andric   case ARM::STRB_POST_IMM:
35660b57cec5SDimitry Andric   case ARM::STRB_POST_REG:
35670b57cec5SDimitry Andric   case ARM::STRB_PRE_IMM:
35680b57cec5SDimitry Andric   case ARM::STRH_POST:
35690b57cec5SDimitry Andric   case ARM::STR_POST_IMM:
35700b57cec5SDimitry Andric   case ARM::STR_POST_REG:
35710b57cec5SDimitry Andric   case ARM::STR_PRE_IMM:
35720b57cec5SDimitry Andric     return 2;
35730b57cec5SDimitry Andric 
35740b57cec5SDimitry Andric   case ARM::LDRSB_PRE:
35750b57cec5SDimitry Andric   case ARM::LDRSH_PRE: {
35768bcb0991SDimitry Andric     Register Rm = MI.getOperand(3).getReg();
35770b57cec5SDimitry Andric     if (Rm == 0)
35780b57cec5SDimitry Andric       return 3;
35798bcb0991SDimitry Andric     Register Rt = MI.getOperand(0).getReg();
35800b57cec5SDimitry Andric     if (Rt == Rm)
35810b57cec5SDimitry Andric       return 4;
35820b57cec5SDimitry Andric     unsigned ShOpVal = MI.getOperand(4).getImm();
35830b57cec5SDimitry Andric     bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
35840b57cec5SDimitry Andric     unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
35850b57cec5SDimitry Andric     if (!isSub &&
35860b57cec5SDimitry Andric         (ShImm == 0 ||
35870b57cec5SDimitry Andric          ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
35880b57cec5SDimitry Andric           ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
35890b57cec5SDimitry Andric       return 3;
35900b57cec5SDimitry Andric     return 4;
35910b57cec5SDimitry Andric   }
35920b57cec5SDimitry Andric 
35930b57cec5SDimitry Andric   case ARM::LDRD: {
35948bcb0991SDimitry Andric     Register Rt = MI.getOperand(0).getReg();
35958bcb0991SDimitry Andric     Register Rn = MI.getOperand(2).getReg();
35968bcb0991SDimitry Andric     Register Rm = MI.getOperand(3).getReg();
35970b57cec5SDimitry Andric     if (Rm)
35980b57cec5SDimitry Andric       return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
35990b57cec5SDimitry Andric                                                                           : 3;
36000b57cec5SDimitry Andric     return (Rt == Rn) ? 3 : 2;
36010b57cec5SDimitry Andric   }
36020b57cec5SDimitry Andric 
36030b57cec5SDimitry Andric   case ARM::STRD: {
36048bcb0991SDimitry Andric     Register Rm = MI.getOperand(3).getReg();
36050b57cec5SDimitry Andric     if (Rm)
36060b57cec5SDimitry Andric       return (ARM_AM::getAM3Op(MI.getOperand(4).getImm()) == ARM_AM::sub) ? 4
36070b57cec5SDimitry Andric                                                                           : 3;
36080b57cec5SDimitry Andric     return 2;
36090b57cec5SDimitry Andric   }
36100b57cec5SDimitry Andric 
36110b57cec5SDimitry Andric   case ARM::LDRD_POST:
36120b57cec5SDimitry Andric   case ARM::t2LDRD_POST:
36130b57cec5SDimitry Andric     return 3;
36140b57cec5SDimitry Andric 
36150b57cec5SDimitry Andric   case ARM::STRD_POST:
36160b57cec5SDimitry Andric   case ARM::t2STRD_POST:
36170b57cec5SDimitry Andric     return 4;
36180b57cec5SDimitry Andric 
36190b57cec5SDimitry Andric   case ARM::LDRD_PRE: {
36208bcb0991SDimitry Andric     Register Rt = MI.getOperand(0).getReg();
36218bcb0991SDimitry Andric     Register Rn = MI.getOperand(3).getReg();
36228bcb0991SDimitry Andric     Register Rm = MI.getOperand(4).getReg();
36230b57cec5SDimitry Andric     if (Rm)
36240b57cec5SDimitry Andric       return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
36250b57cec5SDimitry Andric                                                                           : 4;
36260b57cec5SDimitry Andric     return (Rt == Rn) ? 4 : 3;
36270b57cec5SDimitry Andric   }
36280b57cec5SDimitry Andric 
36290b57cec5SDimitry Andric   case ARM::t2LDRD_PRE: {
36308bcb0991SDimitry Andric     Register Rt = MI.getOperand(0).getReg();
36318bcb0991SDimitry Andric     Register Rn = MI.getOperand(3).getReg();
36320b57cec5SDimitry Andric     return (Rt == Rn) ? 4 : 3;
36330b57cec5SDimitry Andric   }
36340b57cec5SDimitry Andric 
36350b57cec5SDimitry Andric   case ARM::STRD_PRE: {
36368bcb0991SDimitry Andric     Register Rm = MI.getOperand(4).getReg();
36370b57cec5SDimitry Andric     if (Rm)
36380b57cec5SDimitry Andric       return (ARM_AM::getAM3Op(MI.getOperand(5).getImm()) == ARM_AM::sub) ? 5
36390b57cec5SDimitry Andric                                                                           : 4;
36400b57cec5SDimitry Andric     return 3;
36410b57cec5SDimitry Andric   }
36420b57cec5SDimitry Andric 
36430b57cec5SDimitry Andric   case ARM::t2STRD_PRE:
36440b57cec5SDimitry Andric     return 3;
36450b57cec5SDimitry Andric 
36460b57cec5SDimitry Andric   case ARM::t2LDR_POST:
36470b57cec5SDimitry Andric   case ARM::t2LDRB_POST:
36480b57cec5SDimitry Andric   case ARM::t2LDRB_PRE:
36490b57cec5SDimitry Andric   case ARM::t2LDRSBi12:
36500b57cec5SDimitry Andric   case ARM::t2LDRSBi8:
36510b57cec5SDimitry Andric   case ARM::t2LDRSBpci:
36520b57cec5SDimitry Andric   case ARM::t2LDRSBs:
36530b57cec5SDimitry Andric   case ARM::t2LDRH_POST:
36540b57cec5SDimitry Andric   case ARM::t2LDRH_PRE:
36550b57cec5SDimitry Andric   case ARM::t2LDRSBT:
36560b57cec5SDimitry Andric   case ARM::t2LDRSB_POST:
36570b57cec5SDimitry Andric   case ARM::t2LDRSB_PRE:
36580b57cec5SDimitry Andric   case ARM::t2LDRSH_POST:
36590b57cec5SDimitry Andric   case ARM::t2LDRSH_PRE:
36600b57cec5SDimitry Andric   case ARM::t2LDRSHi12:
36610b57cec5SDimitry Andric   case ARM::t2LDRSHi8:
36620b57cec5SDimitry Andric   case ARM::t2LDRSHpci:
36630b57cec5SDimitry Andric   case ARM::t2LDRSHs:
36640b57cec5SDimitry Andric     return 2;
36650b57cec5SDimitry Andric 
36660b57cec5SDimitry Andric   case ARM::t2LDRDi8: {
36678bcb0991SDimitry Andric     Register Rt = MI.getOperand(0).getReg();
36688bcb0991SDimitry Andric     Register Rn = MI.getOperand(2).getReg();
36690b57cec5SDimitry Andric     return (Rt == Rn) ? 3 : 2;
36700b57cec5SDimitry Andric   }
36710b57cec5SDimitry Andric 
36720b57cec5SDimitry Andric   case ARM::t2STRB_POST:
36730b57cec5SDimitry Andric   case ARM::t2STRB_PRE:
36740b57cec5SDimitry Andric   case ARM::t2STRBs:
36750b57cec5SDimitry Andric   case ARM::t2STRDi8:
36760b57cec5SDimitry Andric   case ARM::t2STRH_POST:
36770b57cec5SDimitry Andric   case ARM::t2STRH_PRE:
36780b57cec5SDimitry Andric   case ARM::t2STRHs:
36790b57cec5SDimitry Andric   case ARM::t2STR_POST:
36800b57cec5SDimitry Andric   case ARM::t2STR_PRE:
36810b57cec5SDimitry Andric   case ARM::t2STRs:
36820b57cec5SDimitry Andric     return 2;
36830b57cec5SDimitry Andric   }
36840b57cec5SDimitry Andric }
36850b57cec5SDimitry Andric 
36860b57cec5SDimitry Andric // Return the number of 32-bit words loaded by LDM or stored by STM. If this
36870b57cec5SDimitry Andric // can't be easily determined return 0 (missing MachineMemOperand).
36880b57cec5SDimitry Andric //
36890b57cec5SDimitry Andric // FIXME: The current MachineInstr design does not support relying on machine
36900b57cec5SDimitry Andric // mem operands to determine the width of a memory access. Instead, we expect
36910b57cec5SDimitry Andric // the target to provide this information based on the instruction opcode and
36920b57cec5SDimitry Andric // operands. However, using MachineMemOperand is the best solution now for
36930b57cec5SDimitry Andric // two reasons:
36940b57cec5SDimitry Andric //
36950b57cec5SDimitry Andric // 1) getNumMicroOps tries to infer LDM memory width from the total number of MI
36960b57cec5SDimitry Andric // operands. This is much more dangerous than using the MachineMemOperand
36970b57cec5SDimitry Andric // sizes because CodeGen passes can insert/remove optional machine operands. In
36980b57cec5SDimitry Andric // fact, it's totally incorrect for preRA passes and appears to be wrong for
36990b57cec5SDimitry Andric // postRA passes as well.
37000b57cec5SDimitry Andric //
37010b57cec5SDimitry Andric // 2) getNumLDMAddresses is only used by the scheduling machine model and any
37020b57cec5SDimitry Andric // machine model that calls this should handle the unknown (zero size) case.
37030b57cec5SDimitry Andric //
37040b57cec5SDimitry Andric // Long term, we should require a target hook that verifies MachineMemOperand
37050b57cec5SDimitry Andric // sizes during MC lowering. That target hook should be local to MC lowering
37060b57cec5SDimitry Andric // because we can't ensure that it is aware of other MI forms. Doing this will
37070b57cec5SDimitry Andric // ensure that MachineMemOperands are correctly propagated through all passes.
37080b57cec5SDimitry Andric unsigned ARMBaseInstrInfo::getNumLDMAddresses(const MachineInstr &MI) const {
37090b57cec5SDimitry Andric   unsigned Size = 0;
37100b57cec5SDimitry Andric   for (MachineInstr::mmo_iterator I = MI.memoperands_begin(),
37110b57cec5SDimitry Andric                                   E = MI.memoperands_end();
37120b57cec5SDimitry Andric        I != E; ++I) {
37130b57cec5SDimitry Andric     Size += (*I)->getSize();
37140b57cec5SDimitry Andric   }
37150b57cec5SDimitry Andric   // FIXME: The scheduler currently can't handle values larger than 16. But
37160b57cec5SDimitry Andric   // the values can actually go up to 32 for floating-point load/store
37170b57cec5SDimitry Andric   // multiple (VLDMIA etc.). Also, the way this code is reasoning about memory
37180b57cec5SDimitry Andric   // operations isn't right; we could end up with "extra" memory operands for
37190b57cec5SDimitry Andric   // various reasons, like tail merge merging two memory operations.
37200b57cec5SDimitry Andric   return std::min(Size / 4, 16U);
37210b57cec5SDimitry Andric }
37220b57cec5SDimitry Andric 
37230b57cec5SDimitry Andric static unsigned getNumMicroOpsSingleIssuePlusExtras(unsigned Opc,
37240b57cec5SDimitry Andric                                                     unsigned NumRegs) {
37250b57cec5SDimitry Andric   unsigned UOps = 1 + NumRegs; // 1 for address computation.
37260b57cec5SDimitry Andric   switch (Opc) {
37270b57cec5SDimitry Andric   default:
37280b57cec5SDimitry Andric     break;
37290b57cec5SDimitry Andric   case ARM::VLDMDIA_UPD:
37300b57cec5SDimitry Andric   case ARM::VLDMDDB_UPD:
37310b57cec5SDimitry Andric   case ARM::VLDMSIA_UPD:
37320b57cec5SDimitry Andric   case ARM::VLDMSDB_UPD:
37330b57cec5SDimitry Andric   case ARM::VSTMDIA_UPD:
37340b57cec5SDimitry Andric   case ARM::VSTMDDB_UPD:
37350b57cec5SDimitry Andric   case ARM::VSTMSIA_UPD:
37360b57cec5SDimitry Andric   case ARM::VSTMSDB_UPD:
37370b57cec5SDimitry Andric   case ARM::LDMIA_UPD:
37380b57cec5SDimitry Andric   case ARM::LDMDA_UPD:
37390b57cec5SDimitry Andric   case ARM::LDMDB_UPD:
37400b57cec5SDimitry Andric   case ARM::LDMIB_UPD:
37410b57cec5SDimitry Andric   case ARM::STMIA_UPD:
37420b57cec5SDimitry Andric   case ARM::STMDA_UPD:
37430b57cec5SDimitry Andric   case ARM::STMDB_UPD:
37440b57cec5SDimitry Andric   case ARM::STMIB_UPD:
37450b57cec5SDimitry Andric   case ARM::tLDMIA_UPD:
37460b57cec5SDimitry Andric   case ARM::tSTMIA_UPD:
37470b57cec5SDimitry Andric   case ARM::t2LDMIA_UPD:
37480b57cec5SDimitry Andric   case ARM::t2LDMDB_UPD:
37490b57cec5SDimitry Andric   case ARM::t2STMIA_UPD:
37500b57cec5SDimitry Andric   case ARM::t2STMDB_UPD:
37510b57cec5SDimitry Andric     ++UOps; // One for base register writeback.
37520b57cec5SDimitry Andric     break;
37530b57cec5SDimitry Andric   case ARM::LDMIA_RET:
37540b57cec5SDimitry Andric   case ARM::tPOP_RET:
37550b57cec5SDimitry Andric   case ARM::t2LDMIA_RET:
37560b57cec5SDimitry Andric     UOps += 2; // One for base reg wb, one for write to pc.
37570b57cec5SDimitry Andric     break;
37580b57cec5SDimitry Andric   }
37590b57cec5SDimitry Andric   return UOps;
37600b57cec5SDimitry Andric }
37610b57cec5SDimitry Andric 
37620b57cec5SDimitry Andric unsigned ARMBaseInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
37630b57cec5SDimitry Andric                                           const MachineInstr &MI) const {
37640b57cec5SDimitry Andric   if (!ItinData || ItinData->isEmpty())
37650b57cec5SDimitry Andric     return 1;
37660b57cec5SDimitry Andric 
37670b57cec5SDimitry Andric   const MCInstrDesc &Desc = MI.getDesc();
37680b57cec5SDimitry Andric   unsigned Class = Desc.getSchedClass();
37690b57cec5SDimitry Andric   int ItinUOps = ItinData->getNumMicroOps(Class);
37700b57cec5SDimitry Andric   if (ItinUOps >= 0) {
37710b57cec5SDimitry Andric     if (Subtarget.isSwift() && (Desc.mayLoad() || Desc.mayStore()))
37720b57cec5SDimitry Andric       return getNumMicroOpsSwiftLdSt(ItinData, MI);
37730b57cec5SDimitry Andric 
37740b57cec5SDimitry Andric     return ItinUOps;
37750b57cec5SDimitry Andric   }
37760b57cec5SDimitry Andric 
37770b57cec5SDimitry Andric   unsigned Opc = MI.getOpcode();
37780b57cec5SDimitry Andric   switch (Opc) {
37790b57cec5SDimitry Andric   default:
37800b57cec5SDimitry Andric     llvm_unreachable("Unexpected multi-uops instruction!");
37810b57cec5SDimitry Andric   case ARM::VLDMQIA:
37820b57cec5SDimitry Andric   case ARM::VSTMQIA:
37830b57cec5SDimitry Andric     return 2;
37840b57cec5SDimitry Andric 
37850b57cec5SDimitry Andric   // The number of uOps for load / store multiple are determined by the number
37860b57cec5SDimitry Andric   // registers.
37870b57cec5SDimitry Andric   //
37880b57cec5SDimitry Andric   // On Cortex-A8, each pair of register loads / stores can be scheduled on the
37890b57cec5SDimitry Andric   // same cycle. The scheduling for the first load / store must be done
37900b57cec5SDimitry Andric   // separately by assuming the address is not 64-bit aligned.
37910b57cec5SDimitry Andric   //
37920b57cec5SDimitry Andric   // On Cortex-A9, the formula is simply (#reg / 2) + (#reg % 2). If the address
37930b57cec5SDimitry Andric   // is not 64-bit aligned, then AGU would take an extra cycle.  For VFP / NEON
37940b57cec5SDimitry Andric   // load / store multiple, the formula is (#reg / 2) + (#reg % 2) + 1.
37950b57cec5SDimitry Andric   case ARM::VLDMDIA:
37960b57cec5SDimitry Andric   case ARM::VLDMDIA_UPD:
37970b57cec5SDimitry Andric   case ARM::VLDMDDB_UPD:
37980b57cec5SDimitry Andric   case ARM::VLDMSIA:
37990b57cec5SDimitry Andric   case ARM::VLDMSIA_UPD:
38000b57cec5SDimitry Andric   case ARM::VLDMSDB_UPD:
38010b57cec5SDimitry Andric   case ARM::VSTMDIA:
38020b57cec5SDimitry Andric   case ARM::VSTMDIA_UPD:
38030b57cec5SDimitry Andric   case ARM::VSTMDDB_UPD:
38040b57cec5SDimitry Andric   case ARM::VSTMSIA:
38050b57cec5SDimitry Andric   case ARM::VSTMSIA_UPD:
38060b57cec5SDimitry Andric   case ARM::VSTMSDB_UPD: {
38070b57cec5SDimitry Andric     unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands();
38080b57cec5SDimitry Andric     return (NumRegs / 2) + (NumRegs % 2) + 1;
38090b57cec5SDimitry Andric   }
38100b57cec5SDimitry Andric 
38110b57cec5SDimitry Andric   case ARM::LDMIA_RET:
38120b57cec5SDimitry Andric   case ARM::LDMIA:
38130b57cec5SDimitry Andric   case ARM::LDMDA:
38140b57cec5SDimitry Andric   case ARM::LDMDB:
38150b57cec5SDimitry Andric   case ARM::LDMIB:
38160b57cec5SDimitry Andric   case ARM::LDMIA_UPD:
38170b57cec5SDimitry Andric   case ARM::LDMDA_UPD:
38180b57cec5SDimitry Andric   case ARM::LDMDB_UPD:
38190b57cec5SDimitry Andric   case ARM::LDMIB_UPD:
38200b57cec5SDimitry Andric   case ARM::STMIA:
38210b57cec5SDimitry Andric   case ARM::STMDA:
38220b57cec5SDimitry Andric   case ARM::STMDB:
38230b57cec5SDimitry Andric   case ARM::STMIB:
38240b57cec5SDimitry Andric   case ARM::STMIA_UPD:
38250b57cec5SDimitry Andric   case ARM::STMDA_UPD:
38260b57cec5SDimitry Andric   case ARM::STMDB_UPD:
38270b57cec5SDimitry Andric   case ARM::STMIB_UPD:
38280b57cec5SDimitry Andric   case ARM::tLDMIA:
38290b57cec5SDimitry Andric   case ARM::tLDMIA_UPD:
38300b57cec5SDimitry Andric   case ARM::tSTMIA_UPD:
38310b57cec5SDimitry Andric   case ARM::tPOP_RET:
38320b57cec5SDimitry Andric   case ARM::tPOP:
38330b57cec5SDimitry Andric   case ARM::tPUSH:
38340b57cec5SDimitry Andric   case ARM::t2LDMIA_RET:
38350b57cec5SDimitry Andric   case ARM::t2LDMIA:
38360b57cec5SDimitry Andric   case ARM::t2LDMDB:
38370b57cec5SDimitry Andric   case ARM::t2LDMIA_UPD:
38380b57cec5SDimitry Andric   case ARM::t2LDMDB_UPD:
38390b57cec5SDimitry Andric   case ARM::t2STMIA:
38400b57cec5SDimitry Andric   case ARM::t2STMDB:
38410b57cec5SDimitry Andric   case ARM::t2STMIA_UPD:
38420b57cec5SDimitry Andric   case ARM::t2STMDB_UPD: {
38430b57cec5SDimitry Andric     unsigned NumRegs = MI.getNumOperands() - Desc.getNumOperands() + 1;
38440b57cec5SDimitry Andric     switch (Subtarget.getLdStMultipleTiming()) {
38450b57cec5SDimitry Andric     case ARMSubtarget::SingleIssuePlusExtras:
38460b57cec5SDimitry Andric       return getNumMicroOpsSingleIssuePlusExtras(Opc, NumRegs);
38470b57cec5SDimitry Andric     case ARMSubtarget::SingleIssue:
38480b57cec5SDimitry Andric       // Assume the worst.
38490b57cec5SDimitry Andric       return NumRegs;
38500b57cec5SDimitry Andric     case ARMSubtarget::DoubleIssue: {
38510b57cec5SDimitry Andric       if (NumRegs < 4)
38520b57cec5SDimitry Andric         return 2;
38530b57cec5SDimitry Andric       // 4 registers would be issued: 2, 2.
38540b57cec5SDimitry Andric       // 5 registers would be issued: 2, 2, 1.
38550b57cec5SDimitry Andric       unsigned UOps = (NumRegs / 2);
38560b57cec5SDimitry Andric       if (NumRegs % 2)
38570b57cec5SDimitry Andric         ++UOps;
38580b57cec5SDimitry Andric       return UOps;
38590b57cec5SDimitry Andric     }
38600b57cec5SDimitry Andric     case ARMSubtarget::DoubleIssueCheckUnalignedAccess: {
38610b57cec5SDimitry Andric       unsigned UOps = (NumRegs / 2);
38620b57cec5SDimitry Andric       // If there are odd number of registers or if it's not 64-bit aligned,
38630b57cec5SDimitry Andric       // then it takes an extra AGU (Address Generation Unit) cycle.
38640b57cec5SDimitry Andric       if ((NumRegs % 2) || !MI.hasOneMemOperand() ||
38655ffd83dbSDimitry Andric           (*MI.memoperands_begin())->getAlign() < Align(8))
38660b57cec5SDimitry Andric         ++UOps;
38670b57cec5SDimitry Andric       return UOps;
38680b57cec5SDimitry Andric       }
38690b57cec5SDimitry Andric     }
38700b57cec5SDimitry Andric   }
38710b57cec5SDimitry Andric   }
38720b57cec5SDimitry Andric   llvm_unreachable("Didn't find the number of microops");
38730b57cec5SDimitry Andric }
38740b57cec5SDimitry Andric 
3875*5f757f3fSDimitry Andric std::optional<unsigned>
38760b57cec5SDimitry Andric ARMBaseInstrInfo::getVLDMDefCycle(const InstrItineraryData *ItinData,
3877*5f757f3fSDimitry Andric                                   const MCInstrDesc &DefMCID, unsigned DefClass,
38780b57cec5SDimitry Andric                                   unsigned DefIdx, unsigned DefAlign) const {
38790b57cec5SDimitry Andric   int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
38800b57cec5SDimitry Andric   if (RegNo <= 0)
38810b57cec5SDimitry Andric     // Def is the address writeback.
38820b57cec5SDimitry Andric     return ItinData->getOperandCycle(DefClass, DefIdx);
38830b57cec5SDimitry Andric 
3884*5f757f3fSDimitry Andric   unsigned DefCycle;
38850b57cec5SDimitry Andric   if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
38860b57cec5SDimitry Andric     // (regno / 2) + (regno % 2) + 1
38870b57cec5SDimitry Andric     DefCycle = RegNo / 2 + 1;
38880b57cec5SDimitry Andric     if (RegNo % 2)
38890b57cec5SDimitry Andric       ++DefCycle;
38900b57cec5SDimitry Andric   } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
38910b57cec5SDimitry Andric     DefCycle = RegNo;
38920b57cec5SDimitry Andric     bool isSLoad = false;
38930b57cec5SDimitry Andric 
38940b57cec5SDimitry Andric     switch (DefMCID.getOpcode()) {
38950b57cec5SDimitry Andric     default: break;
38960b57cec5SDimitry Andric     case ARM::VLDMSIA:
38970b57cec5SDimitry Andric     case ARM::VLDMSIA_UPD:
38980b57cec5SDimitry Andric     case ARM::VLDMSDB_UPD:
38990b57cec5SDimitry Andric       isSLoad = true;
39000b57cec5SDimitry Andric       break;
39010b57cec5SDimitry Andric     }
39020b57cec5SDimitry Andric 
39030b57cec5SDimitry Andric     // If there are odd number of 'S' registers or if it's not 64-bit aligned,
39040b57cec5SDimitry Andric     // then it takes an extra cycle.
39050b57cec5SDimitry Andric     if ((isSLoad && (RegNo % 2)) || DefAlign < 8)
39060b57cec5SDimitry Andric       ++DefCycle;
39070b57cec5SDimitry Andric   } else {
39080b57cec5SDimitry Andric     // Assume the worst.
39090b57cec5SDimitry Andric     DefCycle = RegNo + 2;
39100b57cec5SDimitry Andric   }
39110b57cec5SDimitry Andric 
39120b57cec5SDimitry Andric   return DefCycle;
39130b57cec5SDimitry Andric }
39140b57cec5SDimitry Andric 
3915*5f757f3fSDimitry Andric std::optional<unsigned>
39160b57cec5SDimitry Andric ARMBaseInstrInfo::getLDMDefCycle(const InstrItineraryData *ItinData,
3917*5f757f3fSDimitry Andric                                  const MCInstrDesc &DefMCID, unsigned DefClass,
39180b57cec5SDimitry Andric                                  unsigned DefIdx, unsigned DefAlign) const {
39190b57cec5SDimitry Andric   int RegNo = (int)(DefIdx+1) - DefMCID.getNumOperands() + 1;
39200b57cec5SDimitry Andric   if (RegNo <= 0)
39210b57cec5SDimitry Andric     // Def is the address writeback.
39220b57cec5SDimitry Andric     return ItinData->getOperandCycle(DefClass, DefIdx);
39230b57cec5SDimitry Andric 
3924*5f757f3fSDimitry Andric   unsigned DefCycle;
39250b57cec5SDimitry Andric   if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
39260b57cec5SDimitry Andric     // 4 registers would be issued: 1, 2, 1.
39270b57cec5SDimitry Andric     // 5 registers would be issued: 1, 2, 2.
39280b57cec5SDimitry Andric     DefCycle = RegNo / 2;
39290b57cec5SDimitry Andric     if (DefCycle < 1)
39300b57cec5SDimitry Andric       DefCycle = 1;
39310b57cec5SDimitry Andric     // Result latency is issue cycle + 2: E2.
39320b57cec5SDimitry Andric     DefCycle += 2;
39330b57cec5SDimitry Andric   } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
39340b57cec5SDimitry Andric     DefCycle = (RegNo / 2);
39350b57cec5SDimitry Andric     // If there are odd number of registers or if it's not 64-bit aligned,
39360b57cec5SDimitry Andric     // then it takes an extra AGU (Address Generation Unit) cycle.
39370b57cec5SDimitry Andric     if ((RegNo % 2) || DefAlign < 8)
39380b57cec5SDimitry Andric       ++DefCycle;
39390b57cec5SDimitry Andric     // Result latency is AGU cycles + 2.
39400b57cec5SDimitry Andric     DefCycle += 2;
39410b57cec5SDimitry Andric   } else {
39420b57cec5SDimitry Andric     // Assume the worst.
39430b57cec5SDimitry Andric     DefCycle = RegNo + 2;
39440b57cec5SDimitry Andric   }
39450b57cec5SDimitry Andric 
39460b57cec5SDimitry Andric   return DefCycle;
39470b57cec5SDimitry Andric }
39480b57cec5SDimitry Andric 
3949*5f757f3fSDimitry Andric std::optional<unsigned>
39500b57cec5SDimitry Andric ARMBaseInstrInfo::getVSTMUseCycle(const InstrItineraryData *ItinData,
3951*5f757f3fSDimitry Andric                                   const MCInstrDesc &UseMCID, unsigned UseClass,
39520b57cec5SDimitry Andric                                   unsigned UseIdx, unsigned UseAlign) const {
39530b57cec5SDimitry Andric   int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
39540b57cec5SDimitry Andric   if (RegNo <= 0)
39550b57cec5SDimitry Andric     return ItinData->getOperandCycle(UseClass, UseIdx);
39560b57cec5SDimitry Andric 
3957*5f757f3fSDimitry Andric   unsigned UseCycle;
39580b57cec5SDimitry Andric   if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
39590b57cec5SDimitry Andric     // (regno / 2) + (regno % 2) + 1
39600b57cec5SDimitry Andric     UseCycle = RegNo / 2 + 1;
39610b57cec5SDimitry Andric     if (RegNo % 2)
39620b57cec5SDimitry Andric       ++UseCycle;
39630b57cec5SDimitry Andric   } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
39640b57cec5SDimitry Andric     UseCycle = RegNo;
39650b57cec5SDimitry Andric     bool isSStore = false;
39660b57cec5SDimitry Andric 
39670b57cec5SDimitry Andric     switch (UseMCID.getOpcode()) {
39680b57cec5SDimitry Andric     default: break;
39690b57cec5SDimitry Andric     case ARM::VSTMSIA:
39700b57cec5SDimitry Andric     case ARM::VSTMSIA_UPD:
39710b57cec5SDimitry Andric     case ARM::VSTMSDB_UPD:
39720b57cec5SDimitry Andric       isSStore = true;
39730b57cec5SDimitry Andric       break;
39740b57cec5SDimitry Andric     }
39750b57cec5SDimitry Andric 
39760b57cec5SDimitry Andric     // If there are odd number of 'S' registers or if it's not 64-bit aligned,
39770b57cec5SDimitry Andric     // then it takes an extra cycle.
39780b57cec5SDimitry Andric     if ((isSStore && (RegNo % 2)) || UseAlign < 8)
39790b57cec5SDimitry Andric       ++UseCycle;
39800b57cec5SDimitry Andric   } else {
39810b57cec5SDimitry Andric     // Assume the worst.
39820b57cec5SDimitry Andric     UseCycle = RegNo + 2;
39830b57cec5SDimitry Andric   }
39840b57cec5SDimitry Andric 
39850b57cec5SDimitry Andric   return UseCycle;
39860b57cec5SDimitry Andric }
39870b57cec5SDimitry Andric 
3988*5f757f3fSDimitry Andric std::optional<unsigned>
39890b57cec5SDimitry Andric ARMBaseInstrInfo::getSTMUseCycle(const InstrItineraryData *ItinData,
3990*5f757f3fSDimitry Andric                                  const MCInstrDesc &UseMCID, unsigned UseClass,
39910b57cec5SDimitry Andric                                  unsigned UseIdx, unsigned UseAlign) const {
39920b57cec5SDimitry Andric   int RegNo = (int)(UseIdx+1) - UseMCID.getNumOperands() + 1;
39930b57cec5SDimitry Andric   if (RegNo <= 0)
39940b57cec5SDimitry Andric     return ItinData->getOperandCycle(UseClass, UseIdx);
39950b57cec5SDimitry Andric 
3996*5f757f3fSDimitry Andric   unsigned UseCycle;
39970b57cec5SDimitry Andric   if (Subtarget.isCortexA8() || Subtarget.isCortexA7()) {
39980b57cec5SDimitry Andric     UseCycle = RegNo / 2;
39990b57cec5SDimitry Andric     if (UseCycle < 2)
40000b57cec5SDimitry Andric       UseCycle = 2;
40010b57cec5SDimitry Andric     // Read in E3.
40020b57cec5SDimitry Andric     UseCycle += 2;
40030b57cec5SDimitry Andric   } else if (Subtarget.isLikeA9() || Subtarget.isSwift()) {
40040b57cec5SDimitry Andric     UseCycle = (RegNo / 2);
40050b57cec5SDimitry Andric     // If there are odd number of registers or if it's not 64-bit aligned,
40060b57cec5SDimitry Andric     // then it takes an extra AGU (Address Generation Unit) cycle.
40070b57cec5SDimitry Andric     if ((RegNo % 2) || UseAlign < 8)
40080b57cec5SDimitry Andric       ++UseCycle;
40090b57cec5SDimitry Andric   } else {
40100b57cec5SDimitry Andric     // Assume the worst.
40110b57cec5SDimitry Andric     UseCycle = 1;
40120b57cec5SDimitry Andric   }
40130b57cec5SDimitry Andric   return UseCycle;
40140b57cec5SDimitry Andric }
40150b57cec5SDimitry Andric 
4016*5f757f3fSDimitry Andric std::optional<unsigned> ARMBaseInstrInfo::getOperandLatency(
4017*5f757f3fSDimitry Andric     const InstrItineraryData *ItinData, const MCInstrDesc &DefMCID,
4018*5f757f3fSDimitry Andric     unsigned DefIdx, unsigned DefAlign, const MCInstrDesc &UseMCID,
40190b57cec5SDimitry Andric     unsigned UseIdx, unsigned UseAlign) const {
40200b57cec5SDimitry Andric   unsigned DefClass = DefMCID.getSchedClass();
40210b57cec5SDimitry Andric   unsigned UseClass = UseMCID.getSchedClass();
40220b57cec5SDimitry Andric 
40230b57cec5SDimitry Andric   if (DefIdx < DefMCID.getNumDefs() && UseIdx < UseMCID.getNumOperands())
40240b57cec5SDimitry Andric     return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
40250b57cec5SDimitry Andric 
40260b57cec5SDimitry Andric   // This may be a def / use of a variable_ops instruction, the operand
40270b57cec5SDimitry Andric   // latency might be determinable dynamically. Let the target try to
40280b57cec5SDimitry Andric   // figure it out.
4029*5f757f3fSDimitry Andric   std::optional<unsigned> DefCycle;
40300b57cec5SDimitry Andric   bool LdmBypass = false;
40310b57cec5SDimitry Andric   switch (DefMCID.getOpcode()) {
40320b57cec5SDimitry Andric   default:
40330b57cec5SDimitry Andric     DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
40340b57cec5SDimitry Andric     break;
40350b57cec5SDimitry Andric 
40360b57cec5SDimitry Andric   case ARM::VLDMDIA:
40370b57cec5SDimitry Andric   case ARM::VLDMDIA_UPD:
40380b57cec5SDimitry Andric   case ARM::VLDMDDB_UPD:
40390b57cec5SDimitry Andric   case ARM::VLDMSIA:
40400b57cec5SDimitry Andric   case ARM::VLDMSIA_UPD:
40410b57cec5SDimitry Andric   case ARM::VLDMSDB_UPD:
40420b57cec5SDimitry Andric     DefCycle = getVLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
40430b57cec5SDimitry Andric     break;
40440b57cec5SDimitry Andric 
40450b57cec5SDimitry Andric   case ARM::LDMIA_RET:
40460b57cec5SDimitry Andric   case ARM::LDMIA:
40470b57cec5SDimitry Andric   case ARM::LDMDA:
40480b57cec5SDimitry Andric   case ARM::LDMDB:
40490b57cec5SDimitry Andric   case ARM::LDMIB:
40500b57cec5SDimitry Andric   case ARM::LDMIA_UPD:
40510b57cec5SDimitry Andric   case ARM::LDMDA_UPD:
40520b57cec5SDimitry Andric   case ARM::LDMDB_UPD:
40530b57cec5SDimitry Andric   case ARM::LDMIB_UPD:
40540b57cec5SDimitry Andric   case ARM::tLDMIA:
40550b57cec5SDimitry Andric   case ARM::tLDMIA_UPD:
40560b57cec5SDimitry Andric   case ARM::tPUSH:
40570b57cec5SDimitry Andric   case ARM::t2LDMIA_RET:
40580b57cec5SDimitry Andric   case ARM::t2LDMIA:
40590b57cec5SDimitry Andric   case ARM::t2LDMDB:
40600b57cec5SDimitry Andric   case ARM::t2LDMIA_UPD:
40610b57cec5SDimitry Andric   case ARM::t2LDMDB_UPD:
40620b57cec5SDimitry Andric     LdmBypass = true;
40630b57cec5SDimitry Andric     DefCycle = getLDMDefCycle(ItinData, DefMCID, DefClass, DefIdx, DefAlign);
40640b57cec5SDimitry Andric     break;
40650b57cec5SDimitry Andric   }
40660b57cec5SDimitry Andric 
4067*5f757f3fSDimitry Andric   if (!DefCycle)
40680b57cec5SDimitry Andric     // We can't seem to determine the result latency of the def, assume it's 2.
40690b57cec5SDimitry Andric     DefCycle = 2;
40700b57cec5SDimitry Andric 
4071*5f757f3fSDimitry Andric   std::optional<unsigned> UseCycle;
40720b57cec5SDimitry Andric   switch (UseMCID.getOpcode()) {
40730b57cec5SDimitry Andric   default:
40740b57cec5SDimitry Andric     UseCycle = ItinData->getOperandCycle(UseClass, UseIdx);
40750b57cec5SDimitry Andric     break;
40760b57cec5SDimitry Andric 
40770b57cec5SDimitry Andric   case ARM::VSTMDIA:
40780b57cec5SDimitry Andric   case ARM::VSTMDIA_UPD:
40790b57cec5SDimitry Andric   case ARM::VSTMDDB_UPD:
40800b57cec5SDimitry Andric   case ARM::VSTMSIA:
40810b57cec5SDimitry Andric   case ARM::VSTMSIA_UPD:
40820b57cec5SDimitry Andric   case ARM::VSTMSDB_UPD:
40830b57cec5SDimitry Andric     UseCycle = getVSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
40840b57cec5SDimitry Andric     break;
40850b57cec5SDimitry Andric 
40860b57cec5SDimitry Andric   case ARM::STMIA:
40870b57cec5SDimitry Andric   case ARM::STMDA:
40880b57cec5SDimitry Andric   case ARM::STMDB:
40890b57cec5SDimitry Andric   case ARM::STMIB:
40900b57cec5SDimitry Andric   case ARM::STMIA_UPD:
40910b57cec5SDimitry Andric   case ARM::STMDA_UPD:
40920b57cec5SDimitry Andric   case ARM::STMDB_UPD:
40930b57cec5SDimitry Andric   case ARM::STMIB_UPD:
40940b57cec5SDimitry Andric   case ARM::tSTMIA_UPD:
40950b57cec5SDimitry Andric   case ARM::tPOP_RET:
40960b57cec5SDimitry Andric   case ARM::tPOP:
40970b57cec5SDimitry Andric   case ARM::t2STMIA:
40980b57cec5SDimitry Andric   case ARM::t2STMDB:
40990b57cec5SDimitry Andric   case ARM::t2STMIA_UPD:
41000b57cec5SDimitry Andric   case ARM::t2STMDB_UPD:
41010b57cec5SDimitry Andric     UseCycle = getSTMUseCycle(ItinData, UseMCID, UseClass, UseIdx, UseAlign);
41020b57cec5SDimitry Andric     break;
41030b57cec5SDimitry Andric   }
41040b57cec5SDimitry Andric 
4105*5f757f3fSDimitry Andric   if (!UseCycle)
41060b57cec5SDimitry Andric     // Assume it's read in the first stage.
41070b57cec5SDimitry Andric     UseCycle = 1;
41080b57cec5SDimitry Andric 
4109*5f757f3fSDimitry Andric   if (UseCycle > *DefCycle + 1)
4110*5f757f3fSDimitry Andric     return std::nullopt;
4111*5f757f3fSDimitry Andric 
4112*5f757f3fSDimitry Andric   UseCycle = *DefCycle - *UseCycle + 1;
4113*5f757f3fSDimitry Andric   if (UseCycle > 0u) {
41140b57cec5SDimitry Andric     if (LdmBypass) {
41150b57cec5SDimitry Andric       // It's a variable_ops instruction so we can't use DefIdx here. Just use
41160b57cec5SDimitry Andric       // first def operand.
41170b57cec5SDimitry Andric       if (ItinData->hasPipelineForwarding(DefClass, DefMCID.getNumOperands()-1,
41180b57cec5SDimitry Andric                                           UseClass, UseIdx))
4119*5f757f3fSDimitry Andric         UseCycle = *UseCycle - 1;
41200b57cec5SDimitry Andric     } else if (ItinData->hasPipelineForwarding(DefClass, DefIdx,
41210b57cec5SDimitry Andric                                                UseClass, UseIdx)) {
4122*5f757f3fSDimitry Andric       UseCycle = *UseCycle - 1;
41230b57cec5SDimitry Andric     }
41240b57cec5SDimitry Andric   }
41250b57cec5SDimitry Andric 
41260b57cec5SDimitry Andric   return UseCycle;
41270b57cec5SDimitry Andric }
41280b57cec5SDimitry Andric 
41290b57cec5SDimitry Andric static const MachineInstr *getBundledDefMI(const TargetRegisterInfo *TRI,
41300b57cec5SDimitry Andric                                            const MachineInstr *MI, unsigned Reg,
41310b57cec5SDimitry Andric                                            unsigned &DefIdx, unsigned &Dist) {
41320b57cec5SDimitry Andric   Dist = 0;
41330b57cec5SDimitry Andric 
41340b57cec5SDimitry Andric   MachineBasicBlock::const_iterator I = MI; ++I;
41350b57cec5SDimitry Andric   MachineBasicBlock::const_instr_iterator II = std::prev(I.getInstrIterator());
41360b57cec5SDimitry Andric   assert(II->isInsideBundle() && "Empty bundle?");
41370b57cec5SDimitry Andric 
41380b57cec5SDimitry Andric   int Idx = -1;
41390b57cec5SDimitry Andric   while (II->isInsideBundle()) {
41400b57cec5SDimitry Andric     Idx = II->findRegisterDefOperandIdx(Reg, false, true, TRI);
41410b57cec5SDimitry Andric     if (Idx != -1)
41420b57cec5SDimitry Andric       break;
41430b57cec5SDimitry Andric     --II;
41440b57cec5SDimitry Andric     ++Dist;
41450b57cec5SDimitry Andric   }
41460b57cec5SDimitry Andric 
41470b57cec5SDimitry Andric   assert(Idx != -1 && "Cannot find bundled definition!");
41480b57cec5SDimitry Andric   DefIdx = Idx;
41490b57cec5SDimitry Andric   return &*II;
41500b57cec5SDimitry Andric }
41510b57cec5SDimitry Andric 
41520b57cec5SDimitry Andric static const MachineInstr *getBundledUseMI(const TargetRegisterInfo *TRI,
41530b57cec5SDimitry Andric                                            const MachineInstr &MI, unsigned Reg,
41540b57cec5SDimitry Andric                                            unsigned &UseIdx, unsigned &Dist) {
41550b57cec5SDimitry Andric   Dist = 0;
41560b57cec5SDimitry Andric 
41570b57cec5SDimitry Andric   MachineBasicBlock::const_instr_iterator II = ++MI.getIterator();
41580b57cec5SDimitry Andric   assert(II->isInsideBundle() && "Empty bundle?");
41590b57cec5SDimitry Andric   MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
41600b57cec5SDimitry Andric 
41610b57cec5SDimitry Andric   // FIXME: This doesn't properly handle multiple uses.
41620b57cec5SDimitry Andric   int Idx = -1;
41630b57cec5SDimitry Andric   while (II != E && II->isInsideBundle()) {
41640b57cec5SDimitry Andric     Idx = II->findRegisterUseOperandIdx(Reg, false, TRI);
41650b57cec5SDimitry Andric     if (Idx != -1)
41660b57cec5SDimitry Andric       break;
41670b57cec5SDimitry Andric     if (II->getOpcode() != ARM::t2IT)
41680b57cec5SDimitry Andric       ++Dist;
41690b57cec5SDimitry Andric     ++II;
41700b57cec5SDimitry Andric   }
41710b57cec5SDimitry Andric 
41720b57cec5SDimitry Andric   if (Idx == -1) {
41730b57cec5SDimitry Andric     Dist = 0;
41740b57cec5SDimitry Andric     return nullptr;
41750b57cec5SDimitry Andric   }
41760b57cec5SDimitry Andric 
41770b57cec5SDimitry Andric   UseIdx = Idx;
41780b57cec5SDimitry Andric   return &*II;
41790b57cec5SDimitry Andric }
41800b57cec5SDimitry Andric 
41810b57cec5SDimitry Andric /// Return the number of cycles to add to (or subtract from) the static
41820b57cec5SDimitry Andric /// itinerary based on the def opcode and alignment. The caller will ensure that
41830b57cec5SDimitry Andric /// adjusted latency is at least one cycle.
41840b57cec5SDimitry Andric static int adjustDefLatency(const ARMSubtarget &Subtarget,
41850b57cec5SDimitry Andric                             const MachineInstr &DefMI,
41860b57cec5SDimitry Andric                             const MCInstrDesc &DefMCID, unsigned DefAlign) {
41870b57cec5SDimitry Andric   int Adjust = 0;
41880b57cec5SDimitry Andric   if (Subtarget.isCortexA8() || Subtarget.isLikeA9() || Subtarget.isCortexA7()) {
41890b57cec5SDimitry Andric     // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
41900b57cec5SDimitry Andric     // variants are one cycle cheaper.
41910b57cec5SDimitry Andric     switch (DefMCID.getOpcode()) {
41920b57cec5SDimitry Andric     default: break;
41930b57cec5SDimitry Andric     case ARM::LDRrs:
41940b57cec5SDimitry Andric     case ARM::LDRBrs: {
41950b57cec5SDimitry Andric       unsigned ShOpVal = DefMI.getOperand(3).getImm();
41960b57cec5SDimitry Andric       unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
41970b57cec5SDimitry Andric       if (ShImm == 0 ||
41980b57cec5SDimitry Andric           (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
41990b57cec5SDimitry Andric         --Adjust;
42000b57cec5SDimitry Andric       break;
42010b57cec5SDimitry Andric     }
42020b57cec5SDimitry Andric     case ARM::t2LDRs:
42030b57cec5SDimitry Andric     case ARM::t2LDRBs:
42040b57cec5SDimitry Andric     case ARM::t2LDRHs:
42050b57cec5SDimitry Andric     case ARM::t2LDRSHs: {
42060b57cec5SDimitry Andric       // Thumb2 mode: lsl only.
42070b57cec5SDimitry Andric       unsigned ShAmt = DefMI.getOperand(3).getImm();
42080b57cec5SDimitry Andric       if (ShAmt == 0 || ShAmt == 2)
42090b57cec5SDimitry Andric         --Adjust;
42100b57cec5SDimitry Andric       break;
42110b57cec5SDimitry Andric     }
42120b57cec5SDimitry Andric     }
42130b57cec5SDimitry Andric   } else if (Subtarget.isSwift()) {
42140b57cec5SDimitry Andric     // FIXME: Properly handle all of the latency adjustments for address
42150b57cec5SDimitry Andric     // writeback.
42160b57cec5SDimitry Andric     switch (DefMCID.getOpcode()) {
42170b57cec5SDimitry Andric     default: break;
42180b57cec5SDimitry Andric     case ARM::LDRrs:
42190b57cec5SDimitry Andric     case ARM::LDRBrs: {
42200b57cec5SDimitry Andric       unsigned ShOpVal = DefMI.getOperand(3).getImm();
42210b57cec5SDimitry Andric       bool isSub = ARM_AM::getAM2Op(ShOpVal) == ARM_AM::sub;
42220b57cec5SDimitry Andric       unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
42230b57cec5SDimitry Andric       if (!isSub &&
42240b57cec5SDimitry Andric           (ShImm == 0 ||
42250b57cec5SDimitry Andric            ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
42260b57cec5SDimitry Andric             ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl)))
42270b57cec5SDimitry Andric         Adjust -= 2;
42280b57cec5SDimitry Andric       else if (!isSub &&
42290b57cec5SDimitry Andric                ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
42300b57cec5SDimitry Andric         --Adjust;
42310b57cec5SDimitry Andric       break;
42320b57cec5SDimitry Andric     }
42330b57cec5SDimitry Andric     case ARM::t2LDRs:
42340b57cec5SDimitry Andric     case ARM::t2LDRBs:
42350b57cec5SDimitry Andric     case ARM::t2LDRHs:
42360b57cec5SDimitry Andric     case ARM::t2LDRSHs: {
42370b57cec5SDimitry Andric       // Thumb2 mode: lsl only.
42380b57cec5SDimitry Andric       unsigned ShAmt = DefMI.getOperand(3).getImm();
42390b57cec5SDimitry Andric       if (ShAmt == 0 || ShAmt == 1 || ShAmt == 2 || ShAmt == 3)
42400b57cec5SDimitry Andric         Adjust -= 2;
42410b57cec5SDimitry Andric       break;
42420b57cec5SDimitry Andric     }
42430b57cec5SDimitry Andric     }
42440b57cec5SDimitry Andric   }
42450b57cec5SDimitry Andric 
42460b57cec5SDimitry Andric   if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment()) {
42470b57cec5SDimitry Andric     switch (DefMCID.getOpcode()) {
42480b57cec5SDimitry Andric     default: break;
42490b57cec5SDimitry Andric     case ARM::VLD1q8:
42500b57cec5SDimitry Andric     case ARM::VLD1q16:
42510b57cec5SDimitry Andric     case ARM::VLD1q32:
42520b57cec5SDimitry Andric     case ARM::VLD1q64:
42530b57cec5SDimitry Andric     case ARM::VLD1q8wb_fixed:
42540b57cec5SDimitry Andric     case ARM::VLD1q16wb_fixed:
42550b57cec5SDimitry Andric     case ARM::VLD1q32wb_fixed:
42560b57cec5SDimitry Andric     case ARM::VLD1q64wb_fixed:
42570b57cec5SDimitry Andric     case ARM::VLD1q8wb_register:
42580b57cec5SDimitry Andric     case ARM::VLD1q16wb_register:
42590b57cec5SDimitry Andric     case ARM::VLD1q32wb_register:
42600b57cec5SDimitry Andric     case ARM::VLD1q64wb_register:
42610b57cec5SDimitry Andric     case ARM::VLD2d8:
42620b57cec5SDimitry Andric     case ARM::VLD2d16:
42630b57cec5SDimitry Andric     case ARM::VLD2d32:
42640b57cec5SDimitry Andric     case ARM::VLD2q8:
42650b57cec5SDimitry Andric     case ARM::VLD2q16:
42660b57cec5SDimitry Andric     case ARM::VLD2q32:
42670b57cec5SDimitry Andric     case ARM::VLD2d8wb_fixed:
42680b57cec5SDimitry Andric     case ARM::VLD2d16wb_fixed:
42690b57cec5SDimitry Andric     case ARM::VLD2d32wb_fixed:
42700b57cec5SDimitry Andric     case ARM::VLD2q8wb_fixed:
42710b57cec5SDimitry Andric     case ARM::VLD2q16wb_fixed:
42720b57cec5SDimitry Andric     case ARM::VLD2q32wb_fixed:
42730b57cec5SDimitry Andric     case ARM::VLD2d8wb_register:
42740b57cec5SDimitry Andric     case ARM::VLD2d16wb_register:
42750b57cec5SDimitry Andric     case ARM::VLD2d32wb_register:
42760b57cec5SDimitry Andric     case ARM::VLD2q8wb_register:
42770b57cec5SDimitry Andric     case ARM::VLD2q16wb_register:
42780b57cec5SDimitry Andric     case ARM::VLD2q32wb_register:
42790b57cec5SDimitry Andric     case ARM::VLD3d8:
42800b57cec5SDimitry Andric     case ARM::VLD3d16:
42810b57cec5SDimitry Andric     case ARM::VLD3d32:
42820b57cec5SDimitry Andric     case ARM::VLD1d64T:
42830b57cec5SDimitry Andric     case ARM::VLD3d8_UPD:
42840b57cec5SDimitry Andric     case ARM::VLD3d16_UPD:
42850b57cec5SDimitry Andric     case ARM::VLD3d32_UPD:
42860b57cec5SDimitry Andric     case ARM::VLD1d64Twb_fixed:
42870b57cec5SDimitry Andric     case ARM::VLD1d64Twb_register:
42880b57cec5SDimitry Andric     case ARM::VLD3q8_UPD:
42890b57cec5SDimitry Andric     case ARM::VLD3q16_UPD:
42900b57cec5SDimitry Andric     case ARM::VLD3q32_UPD:
42910b57cec5SDimitry Andric     case ARM::VLD4d8:
42920b57cec5SDimitry Andric     case ARM::VLD4d16:
42930b57cec5SDimitry Andric     case ARM::VLD4d32:
42940b57cec5SDimitry Andric     case ARM::VLD1d64Q:
42950b57cec5SDimitry Andric     case ARM::VLD4d8_UPD:
42960b57cec5SDimitry Andric     case ARM::VLD4d16_UPD:
42970b57cec5SDimitry Andric     case ARM::VLD4d32_UPD:
42980b57cec5SDimitry Andric     case ARM::VLD1d64Qwb_fixed:
42990b57cec5SDimitry Andric     case ARM::VLD1d64Qwb_register:
43000b57cec5SDimitry Andric     case ARM::VLD4q8_UPD:
43010b57cec5SDimitry Andric     case ARM::VLD4q16_UPD:
43020b57cec5SDimitry Andric     case ARM::VLD4q32_UPD:
43030b57cec5SDimitry Andric     case ARM::VLD1DUPq8:
43040b57cec5SDimitry Andric     case ARM::VLD1DUPq16:
43050b57cec5SDimitry Andric     case ARM::VLD1DUPq32:
43060b57cec5SDimitry Andric     case ARM::VLD1DUPq8wb_fixed:
43070b57cec5SDimitry Andric     case ARM::VLD1DUPq16wb_fixed:
43080b57cec5SDimitry Andric     case ARM::VLD1DUPq32wb_fixed:
43090b57cec5SDimitry Andric     case ARM::VLD1DUPq8wb_register:
43100b57cec5SDimitry Andric     case ARM::VLD1DUPq16wb_register:
43110b57cec5SDimitry Andric     case ARM::VLD1DUPq32wb_register:
43120b57cec5SDimitry Andric     case ARM::VLD2DUPd8:
43130b57cec5SDimitry Andric     case ARM::VLD2DUPd16:
43140b57cec5SDimitry Andric     case ARM::VLD2DUPd32:
43150b57cec5SDimitry Andric     case ARM::VLD2DUPd8wb_fixed:
43160b57cec5SDimitry Andric     case ARM::VLD2DUPd16wb_fixed:
43170b57cec5SDimitry Andric     case ARM::VLD2DUPd32wb_fixed:
43180b57cec5SDimitry Andric     case ARM::VLD2DUPd8wb_register:
43190b57cec5SDimitry Andric     case ARM::VLD2DUPd16wb_register:
43200b57cec5SDimitry Andric     case ARM::VLD2DUPd32wb_register:
43210b57cec5SDimitry Andric     case ARM::VLD4DUPd8:
43220b57cec5SDimitry Andric     case ARM::VLD4DUPd16:
43230b57cec5SDimitry Andric     case ARM::VLD4DUPd32:
43240b57cec5SDimitry Andric     case ARM::VLD4DUPd8_UPD:
43250b57cec5SDimitry Andric     case ARM::VLD4DUPd16_UPD:
43260b57cec5SDimitry Andric     case ARM::VLD4DUPd32_UPD:
43270b57cec5SDimitry Andric     case ARM::VLD1LNd8:
43280b57cec5SDimitry Andric     case ARM::VLD1LNd16:
43290b57cec5SDimitry Andric     case ARM::VLD1LNd32:
43300b57cec5SDimitry Andric     case ARM::VLD1LNd8_UPD:
43310b57cec5SDimitry Andric     case ARM::VLD1LNd16_UPD:
43320b57cec5SDimitry Andric     case ARM::VLD1LNd32_UPD:
43330b57cec5SDimitry Andric     case ARM::VLD2LNd8:
43340b57cec5SDimitry Andric     case ARM::VLD2LNd16:
43350b57cec5SDimitry Andric     case ARM::VLD2LNd32:
43360b57cec5SDimitry Andric     case ARM::VLD2LNq16:
43370b57cec5SDimitry Andric     case ARM::VLD2LNq32:
43380b57cec5SDimitry Andric     case ARM::VLD2LNd8_UPD:
43390b57cec5SDimitry Andric     case ARM::VLD2LNd16_UPD:
43400b57cec5SDimitry Andric     case ARM::VLD2LNd32_UPD:
43410b57cec5SDimitry Andric     case ARM::VLD2LNq16_UPD:
43420b57cec5SDimitry Andric     case ARM::VLD2LNq32_UPD:
43430b57cec5SDimitry Andric     case ARM::VLD4LNd8:
43440b57cec5SDimitry Andric     case ARM::VLD4LNd16:
43450b57cec5SDimitry Andric     case ARM::VLD4LNd32:
43460b57cec5SDimitry Andric     case ARM::VLD4LNq16:
43470b57cec5SDimitry Andric     case ARM::VLD4LNq32:
43480b57cec5SDimitry Andric     case ARM::VLD4LNd8_UPD:
43490b57cec5SDimitry Andric     case ARM::VLD4LNd16_UPD:
43500b57cec5SDimitry Andric     case ARM::VLD4LNd32_UPD:
43510b57cec5SDimitry Andric     case ARM::VLD4LNq16_UPD:
43520b57cec5SDimitry Andric     case ARM::VLD4LNq32_UPD:
43530b57cec5SDimitry Andric       // If the address is not 64-bit aligned, the latencies of these
43540b57cec5SDimitry Andric       // instructions increases by one.
43550b57cec5SDimitry Andric       ++Adjust;
43560b57cec5SDimitry Andric       break;
43570b57cec5SDimitry Andric     }
43580b57cec5SDimitry Andric   }
43590b57cec5SDimitry Andric   return Adjust;
43600b57cec5SDimitry Andric }
43610b57cec5SDimitry Andric 
4362*5f757f3fSDimitry Andric std::optional<unsigned> ARMBaseInstrInfo::getOperandLatency(
4363*5f757f3fSDimitry Andric     const InstrItineraryData *ItinData, const MachineInstr &DefMI,
4364*5f757f3fSDimitry Andric     unsigned DefIdx, const MachineInstr &UseMI, unsigned UseIdx) const {
43650b57cec5SDimitry Andric   // No operand latency. The caller may fall back to getInstrLatency.
43660b57cec5SDimitry Andric   if (!ItinData || ItinData->isEmpty())
4367*5f757f3fSDimitry Andric     return std::nullopt;
43680b57cec5SDimitry Andric 
43690b57cec5SDimitry Andric   const MachineOperand &DefMO = DefMI.getOperand(DefIdx);
43708bcb0991SDimitry Andric   Register Reg = DefMO.getReg();
43710b57cec5SDimitry Andric 
43720b57cec5SDimitry Andric   const MachineInstr *ResolvedDefMI = &DefMI;
43730b57cec5SDimitry Andric   unsigned DefAdj = 0;
43740b57cec5SDimitry Andric   if (DefMI.isBundle())
43750b57cec5SDimitry Andric     ResolvedDefMI =
43760b57cec5SDimitry Andric         getBundledDefMI(&getRegisterInfo(), &DefMI, Reg, DefIdx, DefAdj);
43770b57cec5SDimitry Andric   if (ResolvedDefMI->isCopyLike() || ResolvedDefMI->isInsertSubreg() ||
43780b57cec5SDimitry Andric       ResolvedDefMI->isRegSequence() || ResolvedDefMI->isImplicitDef()) {
43790b57cec5SDimitry Andric     return 1;
43800b57cec5SDimitry Andric   }
43810b57cec5SDimitry Andric 
43820b57cec5SDimitry Andric   const MachineInstr *ResolvedUseMI = &UseMI;
43830b57cec5SDimitry Andric   unsigned UseAdj = 0;
43840b57cec5SDimitry Andric   if (UseMI.isBundle()) {
43850b57cec5SDimitry Andric     ResolvedUseMI =
43860b57cec5SDimitry Andric         getBundledUseMI(&getRegisterInfo(), UseMI, Reg, UseIdx, UseAdj);
43870b57cec5SDimitry Andric     if (!ResolvedUseMI)
4388*5f757f3fSDimitry Andric       return std::nullopt;
43890b57cec5SDimitry Andric   }
43900b57cec5SDimitry Andric 
43910b57cec5SDimitry Andric   return getOperandLatencyImpl(
43920b57cec5SDimitry Andric       ItinData, *ResolvedDefMI, DefIdx, ResolvedDefMI->getDesc(), DefAdj, DefMO,
43930b57cec5SDimitry Andric       Reg, *ResolvedUseMI, UseIdx, ResolvedUseMI->getDesc(), UseAdj);
43940b57cec5SDimitry Andric }
43950b57cec5SDimitry Andric 
4396*5f757f3fSDimitry Andric std::optional<unsigned> ARMBaseInstrInfo::getOperandLatencyImpl(
43970b57cec5SDimitry Andric     const InstrItineraryData *ItinData, const MachineInstr &DefMI,
43980b57cec5SDimitry Andric     unsigned DefIdx, const MCInstrDesc &DefMCID, unsigned DefAdj,
43990b57cec5SDimitry Andric     const MachineOperand &DefMO, unsigned Reg, const MachineInstr &UseMI,
44000b57cec5SDimitry Andric     unsigned UseIdx, const MCInstrDesc &UseMCID, unsigned UseAdj) const {
44010b57cec5SDimitry Andric   if (Reg == ARM::CPSR) {
44020b57cec5SDimitry Andric     if (DefMI.getOpcode() == ARM::FMSTAT) {
44030b57cec5SDimitry Andric       // fpscr -> cpsr stalls over 20 cycles on A8 (and earlier?)
44040b57cec5SDimitry Andric       return Subtarget.isLikeA9() ? 1 : 20;
44050b57cec5SDimitry Andric     }
44060b57cec5SDimitry Andric 
44070b57cec5SDimitry Andric     // CPSR set and branch can be paired in the same cycle.
44080b57cec5SDimitry Andric     if (UseMI.isBranch())
44090b57cec5SDimitry Andric       return 0;
44100b57cec5SDimitry Andric 
44110b57cec5SDimitry Andric     // Otherwise it takes the instruction latency (generally one).
44120b57cec5SDimitry Andric     unsigned Latency = getInstrLatency(ItinData, DefMI);
44130b57cec5SDimitry Andric 
44140b57cec5SDimitry Andric     // For Thumb2 and -Os, prefer scheduling CPSR setting instruction close to
44150b57cec5SDimitry Andric     // its uses. Instructions which are otherwise scheduled between them may
44160b57cec5SDimitry Andric     // incur a code size penalty (not able to use the CPSR setting 16-bit
44170b57cec5SDimitry Andric     // instructions).
44180b57cec5SDimitry Andric     if (Latency > 0 && Subtarget.isThumb2()) {
44190b57cec5SDimitry Andric       const MachineFunction *MF = DefMI.getParent()->getParent();
44200b57cec5SDimitry Andric       // FIXME: Use Function::hasOptSize().
44210b57cec5SDimitry Andric       if (MF->getFunction().hasFnAttribute(Attribute::OptimizeForSize))
44220b57cec5SDimitry Andric         --Latency;
44230b57cec5SDimitry Andric     }
44240b57cec5SDimitry Andric     return Latency;
44250b57cec5SDimitry Andric   }
44260b57cec5SDimitry Andric 
44270b57cec5SDimitry Andric   if (DefMO.isImplicit() || UseMI.getOperand(UseIdx).isImplicit())
4428*5f757f3fSDimitry Andric     return std::nullopt;
44290b57cec5SDimitry Andric 
44300b57cec5SDimitry Andric   unsigned DefAlign = DefMI.hasOneMemOperand()
44315ffd83dbSDimitry Andric                           ? (*DefMI.memoperands_begin())->getAlign().value()
44320b57cec5SDimitry Andric                           : 0;
44330b57cec5SDimitry Andric   unsigned UseAlign = UseMI.hasOneMemOperand()
44345ffd83dbSDimitry Andric                           ? (*UseMI.memoperands_begin())->getAlign().value()
44350b57cec5SDimitry Andric                           : 0;
44360b57cec5SDimitry Andric 
44370b57cec5SDimitry Andric   // Get the itinerary's latency if possible, and handle variable_ops.
4438*5f757f3fSDimitry Andric   std::optional<unsigned> Latency = getOperandLatency(
4439*5f757f3fSDimitry Andric       ItinData, DefMCID, DefIdx, DefAlign, UseMCID, UseIdx, UseAlign);
44400b57cec5SDimitry Andric   // Unable to find operand latency. The caller may resort to getInstrLatency.
4441*5f757f3fSDimitry Andric   if (!Latency)
4442*5f757f3fSDimitry Andric     return std::nullopt;
44430b57cec5SDimitry Andric 
44440b57cec5SDimitry Andric   // Adjust for IT block position.
44450b57cec5SDimitry Andric   int Adj = DefAdj + UseAdj;
44460b57cec5SDimitry Andric 
44470b57cec5SDimitry Andric   // Adjust for dynamic def-side opcode variants not captured by the itinerary.
44480b57cec5SDimitry Andric   Adj += adjustDefLatency(Subtarget, DefMI, DefMCID, DefAlign);
4449*5f757f3fSDimitry Andric   if (Adj >= 0 || (int)*Latency > -Adj) {
4450*5f757f3fSDimitry Andric     return *Latency + Adj;
44510b57cec5SDimitry Andric   }
44520b57cec5SDimitry Andric   // Return the itinerary latency, which may be zero but not less than zero.
44530b57cec5SDimitry Andric   return Latency;
44540b57cec5SDimitry Andric }
44550b57cec5SDimitry Andric 
4456*5f757f3fSDimitry Andric std::optional<unsigned>
44570b57cec5SDimitry Andric ARMBaseInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
44580b57cec5SDimitry Andric                                     SDNode *DefNode, unsigned DefIdx,
44590b57cec5SDimitry Andric                                     SDNode *UseNode, unsigned UseIdx) const {
44600b57cec5SDimitry Andric   if (!DefNode->isMachineOpcode())
44610b57cec5SDimitry Andric     return 1;
44620b57cec5SDimitry Andric 
44630b57cec5SDimitry Andric   const MCInstrDesc &DefMCID = get(DefNode->getMachineOpcode());
44640b57cec5SDimitry Andric 
44650b57cec5SDimitry Andric   if (isZeroCost(DefMCID.Opcode))
44660b57cec5SDimitry Andric     return 0;
44670b57cec5SDimitry Andric 
44680b57cec5SDimitry Andric   if (!ItinData || ItinData->isEmpty())
44690b57cec5SDimitry Andric     return DefMCID.mayLoad() ? 3 : 1;
44700b57cec5SDimitry Andric 
44710b57cec5SDimitry Andric   if (!UseNode->isMachineOpcode()) {
4472*5f757f3fSDimitry Andric     std::optional<unsigned> Latency =
4473*5f757f3fSDimitry Andric         ItinData->getOperandCycle(DefMCID.getSchedClass(), DefIdx);
44740b57cec5SDimitry Andric     int Adj = Subtarget.getPreISelOperandLatencyAdjustment();
44750b57cec5SDimitry Andric     int Threshold = 1 + Adj;
4476*5f757f3fSDimitry Andric     return !Latency || Latency <= (unsigned)Threshold ? 1 : *Latency - Adj;
44770b57cec5SDimitry Andric   }
44780b57cec5SDimitry Andric 
44790b57cec5SDimitry Andric   const MCInstrDesc &UseMCID = get(UseNode->getMachineOpcode());
44808bcb0991SDimitry Andric   auto *DefMN = cast<MachineSDNode>(DefNode);
44810b57cec5SDimitry Andric   unsigned DefAlign = !DefMN->memoperands_empty()
44825ffd83dbSDimitry Andric                           ? (*DefMN->memoperands_begin())->getAlign().value()
44835ffd83dbSDimitry Andric                           : 0;
44848bcb0991SDimitry Andric   auto *UseMN = cast<MachineSDNode>(UseNode);
44850b57cec5SDimitry Andric   unsigned UseAlign = !UseMN->memoperands_empty()
44865ffd83dbSDimitry Andric                           ? (*UseMN->memoperands_begin())->getAlign().value()
44875ffd83dbSDimitry Andric                           : 0;
4488*5f757f3fSDimitry Andric   std::optional<unsigned> Latency = getOperandLatency(
4489*5f757f3fSDimitry Andric       ItinData, DefMCID, DefIdx, DefAlign, UseMCID, UseIdx, UseAlign);
4490*5f757f3fSDimitry Andric   if (!Latency)
4491*5f757f3fSDimitry Andric     return std::nullopt;
44920b57cec5SDimitry Andric 
4493*5f757f3fSDimitry Andric   if (Latency > 1U &&
44940b57cec5SDimitry Andric       (Subtarget.isCortexA8() || Subtarget.isLikeA9() ||
44950b57cec5SDimitry Andric        Subtarget.isCortexA7())) {
44960b57cec5SDimitry Andric     // FIXME: Shifter op hack: no shift (i.e. [r +/- r]) or [r + r << 2]
44970b57cec5SDimitry Andric     // variants are one cycle cheaper.
44980b57cec5SDimitry Andric     switch (DefMCID.getOpcode()) {
44990b57cec5SDimitry Andric     default: break;
45000b57cec5SDimitry Andric     case ARM::LDRrs:
45010b57cec5SDimitry Andric     case ARM::LDRBrs: {
45020b57cec5SDimitry Andric       unsigned ShOpVal =
45030b57cec5SDimitry Andric         cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
45040b57cec5SDimitry Andric       unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
45050b57cec5SDimitry Andric       if (ShImm == 0 ||
45060b57cec5SDimitry Andric           (ShImm == 2 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4507*5f757f3fSDimitry Andric         Latency = *Latency - 1;
45080b57cec5SDimitry Andric       break;
45090b57cec5SDimitry Andric     }
45100b57cec5SDimitry Andric     case ARM::t2LDRs:
45110b57cec5SDimitry Andric     case ARM::t2LDRBs:
45120b57cec5SDimitry Andric     case ARM::t2LDRHs:
45130b57cec5SDimitry Andric     case ARM::t2LDRSHs: {
45140b57cec5SDimitry Andric       // Thumb2 mode: lsl only.
45150b57cec5SDimitry Andric       unsigned ShAmt =
45160b57cec5SDimitry Andric         cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
45170b57cec5SDimitry Andric       if (ShAmt == 0 || ShAmt == 2)
4518*5f757f3fSDimitry Andric         Latency = *Latency - 1;
45190b57cec5SDimitry Andric       break;
45200b57cec5SDimitry Andric     }
45210b57cec5SDimitry Andric     }
4522*5f757f3fSDimitry Andric   } else if (DefIdx == 0 && Latency > 2U && Subtarget.isSwift()) {
45230b57cec5SDimitry Andric     // FIXME: Properly handle all of the latency adjustments for address
45240b57cec5SDimitry Andric     // writeback.
45250b57cec5SDimitry Andric     switch (DefMCID.getOpcode()) {
45260b57cec5SDimitry Andric     default: break;
45270b57cec5SDimitry Andric     case ARM::LDRrs:
45280b57cec5SDimitry Andric     case ARM::LDRBrs: {
45290b57cec5SDimitry Andric       unsigned ShOpVal =
45300b57cec5SDimitry Andric         cast<ConstantSDNode>(DefNode->getOperand(2))->getZExtValue();
45310b57cec5SDimitry Andric       unsigned ShImm = ARM_AM::getAM2Offset(ShOpVal);
45320b57cec5SDimitry Andric       if (ShImm == 0 ||
45330b57cec5SDimitry Andric           ((ShImm == 1 || ShImm == 2 || ShImm == 3) &&
45340b57cec5SDimitry Andric            ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsl))
4535*5f757f3fSDimitry Andric         Latency = *Latency - 2;
45360b57cec5SDimitry Andric       else if (ShImm == 1 && ARM_AM::getAM2ShiftOpc(ShOpVal) == ARM_AM::lsr)
4537*5f757f3fSDimitry Andric         Latency = *Latency - 1;
45380b57cec5SDimitry Andric       break;
45390b57cec5SDimitry Andric     }
45400b57cec5SDimitry Andric     case ARM::t2LDRs:
45410b57cec5SDimitry Andric     case ARM::t2LDRBs:
45420b57cec5SDimitry Andric     case ARM::t2LDRHs:
45430b57cec5SDimitry Andric     case ARM::t2LDRSHs:
45440b57cec5SDimitry Andric       // Thumb2 mode: lsl 0-3 only.
4545*5f757f3fSDimitry Andric       Latency = *Latency - 2;
45460b57cec5SDimitry Andric       break;
45470b57cec5SDimitry Andric     }
45480b57cec5SDimitry Andric   }
45490b57cec5SDimitry Andric 
45500b57cec5SDimitry Andric   if (DefAlign < 8 && Subtarget.checkVLDnAccessAlignment())
45510b57cec5SDimitry Andric     switch (DefMCID.getOpcode()) {
45520b57cec5SDimitry Andric     default: break;
45530b57cec5SDimitry Andric     case ARM::VLD1q8:
45540b57cec5SDimitry Andric     case ARM::VLD1q16:
45550b57cec5SDimitry Andric     case ARM::VLD1q32:
45560b57cec5SDimitry Andric     case ARM::VLD1q64:
45570b57cec5SDimitry Andric     case ARM::VLD1q8wb_register:
45580b57cec5SDimitry Andric     case ARM::VLD1q16wb_register:
45590b57cec5SDimitry Andric     case ARM::VLD1q32wb_register:
45600b57cec5SDimitry Andric     case ARM::VLD1q64wb_register:
45610b57cec5SDimitry Andric     case ARM::VLD1q8wb_fixed:
45620b57cec5SDimitry Andric     case ARM::VLD1q16wb_fixed:
45630b57cec5SDimitry Andric     case ARM::VLD1q32wb_fixed:
45640b57cec5SDimitry Andric     case ARM::VLD1q64wb_fixed:
45650b57cec5SDimitry Andric     case ARM::VLD2d8:
45660b57cec5SDimitry Andric     case ARM::VLD2d16:
45670b57cec5SDimitry Andric     case ARM::VLD2d32:
45680b57cec5SDimitry Andric     case ARM::VLD2q8Pseudo:
45690b57cec5SDimitry Andric     case ARM::VLD2q16Pseudo:
45700b57cec5SDimitry Andric     case ARM::VLD2q32Pseudo:
45710b57cec5SDimitry Andric     case ARM::VLD2d8wb_fixed:
45720b57cec5SDimitry Andric     case ARM::VLD2d16wb_fixed:
45730b57cec5SDimitry Andric     case ARM::VLD2d32wb_fixed:
45740b57cec5SDimitry Andric     case ARM::VLD2q8PseudoWB_fixed:
45750b57cec5SDimitry Andric     case ARM::VLD2q16PseudoWB_fixed:
45760b57cec5SDimitry Andric     case ARM::VLD2q32PseudoWB_fixed:
45770b57cec5SDimitry Andric     case ARM::VLD2d8wb_register:
45780b57cec5SDimitry Andric     case ARM::VLD2d16wb_register:
45790b57cec5SDimitry Andric     case ARM::VLD2d32wb_register:
45800b57cec5SDimitry Andric     case ARM::VLD2q8PseudoWB_register:
45810b57cec5SDimitry Andric     case ARM::VLD2q16PseudoWB_register:
45820b57cec5SDimitry Andric     case ARM::VLD2q32PseudoWB_register:
45830b57cec5SDimitry Andric     case ARM::VLD3d8Pseudo:
45840b57cec5SDimitry Andric     case ARM::VLD3d16Pseudo:
45850b57cec5SDimitry Andric     case ARM::VLD3d32Pseudo:
45860b57cec5SDimitry Andric     case ARM::VLD1d8TPseudo:
45870b57cec5SDimitry Andric     case ARM::VLD1d16TPseudo:
45880b57cec5SDimitry Andric     case ARM::VLD1d32TPseudo:
45890b57cec5SDimitry Andric     case ARM::VLD1d64TPseudo:
45900b57cec5SDimitry Andric     case ARM::VLD1d64TPseudoWB_fixed:
45910b57cec5SDimitry Andric     case ARM::VLD1d64TPseudoWB_register:
45920b57cec5SDimitry Andric     case ARM::VLD3d8Pseudo_UPD:
45930b57cec5SDimitry Andric     case ARM::VLD3d16Pseudo_UPD:
45940b57cec5SDimitry Andric     case ARM::VLD3d32Pseudo_UPD:
45950b57cec5SDimitry Andric     case ARM::VLD3q8Pseudo_UPD:
45960b57cec5SDimitry Andric     case ARM::VLD3q16Pseudo_UPD:
45970b57cec5SDimitry Andric     case ARM::VLD3q32Pseudo_UPD:
45980b57cec5SDimitry Andric     case ARM::VLD3q8oddPseudo:
45990b57cec5SDimitry Andric     case ARM::VLD3q16oddPseudo:
46000b57cec5SDimitry Andric     case ARM::VLD3q32oddPseudo:
46010b57cec5SDimitry Andric     case ARM::VLD3q8oddPseudo_UPD:
46020b57cec5SDimitry Andric     case ARM::VLD3q16oddPseudo_UPD:
46030b57cec5SDimitry Andric     case ARM::VLD3q32oddPseudo_UPD:
46040b57cec5SDimitry Andric     case ARM::VLD4d8Pseudo:
46050b57cec5SDimitry Andric     case ARM::VLD4d16Pseudo:
46060b57cec5SDimitry Andric     case ARM::VLD4d32Pseudo:
46070b57cec5SDimitry Andric     case ARM::VLD1d8QPseudo:
46080b57cec5SDimitry Andric     case ARM::VLD1d16QPseudo:
46090b57cec5SDimitry Andric     case ARM::VLD1d32QPseudo:
46100b57cec5SDimitry Andric     case ARM::VLD1d64QPseudo:
46110b57cec5SDimitry Andric     case ARM::VLD1d64QPseudoWB_fixed:
46120b57cec5SDimitry Andric     case ARM::VLD1d64QPseudoWB_register:
46130b57cec5SDimitry Andric     case ARM::VLD1q8HighQPseudo:
46140b57cec5SDimitry Andric     case ARM::VLD1q8LowQPseudo_UPD:
46150b57cec5SDimitry Andric     case ARM::VLD1q8HighTPseudo:
46160b57cec5SDimitry Andric     case ARM::VLD1q8LowTPseudo_UPD:
46170b57cec5SDimitry Andric     case ARM::VLD1q16HighQPseudo:
46180b57cec5SDimitry Andric     case ARM::VLD1q16LowQPseudo_UPD:
46190b57cec5SDimitry Andric     case ARM::VLD1q16HighTPseudo:
46200b57cec5SDimitry Andric     case ARM::VLD1q16LowTPseudo_UPD:
46210b57cec5SDimitry Andric     case ARM::VLD1q32HighQPseudo:
46220b57cec5SDimitry Andric     case ARM::VLD1q32LowQPseudo_UPD:
46230b57cec5SDimitry Andric     case ARM::VLD1q32HighTPseudo:
46240b57cec5SDimitry Andric     case ARM::VLD1q32LowTPseudo_UPD:
46250b57cec5SDimitry Andric     case ARM::VLD1q64HighQPseudo:
46260b57cec5SDimitry Andric     case ARM::VLD1q64LowQPseudo_UPD:
46270b57cec5SDimitry Andric     case ARM::VLD1q64HighTPseudo:
46280b57cec5SDimitry Andric     case ARM::VLD1q64LowTPseudo_UPD:
46290b57cec5SDimitry Andric     case ARM::VLD4d8Pseudo_UPD:
46300b57cec5SDimitry Andric     case ARM::VLD4d16Pseudo_UPD:
46310b57cec5SDimitry Andric     case ARM::VLD4d32Pseudo_UPD:
46320b57cec5SDimitry Andric     case ARM::VLD4q8Pseudo_UPD:
46330b57cec5SDimitry Andric     case ARM::VLD4q16Pseudo_UPD:
46340b57cec5SDimitry Andric     case ARM::VLD4q32Pseudo_UPD:
46350b57cec5SDimitry Andric     case ARM::VLD4q8oddPseudo:
46360b57cec5SDimitry Andric     case ARM::VLD4q16oddPseudo:
46370b57cec5SDimitry Andric     case ARM::VLD4q32oddPseudo:
46380b57cec5SDimitry Andric     case ARM::VLD4q8oddPseudo_UPD:
46390b57cec5SDimitry Andric     case ARM::VLD4q16oddPseudo_UPD:
46400b57cec5SDimitry Andric     case ARM::VLD4q32oddPseudo_UPD:
46410b57cec5SDimitry Andric     case ARM::VLD1DUPq8:
46420b57cec5SDimitry Andric     case ARM::VLD1DUPq16:
46430b57cec5SDimitry Andric     case ARM::VLD1DUPq32:
46440b57cec5SDimitry Andric     case ARM::VLD1DUPq8wb_fixed:
46450b57cec5SDimitry Andric     case ARM::VLD1DUPq16wb_fixed:
46460b57cec5SDimitry Andric     case ARM::VLD1DUPq32wb_fixed:
46470b57cec5SDimitry Andric     case ARM::VLD1DUPq8wb_register:
46480b57cec5SDimitry Andric     case ARM::VLD1DUPq16wb_register:
46490b57cec5SDimitry Andric     case ARM::VLD1DUPq32wb_register:
46500b57cec5SDimitry Andric     case ARM::VLD2DUPd8:
46510b57cec5SDimitry Andric     case ARM::VLD2DUPd16:
46520b57cec5SDimitry Andric     case ARM::VLD2DUPd32:
46530b57cec5SDimitry Andric     case ARM::VLD2DUPd8wb_fixed:
46540b57cec5SDimitry Andric     case ARM::VLD2DUPd16wb_fixed:
46550b57cec5SDimitry Andric     case ARM::VLD2DUPd32wb_fixed:
46560b57cec5SDimitry Andric     case ARM::VLD2DUPd8wb_register:
46570b57cec5SDimitry Andric     case ARM::VLD2DUPd16wb_register:
46580b57cec5SDimitry Andric     case ARM::VLD2DUPd32wb_register:
46590b57cec5SDimitry Andric     case ARM::VLD2DUPq8EvenPseudo:
46600b57cec5SDimitry Andric     case ARM::VLD2DUPq8OddPseudo:
46610b57cec5SDimitry Andric     case ARM::VLD2DUPq16EvenPseudo:
46620b57cec5SDimitry Andric     case ARM::VLD2DUPq16OddPseudo:
46630b57cec5SDimitry Andric     case ARM::VLD2DUPq32EvenPseudo:
46640b57cec5SDimitry Andric     case ARM::VLD2DUPq32OddPseudo:
46650b57cec5SDimitry Andric     case ARM::VLD3DUPq8EvenPseudo:
46660b57cec5SDimitry Andric     case ARM::VLD3DUPq8OddPseudo:
46670b57cec5SDimitry Andric     case ARM::VLD3DUPq16EvenPseudo:
46680b57cec5SDimitry Andric     case ARM::VLD3DUPq16OddPseudo:
46690b57cec5SDimitry Andric     case ARM::VLD3DUPq32EvenPseudo:
46700b57cec5SDimitry Andric     case ARM::VLD3DUPq32OddPseudo:
46710b57cec5SDimitry Andric     case ARM::VLD4DUPd8Pseudo:
46720b57cec5SDimitry Andric     case ARM::VLD4DUPd16Pseudo:
46730b57cec5SDimitry Andric     case ARM::VLD4DUPd32Pseudo:
46740b57cec5SDimitry Andric     case ARM::VLD4DUPd8Pseudo_UPD:
46750b57cec5SDimitry Andric     case ARM::VLD4DUPd16Pseudo_UPD:
46760b57cec5SDimitry Andric     case ARM::VLD4DUPd32Pseudo_UPD:
46770b57cec5SDimitry Andric     case ARM::VLD4DUPq8EvenPseudo:
46780b57cec5SDimitry Andric     case ARM::VLD4DUPq8OddPseudo:
46790b57cec5SDimitry Andric     case ARM::VLD4DUPq16EvenPseudo:
46800b57cec5SDimitry Andric     case ARM::VLD4DUPq16OddPseudo:
46810b57cec5SDimitry Andric     case ARM::VLD4DUPq32EvenPseudo:
46820b57cec5SDimitry Andric     case ARM::VLD4DUPq32OddPseudo:
46830b57cec5SDimitry Andric     case ARM::VLD1LNq8Pseudo:
46840b57cec5SDimitry Andric     case ARM::VLD1LNq16Pseudo:
46850b57cec5SDimitry Andric     case ARM::VLD1LNq32Pseudo:
46860b57cec5SDimitry Andric     case ARM::VLD1LNq8Pseudo_UPD:
46870b57cec5SDimitry Andric     case ARM::VLD1LNq16Pseudo_UPD:
46880b57cec5SDimitry Andric     case ARM::VLD1LNq32Pseudo_UPD:
46890b57cec5SDimitry Andric     case ARM::VLD2LNd8Pseudo:
46900b57cec5SDimitry Andric     case ARM::VLD2LNd16Pseudo:
46910b57cec5SDimitry Andric     case ARM::VLD2LNd32Pseudo:
46920b57cec5SDimitry Andric     case ARM::VLD2LNq16Pseudo:
46930b57cec5SDimitry Andric     case ARM::VLD2LNq32Pseudo:
46940b57cec5SDimitry Andric     case ARM::VLD2LNd8Pseudo_UPD:
46950b57cec5SDimitry Andric     case ARM::VLD2LNd16Pseudo_UPD:
46960b57cec5SDimitry Andric     case ARM::VLD2LNd32Pseudo_UPD:
46970b57cec5SDimitry Andric     case ARM::VLD2LNq16Pseudo_UPD:
46980b57cec5SDimitry Andric     case ARM::VLD2LNq32Pseudo_UPD:
46990b57cec5SDimitry Andric     case ARM::VLD4LNd8Pseudo:
47000b57cec5SDimitry Andric     case ARM::VLD4LNd16Pseudo:
47010b57cec5SDimitry Andric     case ARM::VLD4LNd32Pseudo:
47020b57cec5SDimitry Andric     case ARM::VLD4LNq16Pseudo:
47030b57cec5SDimitry Andric     case ARM::VLD4LNq32Pseudo:
47040b57cec5SDimitry Andric     case ARM::VLD4LNd8Pseudo_UPD:
47050b57cec5SDimitry Andric     case ARM::VLD4LNd16Pseudo_UPD:
47060b57cec5SDimitry Andric     case ARM::VLD4LNd32Pseudo_UPD:
47070b57cec5SDimitry Andric     case ARM::VLD4LNq16Pseudo_UPD:
47080b57cec5SDimitry Andric     case ARM::VLD4LNq32Pseudo_UPD:
47090b57cec5SDimitry Andric       // If the address is not 64-bit aligned, the latencies of these
47100b57cec5SDimitry Andric       // instructions increases by one.
4711*5f757f3fSDimitry Andric       Latency = *Latency + 1;
47120b57cec5SDimitry Andric       break;
47130b57cec5SDimitry Andric     }
47140b57cec5SDimitry Andric 
47150b57cec5SDimitry Andric   return Latency;
47160b57cec5SDimitry Andric }
47170b57cec5SDimitry Andric 
47180b57cec5SDimitry Andric unsigned ARMBaseInstrInfo::getPredicationCost(const MachineInstr &MI) const {
47190b57cec5SDimitry Andric   if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
47200b57cec5SDimitry Andric       MI.isImplicitDef())
47210b57cec5SDimitry Andric     return 0;
47220b57cec5SDimitry Andric 
47230b57cec5SDimitry Andric   if (MI.isBundle())
47240b57cec5SDimitry Andric     return 0;
47250b57cec5SDimitry Andric 
47260b57cec5SDimitry Andric   const MCInstrDesc &MCID = MI.getDesc();
47270b57cec5SDimitry Andric 
47280b57cec5SDimitry Andric   if (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
47290b57cec5SDimitry Andric                         !Subtarget.cheapPredicableCPSRDef())) {
47300b57cec5SDimitry Andric     // When predicated, CPSR is an additional source operand for CPSR updating
47310b57cec5SDimitry Andric     // instructions, this apparently increases their latencies.
47320b57cec5SDimitry Andric     return 1;
47330b57cec5SDimitry Andric   }
47340b57cec5SDimitry Andric   return 0;
47350b57cec5SDimitry Andric }
47360b57cec5SDimitry Andric 
47370b57cec5SDimitry Andric unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
47380b57cec5SDimitry Andric                                            const MachineInstr &MI,
47390b57cec5SDimitry Andric                                            unsigned *PredCost) const {
47400b57cec5SDimitry Andric   if (MI.isCopyLike() || MI.isInsertSubreg() || MI.isRegSequence() ||
47410b57cec5SDimitry Andric       MI.isImplicitDef())
47420b57cec5SDimitry Andric     return 1;
47430b57cec5SDimitry Andric 
47440b57cec5SDimitry Andric   // An instruction scheduler typically runs on unbundled instructions, however
47450b57cec5SDimitry Andric   // other passes may query the latency of a bundled instruction.
47460b57cec5SDimitry Andric   if (MI.isBundle()) {
47470b57cec5SDimitry Andric     unsigned Latency = 0;
47480b57cec5SDimitry Andric     MachineBasicBlock::const_instr_iterator I = MI.getIterator();
47490b57cec5SDimitry Andric     MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end();
47500b57cec5SDimitry Andric     while (++I != E && I->isInsideBundle()) {
47510b57cec5SDimitry Andric       if (I->getOpcode() != ARM::t2IT)
47520b57cec5SDimitry Andric         Latency += getInstrLatency(ItinData, *I, PredCost);
47530b57cec5SDimitry Andric     }
47540b57cec5SDimitry Andric     return Latency;
47550b57cec5SDimitry Andric   }
47560b57cec5SDimitry Andric 
47570b57cec5SDimitry Andric   const MCInstrDesc &MCID = MI.getDesc();
47580b57cec5SDimitry Andric   if (PredCost && (MCID.isCall() || (MCID.hasImplicitDefOfPhysReg(ARM::CPSR) &&
47590b57cec5SDimitry Andric                                      !Subtarget.cheapPredicableCPSRDef()))) {
47600b57cec5SDimitry Andric     // When predicated, CPSR is an additional source operand for CPSR updating
47610b57cec5SDimitry Andric     // instructions, this apparently increases their latencies.
47620b57cec5SDimitry Andric     *PredCost = 1;
47630b57cec5SDimitry Andric   }
47640b57cec5SDimitry Andric   // Be sure to call getStageLatency for an empty itinerary in case it has a
47650b57cec5SDimitry Andric   // valid MinLatency property.
47660b57cec5SDimitry Andric   if (!ItinData)
47670b57cec5SDimitry Andric     return MI.mayLoad() ? 3 : 1;
47680b57cec5SDimitry Andric 
47690b57cec5SDimitry Andric   unsigned Class = MCID.getSchedClass();
47700b57cec5SDimitry Andric 
47710b57cec5SDimitry Andric   // For instructions with variable uops, use uops as latency.
47720b57cec5SDimitry Andric   if (!ItinData->isEmpty() && ItinData->getNumMicroOps(Class) < 0)
47730b57cec5SDimitry Andric     return getNumMicroOps(ItinData, MI);
47740b57cec5SDimitry Andric 
47750b57cec5SDimitry Andric   // For the common case, fall back on the itinerary's latency.
47760b57cec5SDimitry Andric   unsigned Latency = ItinData->getStageLatency(Class);
47770b57cec5SDimitry Andric 
47780b57cec5SDimitry Andric   // Adjust for dynamic def-side opcode variants not captured by the itinerary.
47790b57cec5SDimitry Andric   unsigned DefAlign =
47805ffd83dbSDimitry Andric       MI.hasOneMemOperand() ? (*MI.memoperands_begin())->getAlign().value() : 0;
47810b57cec5SDimitry Andric   int Adj = adjustDefLatency(Subtarget, MI, MCID, DefAlign);
47820b57cec5SDimitry Andric   if (Adj >= 0 || (int)Latency > -Adj) {
47830b57cec5SDimitry Andric     return Latency + Adj;
47840b57cec5SDimitry Andric   }
47850b57cec5SDimitry Andric   return Latency;
47860b57cec5SDimitry Andric }
47870b57cec5SDimitry Andric 
4788*5f757f3fSDimitry Andric unsigned ARMBaseInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
47890b57cec5SDimitry Andric                                            SDNode *Node) const {
47900b57cec5SDimitry Andric   if (!Node->isMachineOpcode())
47910b57cec5SDimitry Andric     return 1;
47920b57cec5SDimitry Andric 
47930b57cec5SDimitry Andric   if (!ItinData || ItinData->isEmpty())
47940b57cec5SDimitry Andric     return 1;
47950b57cec5SDimitry Andric 
47960b57cec5SDimitry Andric   unsigned Opcode = Node->getMachineOpcode();
47970b57cec5SDimitry Andric   switch (Opcode) {
47980b57cec5SDimitry Andric   default:
47990b57cec5SDimitry Andric     return ItinData->getStageLatency(get(Opcode).getSchedClass());
48000b57cec5SDimitry Andric   case ARM::VLDMQIA:
48010b57cec5SDimitry Andric   case ARM::VSTMQIA:
48020b57cec5SDimitry Andric     return 2;
48030b57cec5SDimitry Andric   }
48040b57cec5SDimitry Andric }
48050b57cec5SDimitry Andric 
48060b57cec5SDimitry Andric bool ARMBaseInstrInfo::hasHighOperandLatency(const TargetSchedModel &SchedModel,
48070b57cec5SDimitry Andric                                              const MachineRegisterInfo *MRI,
48080b57cec5SDimitry Andric                                              const MachineInstr &DefMI,
48090b57cec5SDimitry Andric                                              unsigned DefIdx,
48100b57cec5SDimitry Andric                                              const MachineInstr &UseMI,
48110b57cec5SDimitry Andric                                              unsigned UseIdx) const {
48120b57cec5SDimitry Andric   unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
48130b57cec5SDimitry Andric   unsigned UDomain = UseMI.getDesc().TSFlags & ARMII::DomainMask;
48140b57cec5SDimitry Andric   if (Subtarget.nonpipelinedVFP() &&
48150b57cec5SDimitry Andric       (DDomain == ARMII::DomainVFP || UDomain == ARMII::DomainVFP))
48160b57cec5SDimitry Andric     return true;
48170b57cec5SDimitry Andric 
48180b57cec5SDimitry Andric   // Hoist VFP / NEON instructions with 4 or higher latency.
48190b57cec5SDimitry Andric   unsigned Latency =
48200b57cec5SDimitry Andric       SchedModel.computeOperandLatency(&DefMI, DefIdx, &UseMI, UseIdx);
48210b57cec5SDimitry Andric   if (Latency <= 3)
48220b57cec5SDimitry Andric     return false;
48230b57cec5SDimitry Andric   return DDomain == ARMII::DomainVFP || DDomain == ARMII::DomainNEON ||
48240b57cec5SDimitry Andric          UDomain == ARMII::DomainVFP || UDomain == ARMII::DomainNEON;
48250b57cec5SDimitry Andric }
48260b57cec5SDimitry Andric 
48270b57cec5SDimitry Andric bool ARMBaseInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
48280b57cec5SDimitry Andric                                         const MachineInstr &DefMI,
48290b57cec5SDimitry Andric                                         unsigned DefIdx) const {
48300b57cec5SDimitry Andric   const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
48310b57cec5SDimitry Andric   if (!ItinData || ItinData->isEmpty())
48320b57cec5SDimitry Andric     return false;
48330b57cec5SDimitry Andric 
48340b57cec5SDimitry Andric   unsigned DDomain = DefMI.getDesc().TSFlags & ARMII::DomainMask;
48350b57cec5SDimitry Andric   if (DDomain == ARMII::DomainGeneral) {
48360b57cec5SDimitry Andric     unsigned DefClass = DefMI.getDesc().getSchedClass();
4837*5f757f3fSDimitry Andric     std::optional<unsigned> DefCycle =
4838*5f757f3fSDimitry Andric         ItinData->getOperandCycle(DefClass, DefIdx);
4839*5f757f3fSDimitry Andric     return DefCycle && DefCycle <= 2U;
48400b57cec5SDimitry Andric   }
48410b57cec5SDimitry Andric   return false;
48420b57cec5SDimitry Andric }
48430b57cec5SDimitry Andric 
48440b57cec5SDimitry Andric bool ARMBaseInstrInfo::verifyInstruction(const MachineInstr &MI,
48450b57cec5SDimitry Andric                                          StringRef &ErrInfo) const {
48460b57cec5SDimitry Andric   if (convertAddSubFlagsOpcode(MI.getOpcode())) {
48470b57cec5SDimitry Andric     ErrInfo = "Pseudo flag setting opcodes only exist in Selection DAG";
48480b57cec5SDimitry Andric     return false;
48490b57cec5SDimitry Andric   }
48500b57cec5SDimitry Andric   if (MI.getOpcode() == ARM::tMOVr && !Subtarget.hasV6Ops()) {
48510b57cec5SDimitry Andric     // Make sure we don't generate a lo-lo mov that isn't supported.
48520b57cec5SDimitry Andric     if (!ARM::hGPRRegClass.contains(MI.getOperand(0).getReg()) &&
48530b57cec5SDimitry Andric         !ARM::hGPRRegClass.contains(MI.getOperand(1).getReg())) {
48540b57cec5SDimitry Andric       ErrInfo = "Non-flag-setting Thumb1 mov is v6-only";
48550b57cec5SDimitry Andric       return false;
48560b57cec5SDimitry Andric     }
48570b57cec5SDimitry Andric   }
48580b57cec5SDimitry Andric   if (MI.getOpcode() == ARM::tPUSH ||
48590b57cec5SDimitry Andric       MI.getOpcode() == ARM::tPOP ||
48600b57cec5SDimitry Andric       MI.getOpcode() == ARM::tPOP_RET) {
48614824e7fdSDimitry Andric     for (const MachineOperand &MO : llvm::drop_begin(MI.operands(), 2)) {
48624824e7fdSDimitry Andric       if (MO.isImplicit() || !MO.isReg())
48630b57cec5SDimitry Andric         continue;
48644824e7fdSDimitry Andric       Register Reg = MO.getReg();
48650b57cec5SDimitry Andric       if (Reg < ARM::R0 || Reg > ARM::R7) {
48660b57cec5SDimitry Andric         if (!(MI.getOpcode() == ARM::tPUSH && Reg == ARM::LR) &&
48670b57cec5SDimitry Andric             !(MI.getOpcode() == ARM::tPOP_RET && Reg == ARM::PC)) {
48680b57cec5SDimitry Andric           ErrInfo = "Unsupported register in Thumb1 push/pop";
48690b57cec5SDimitry Andric           return false;
48700b57cec5SDimitry Andric         }
48710b57cec5SDimitry Andric       }
48720b57cec5SDimitry Andric     }
48730b57cec5SDimitry Andric   }
4874e8d8bef9SDimitry Andric   if (MI.getOpcode() == ARM::MVE_VMOV_q_rr) {
4875e8d8bef9SDimitry Andric     assert(MI.getOperand(4).isImm() && MI.getOperand(5).isImm());
4876e8d8bef9SDimitry Andric     if ((MI.getOperand(4).getImm() != 2 && MI.getOperand(4).getImm() != 3) ||
4877e8d8bef9SDimitry Andric         MI.getOperand(4).getImm() != MI.getOperand(5).getImm() + 2) {
4878e8d8bef9SDimitry Andric       ErrInfo = "Incorrect array index for MVE_VMOV_q_rr";
4879e8d8bef9SDimitry Andric       return false;
4880e8d8bef9SDimitry Andric     }
4881e8d8bef9SDimitry Andric   }
488204eeddc0SDimitry Andric 
488304eeddc0SDimitry Andric   // Check the address model by taking the first Imm operand and checking it is
488404eeddc0SDimitry Andric   // legal for that addressing mode.
488504eeddc0SDimitry Andric   ARMII::AddrMode AddrMode =
488604eeddc0SDimitry Andric       (ARMII::AddrMode)(MI.getDesc().TSFlags & ARMII::AddrModeMask);
488704eeddc0SDimitry Andric   switch (AddrMode) {
488804eeddc0SDimitry Andric   default:
488904eeddc0SDimitry Andric     break;
489004eeddc0SDimitry Andric   case ARMII::AddrModeT2_i7:
489104eeddc0SDimitry Andric   case ARMII::AddrModeT2_i7s2:
489204eeddc0SDimitry Andric   case ARMII::AddrModeT2_i7s4:
489304eeddc0SDimitry Andric   case ARMII::AddrModeT2_i8:
489404eeddc0SDimitry Andric   case ARMII::AddrModeT2_i8pos:
489504eeddc0SDimitry Andric   case ARMII::AddrModeT2_i8neg:
489604eeddc0SDimitry Andric   case ARMII::AddrModeT2_i8s4:
489704eeddc0SDimitry Andric   case ARMII::AddrModeT2_i12: {
489804eeddc0SDimitry Andric     uint32_t Imm = 0;
489904eeddc0SDimitry Andric     for (auto Op : MI.operands()) {
490004eeddc0SDimitry Andric       if (Op.isImm()) {
490104eeddc0SDimitry Andric         Imm = Op.getImm();
490204eeddc0SDimitry Andric         break;
490304eeddc0SDimitry Andric       }
490404eeddc0SDimitry Andric     }
490504eeddc0SDimitry Andric     if (!isLegalAddressImm(MI.getOpcode(), Imm, this)) {
490604eeddc0SDimitry Andric       ErrInfo = "Incorrect AddrMode Imm for instruction";
490704eeddc0SDimitry Andric       return false;
490804eeddc0SDimitry Andric     }
490904eeddc0SDimitry Andric     break;
491004eeddc0SDimitry Andric   }
491104eeddc0SDimitry Andric   }
49120b57cec5SDimitry Andric   return true;
49130b57cec5SDimitry Andric }
49140b57cec5SDimitry Andric 
49150b57cec5SDimitry Andric void ARMBaseInstrInfo::expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
49160b57cec5SDimitry Andric                                                 unsigned LoadImmOpc,
49170b57cec5SDimitry Andric                                                 unsigned LoadOpc) const {
49180b57cec5SDimitry Andric   assert(!Subtarget.isROPI() && !Subtarget.isRWPI() &&
49190b57cec5SDimitry Andric          "ROPI/RWPI not currently supported with stack guard");
49200b57cec5SDimitry Andric 
49210b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI->getParent();
49220b57cec5SDimitry Andric   DebugLoc DL = MI->getDebugLoc();
49238bcb0991SDimitry Andric   Register Reg = MI->getOperand(0).getReg();
49240b57cec5SDimitry Andric   MachineInstrBuilder MIB;
4925349cc55cSDimitry Andric   unsigned int Offset = 0;
4926349cc55cSDimitry Andric 
4927349cc55cSDimitry Andric   if (LoadImmOpc == ARM::MRC || LoadImmOpc == ARM::t2MRC) {
492806c3fb27SDimitry Andric     assert(!Subtarget.isReadTPSoft() &&
4929349cc55cSDimitry Andric            "TLS stack protector requires hardware TLS register");
49300b57cec5SDimitry Andric 
49310b57cec5SDimitry Andric     BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4932349cc55cSDimitry Andric         .addImm(15)
4933349cc55cSDimitry Andric         .addImm(0)
4934349cc55cSDimitry Andric         .addImm(13)
4935349cc55cSDimitry Andric         .addImm(0)
4936349cc55cSDimitry Andric         .addImm(3)
4937349cc55cSDimitry Andric         .add(predOps(ARMCC::AL));
49380b57cec5SDimitry Andric 
4939349cc55cSDimitry Andric     Module &M = *MBB.getParent()->getFunction().getParent();
4940349cc55cSDimitry Andric     Offset = M.getStackProtectorGuardOffset();
4941349cc55cSDimitry Andric     if (Offset & ~0xfffU) {
4942349cc55cSDimitry Andric       // The offset won't fit in the LDR's 12-bit immediate field, so emit an
4943349cc55cSDimitry Andric       // extra ADD to cover the delta. This gives us a guaranteed 8 additional
4944349cc55cSDimitry Andric       // bits, resulting in a range of 0 to +1 MiB for the guard offset.
4945349cc55cSDimitry Andric       unsigned AddOpc = (LoadImmOpc == ARM::MRC) ? ARM::ADDri : ARM::t2ADDri;
4946349cc55cSDimitry Andric       BuildMI(MBB, MI, DL, get(AddOpc), Reg)
4947349cc55cSDimitry Andric           .addReg(Reg, RegState::Kill)
4948349cc55cSDimitry Andric           .addImm(Offset & ~0xfffU)
4949349cc55cSDimitry Andric           .add(predOps(ARMCC::AL))
4950349cc55cSDimitry Andric           .addReg(0);
4951349cc55cSDimitry Andric       Offset &= 0xfffU;
4952349cc55cSDimitry Andric     }
4953349cc55cSDimitry Andric   } else {
4954349cc55cSDimitry Andric     const GlobalValue *GV =
4955349cc55cSDimitry Andric         cast<GlobalValue>((*MI->memoperands_begin())->getValue());
4956349cc55cSDimitry Andric     bool IsIndirect = Subtarget.isGVIndirectSymbol(GV);
4957349cc55cSDimitry Andric 
4958349cc55cSDimitry Andric     unsigned TargetFlags = ARMII::MO_NO_FLAG;
4959349cc55cSDimitry Andric     if (Subtarget.isTargetMachO()) {
4960349cc55cSDimitry Andric       TargetFlags |= ARMII::MO_NONLAZY;
4961349cc55cSDimitry Andric     } else if (Subtarget.isTargetCOFF()) {
4962349cc55cSDimitry Andric       if (GV->hasDLLImportStorageClass())
4963349cc55cSDimitry Andric         TargetFlags |= ARMII::MO_DLLIMPORT;
4964349cc55cSDimitry Andric       else if (IsIndirect)
4965349cc55cSDimitry Andric         TargetFlags |= ARMII::MO_COFFSTUB;
4966*5f757f3fSDimitry Andric     } else if (IsIndirect) {
4967349cc55cSDimitry Andric       TargetFlags |= ARMII::MO_GOT;
4968349cc55cSDimitry Andric     }
4969349cc55cSDimitry Andric 
4970*5f757f3fSDimitry Andric     if (LoadImmOpc == ARM::tMOVi32imm) { // Thumb-1 execute-only
4971*5f757f3fSDimitry Andric       Register CPSRSaveReg = ARM::R12; // Use R12 as scratch register
4972*5f757f3fSDimitry Andric       auto APSREncoding =
4973*5f757f3fSDimitry Andric           ARMSysReg::lookupMClassSysRegByName("apsr_nzcvq")->Encoding;
4974*5f757f3fSDimitry Andric       BuildMI(MBB, MI, DL, get(ARM::t2MRS_M), CPSRSaveReg)
4975*5f757f3fSDimitry Andric           .addImm(APSREncoding)
4976*5f757f3fSDimitry Andric           .add(predOps(ARMCC::AL));
4977349cc55cSDimitry Andric       BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4978349cc55cSDimitry Andric           .addGlobalAddress(GV, 0, TargetFlags);
4979*5f757f3fSDimitry Andric       BuildMI(MBB, MI, DL, get(ARM::t2MSR_M))
4980*5f757f3fSDimitry Andric           .addImm(APSREncoding)
4981*5f757f3fSDimitry Andric           .addReg(CPSRSaveReg, RegState::Kill)
4982*5f757f3fSDimitry Andric           .add(predOps(ARMCC::AL));
4983*5f757f3fSDimitry Andric     } else {
4984*5f757f3fSDimitry Andric       BuildMI(MBB, MI, DL, get(LoadImmOpc), Reg)
4985*5f757f3fSDimitry Andric           .addGlobalAddress(GV, 0, TargetFlags);
4986*5f757f3fSDimitry Andric     }
4987349cc55cSDimitry Andric 
4988349cc55cSDimitry Andric     if (IsIndirect) {
49890b57cec5SDimitry Andric       MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
49900b57cec5SDimitry Andric       MIB.addReg(Reg, RegState::Kill).addImm(0);
49910b57cec5SDimitry Andric       auto Flags = MachineMemOperand::MOLoad |
49920b57cec5SDimitry Andric                    MachineMemOperand::MODereferenceable |
49930b57cec5SDimitry Andric                    MachineMemOperand::MOInvariant;
49940b57cec5SDimitry Andric       MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
49955ffd83dbSDimitry Andric           MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, Align(4));
49960b57cec5SDimitry Andric       MIB.addMemOperand(MMO).add(predOps(ARMCC::AL));
49970b57cec5SDimitry Andric     }
4998349cc55cSDimitry Andric   }
49990b57cec5SDimitry Andric 
50000b57cec5SDimitry Andric   MIB = BuildMI(MBB, MI, DL, get(LoadOpc), Reg);
50010b57cec5SDimitry Andric   MIB.addReg(Reg, RegState::Kill)
5002349cc55cSDimitry Andric       .addImm(Offset)
50030b57cec5SDimitry Andric       .cloneMemRefs(*MI)
50040b57cec5SDimitry Andric       .add(predOps(ARMCC::AL));
50050b57cec5SDimitry Andric }
50060b57cec5SDimitry Andric 
50070b57cec5SDimitry Andric bool
50080b57cec5SDimitry Andric ARMBaseInstrInfo::isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
50090b57cec5SDimitry Andric                                      unsigned &AddSubOpc,
50100b57cec5SDimitry Andric                                      bool &NegAcc, bool &HasLane) const {
50110b57cec5SDimitry Andric   DenseMap<unsigned, unsigned>::const_iterator I = MLxEntryMap.find(Opcode);
50120b57cec5SDimitry Andric   if (I == MLxEntryMap.end())
50130b57cec5SDimitry Andric     return false;
50140b57cec5SDimitry Andric 
50150b57cec5SDimitry Andric   const ARM_MLxEntry &Entry = ARM_MLxTable[I->second];
50160b57cec5SDimitry Andric   MulOpc = Entry.MulOpc;
50170b57cec5SDimitry Andric   AddSubOpc = Entry.AddSubOpc;
50180b57cec5SDimitry Andric   NegAcc = Entry.NegAcc;
50190b57cec5SDimitry Andric   HasLane = Entry.HasLane;
50200b57cec5SDimitry Andric   return true;
50210b57cec5SDimitry Andric }
50220b57cec5SDimitry Andric 
50230b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
50240b57cec5SDimitry Andric // Execution domains.
50250b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
50260b57cec5SDimitry Andric //
50270b57cec5SDimitry Andric // Some instructions go down the NEON pipeline, some go down the VFP pipeline,
50280b57cec5SDimitry Andric // and some can go down both.  The vmov instructions go down the VFP pipeline,
50290b57cec5SDimitry Andric // but they can be changed to vorr equivalents that are executed by the NEON
50300b57cec5SDimitry Andric // pipeline.
50310b57cec5SDimitry Andric //
50320b57cec5SDimitry Andric // We use the following execution domain numbering:
50330b57cec5SDimitry Andric //
50340b57cec5SDimitry Andric enum ARMExeDomain {
50350b57cec5SDimitry Andric   ExeGeneric = 0,
50360b57cec5SDimitry Andric   ExeVFP = 1,
50370b57cec5SDimitry Andric   ExeNEON = 2
50380b57cec5SDimitry Andric };
50390b57cec5SDimitry Andric 
50400b57cec5SDimitry Andric //
50410b57cec5SDimitry Andric // Also see ARMInstrFormats.td and Domain* enums in ARMBaseInfo.h
50420b57cec5SDimitry Andric //
50430b57cec5SDimitry Andric std::pair<uint16_t, uint16_t>
50440b57cec5SDimitry Andric ARMBaseInstrInfo::getExecutionDomain(const MachineInstr &MI) const {
50450b57cec5SDimitry Andric   // If we don't have access to NEON instructions then we won't be able
50460b57cec5SDimitry Andric   // to swizzle anything to the NEON domain. Check to make sure.
50470b57cec5SDimitry Andric   if (Subtarget.hasNEON()) {
50480b57cec5SDimitry Andric     // VMOVD, VMOVRS and VMOVSR are VFP instructions, but can be changed to NEON
50490b57cec5SDimitry Andric     // if they are not predicated.
50500b57cec5SDimitry Andric     if (MI.getOpcode() == ARM::VMOVD && !isPredicated(MI))
50510b57cec5SDimitry Andric       return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
50520b57cec5SDimitry Andric 
50530b57cec5SDimitry Andric     // CortexA9 is particularly picky about mixing the two and wants these
50540b57cec5SDimitry Andric     // converted.
50550b57cec5SDimitry Andric     if (Subtarget.useNEONForFPMovs() && !isPredicated(MI) &&
50560b57cec5SDimitry Andric         (MI.getOpcode() == ARM::VMOVRS || MI.getOpcode() == ARM::VMOVSR ||
50570b57cec5SDimitry Andric          MI.getOpcode() == ARM::VMOVS))
50580b57cec5SDimitry Andric       return std::make_pair(ExeVFP, (1 << ExeVFP) | (1 << ExeNEON));
50590b57cec5SDimitry Andric   }
50600b57cec5SDimitry Andric   // No other instructions can be swizzled, so just determine their domain.
50610b57cec5SDimitry Andric   unsigned Domain = MI.getDesc().TSFlags & ARMII::DomainMask;
50620b57cec5SDimitry Andric 
50630b57cec5SDimitry Andric   if (Domain & ARMII::DomainNEON)
50640b57cec5SDimitry Andric     return std::make_pair(ExeNEON, 0);
50650b57cec5SDimitry Andric 
50660b57cec5SDimitry Andric   // Certain instructions can go either way on Cortex-A8.
50670b57cec5SDimitry Andric   // Treat them as NEON instructions.
50680b57cec5SDimitry Andric   if ((Domain & ARMII::DomainNEONA8) && Subtarget.isCortexA8())
50690b57cec5SDimitry Andric     return std::make_pair(ExeNEON, 0);
50700b57cec5SDimitry Andric 
50710b57cec5SDimitry Andric   if (Domain & ARMII::DomainVFP)
50720b57cec5SDimitry Andric     return std::make_pair(ExeVFP, 0);
50730b57cec5SDimitry Andric 
50740b57cec5SDimitry Andric   return std::make_pair(ExeGeneric, 0);
50750b57cec5SDimitry Andric }
50760b57cec5SDimitry Andric 
50770b57cec5SDimitry Andric static unsigned getCorrespondingDRegAndLane(const TargetRegisterInfo *TRI,
50780b57cec5SDimitry Andric                                             unsigned SReg, unsigned &Lane) {
50790b57cec5SDimitry Andric   unsigned DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_0, &ARM::DPRRegClass);
50800b57cec5SDimitry Andric   Lane = 0;
50810b57cec5SDimitry Andric 
50820b57cec5SDimitry Andric   if (DReg != ARM::NoRegister)
50830b57cec5SDimitry Andric    return DReg;
50840b57cec5SDimitry Andric 
50850b57cec5SDimitry Andric   Lane = 1;
50860b57cec5SDimitry Andric   DReg = TRI->getMatchingSuperReg(SReg, ARM::ssub_1, &ARM::DPRRegClass);
50870b57cec5SDimitry Andric 
50880b57cec5SDimitry Andric   assert(DReg && "S-register with no D super-register?");
50890b57cec5SDimitry Andric   return DReg;
50900b57cec5SDimitry Andric }
50910b57cec5SDimitry Andric 
50920b57cec5SDimitry Andric /// getImplicitSPRUseForDPRUse - Given a use of a DPR register and lane,
50930b57cec5SDimitry Andric /// set ImplicitSReg to a register number that must be marked as implicit-use or
50940b57cec5SDimitry Andric /// zero if no register needs to be defined as implicit-use.
50950b57cec5SDimitry Andric ///
50960b57cec5SDimitry Andric /// If the function cannot determine if an SPR should be marked implicit use or
50970b57cec5SDimitry Andric /// not, it returns false.
50980b57cec5SDimitry Andric ///
50990b57cec5SDimitry Andric /// This function handles cases where an instruction is being modified from taking
51000b57cec5SDimitry Andric /// an SPR to a DPR[Lane]. A use of the DPR is being added, which may conflict
51010b57cec5SDimitry Andric /// with an earlier def of an SPR corresponding to DPR[Lane^1] (i.e. the other
51020b57cec5SDimitry Andric /// lane of the DPR).
51030b57cec5SDimitry Andric ///
51040b57cec5SDimitry Andric /// If the other SPR is defined, an implicit-use of it should be added. Else,
51050b57cec5SDimitry Andric /// (including the case where the DPR itself is defined), it should not.
51060b57cec5SDimitry Andric ///
51070b57cec5SDimitry Andric static bool getImplicitSPRUseForDPRUse(const TargetRegisterInfo *TRI,
51080b57cec5SDimitry Andric                                        MachineInstr &MI, unsigned DReg,
51090b57cec5SDimitry Andric                                        unsigned Lane, unsigned &ImplicitSReg) {
51100b57cec5SDimitry Andric   // If the DPR is defined or used already, the other SPR lane will be chained
51110b57cec5SDimitry Andric   // correctly, so there is nothing to be done.
51120b57cec5SDimitry Andric   if (MI.definesRegister(DReg, TRI) || MI.readsRegister(DReg, TRI)) {
51130b57cec5SDimitry Andric     ImplicitSReg = 0;
51140b57cec5SDimitry Andric     return true;
51150b57cec5SDimitry Andric   }
51160b57cec5SDimitry Andric 
51170b57cec5SDimitry Andric   // Otherwise we need to go searching to see if the SPR is set explicitly.
51180b57cec5SDimitry Andric   ImplicitSReg = TRI->getSubReg(DReg,
51190b57cec5SDimitry Andric                                 (Lane & 1) ? ARM::ssub_0 : ARM::ssub_1);
51200b57cec5SDimitry Andric   MachineBasicBlock::LivenessQueryResult LQR =
51210b57cec5SDimitry Andric       MI.getParent()->computeRegisterLiveness(TRI, ImplicitSReg, MI);
51220b57cec5SDimitry Andric 
51230b57cec5SDimitry Andric   if (LQR == MachineBasicBlock::LQR_Live)
51240b57cec5SDimitry Andric     return true;
51250b57cec5SDimitry Andric   else if (LQR == MachineBasicBlock::LQR_Unknown)
51260b57cec5SDimitry Andric     return false;
51270b57cec5SDimitry Andric 
51280b57cec5SDimitry Andric   // If the register is known not to be live, there is no need to add an
51290b57cec5SDimitry Andric   // implicit-use.
51300b57cec5SDimitry Andric   ImplicitSReg = 0;
51310b57cec5SDimitry Andric   return true;
51320b57cec5SDimitry Andric }
51330b57cec5SDimitry Andric 
51340b57cec5SDimitry Andric void ARMBaseInstrInfo::setExecutionDomain(MachineInstr &MI,
51350b57cec5SDimitry Andric                                           unsigned Domain) const {
51360b57cec5SDimitry Andric   unsigned DstReg, SrcReg, DReg;
51370b57cec5SDimitry Andric   unsigned Lane;
51380b57cec5SDimitry Andric   MachineInstrBuilder MIB(*MI.getParent()->getParent(), MI);
51390b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = &getRegisterInfo();
51400b57cec5SDimitry Andric   switch (MI.getOpcode()) {
51410b57cec5SDimitry Andric   default:
51420b57cec5SDimitry Andric     llvm_unreachable("cannot handle opcode!");
51430b57cec5SDimitry Andric     break;
51440b57cec5SDimitry Andric   case ARM::VMOVD:
51450b57cec5SDimitry Andric     if (Domain != ExeNEON)
51460b57cec5SDimitry Andric       break;
51470b57cec5SDimitry Andric 
51480b57cec5SDimitry Andric     // Zap the predicate operands.
51490b57cec5SDimitry Andric     assert(!isPredicated(MI) && "Cannot predicate a VORRd");
51500b57cec5SDimitry Andric 
51510b57cec5SDimitry Andric     // Make sure we've got NEON instructions.
51520b57cec5SDimitry Andric     assert(Subtarget.hasNEON() && "VORRd requires NEON");
51530b57cec5SDimitry Andric 
51540b57cec5SDimitry Andric     // Source instruction is %DDst = VMOVD %DSrc, 14, %noreg (; implicits)
51550b57cec5SDimitry Andric     DstReg = MI.getOperand(0).getReg();
51560b57cec5SDimitry Andric     SrcReg = MI.getOperand(1).getReg();
51570b57cec5SDimitry Andric 
51580b57cec5SDimitry Andric     for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
515981ad6265SDimitry Andric       MI.removeOperand(i - 1);
51600b57cec5SDimitry Andric 
51610b57cec5SDimitry Andric     // Change to a %DDst = VORRd %DSrc, %DSrc, 14, %noreg (; implicits)
51620b57cec5SDimitry Andric     MI.setDesc(get(ARM::VORRd));
51630b57cec5SDimitry Andric     MIB.addReg(DstReg, RegState::Define)
51640b57cec5SDimitry Andric         .addReg(SrcReg)
51650b57cec5SDimitry Andric         .addReg(SrcReg)
51660b57cec5SDimitry Andric         .add(predOps(ARMCC::AL));
51670b57cec5SDimitry Andric     break;
51680b57cec5SDimitry Andric   case ARM::VMOVRS:
51690b57cec5SDimitry Andric     if (Domain != ExeNEON)
51700b57cec5SDimitry Andric       break;
51710b57cec5SDimitry Andric     assert(!isPredicated(MI) && "Cannot predicate a VGETLN");
51720b57cec5SDimitry Andric 
51730b57cec5SDimitry Andric     // Source instruction is %RDst = VMOVRS %SSrc, 14, %noreg (; implicits)
51740b57cec5SDimitry Andric     DstReg = MI.getOperand(0).getReg();
51750b57cec5SDimitry Andric     SrcReg = MI.getOperand(1).getReg();
51760b57cec5SDimitry Andric 
51770b57cec5SDimitry Andric     for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
517881ad6265SDimitry Andric       MI.removeOperand(i - 1);
51790b57cec5SDimitry Andric 
51800b57cec5SDimitry Andric     DReg = getCorrespondingDRegAndLane(TRI, SrcReg, Lane);
51810b57cec5SDimitry Andric 
51820b57cec5SDimitry Andric     // Convert to %RDst = VGETLNi32 %DSrc, Lane, 14, %noreg (; imps)
51830b57cec5SDimitry Andric     // Note that DSrc has been widened and the other lane may be undef, which
51840b57cec5SDimitry Andric     // contaminates the entire register.
51850b57cec5SDimitry Andric     MI.setDesc(get(ARM::VGETLNi32));
51860b57cec5SDimitry Andric     MIB.addReg(DstReg, RegState::Define)
51870b57cec5SDimitry Andric         .addReg(DReg, RegState::Undef)
51880b57cec5SDimitry Andric         .addImm(Lane)
51890b57cec5SDimitry Andric         .add(predOps(ARMCC::AL));
51900b57cec5SDimitry Andric 
51910b57cec5SDimitry Andric     // The old source should be an implicit use, otherwise we might think it
51920b57cec5SDimitry Andric     // was dead before here.
51930b57cec5SDimitry Andric     MIB.addReg(SrcReg, RegState::Implicit);
51940b57cec5SDimitry Andric     break;
51950b57cec5SDimitry Andric   case ARM::VMOVSR: {
51960b57cec5SDimitry Andric     if (Domain != ExeNEON)
51970b57cec5SDimitry Andric       break;
51980b57cec5SDimitry Andric     assert(!isPredicated(MI) && "Cannot predicate a VSETLN");
51990b57cec5SDimitry Andric 
52000b57cec5SDimitry Andric     // Source instruction is %SDst = VMOVSR %RSrc, 14, %noreg (; implicits)
52010b57cec5SDimitry Andric     DstReg = MI.getOperand(0).getReg();
52020b57cec5SDimitry Andric     SrcReg = MI.getOperand(1).getReg();
52030b57cec5SDimitry Andric 
52040b57cec5SDimitry Andric     DReg = getCorrespondingDRegAndLane(TRI, DstReg, Lane);
52050b57cec5SDimitry Andric 
52060b57cec5SDimitry Andric     unsigned ImplicitSReg;
52070b57cec5SDimitry Andric     if (!getImplicitSPRUseForDPRUse(TRI, MI, DReg, Lane, ImplicitSReg))
52080b57cec5SDimitry Andric       break;
52090b57cec5SDimitry Andric 
52100b57cec5SDimitry Andric     for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
521181ad6265SDimitry Andric       MI.removeOperand(i - 1);
52120b57cec5SDimitry Andric 
52130b57cec5SDimitry Andric     // Convert to %DDst = VSETLNi32 %DDst, %RSrc, Lane, 14, %noreg (; imps)
52140b57cec5SDimitry Andric     // Again DDst may be undefined at the beginning of this instruction.
52150b57cec5SDimitry Andric     MI.setDesc(get(ARM::VSETLNi32));
52160b57cec5SDimitry Andric     MIB.addReg(DReg, RegState::Define)
52170b57cec5SDimitry Andric         .addReg(DReg, getUndefRegState(!MI.readsRegister(DReg, TRI)))
52180b57cec5SDimitry Andric         .addReg(SrcReg)
52190b57cec5SDimitry Andric         .addImm(Lane)
52200b57cec5SDimitry Andric         .add(predOps(ARMCC::AL));
52210b57cec5SDimitry Andric 
52220b57cec5SDimitry Andric     // The narrower destination must be marked as set to keep previous chains
52230b57cec5SDimitry Andric     // in place.
52240b57cec5SDimitry Andric     MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
52250b57cec5SDimitry Andric     if (ImplicitSReg != 0)
52260b57cec5SDimitry Andric       MIB.addReg(ImplicitSReg, RegState::Implicit);
52270b57cec5SDimitry Andric     break;
52280b57cec5SDimitry Andric     }
52290b57cec5SDimitry Andric     case ARM::VMOVS: {
52300b57cec5SDimitry Andric       if (Domain != ExeNEON)
52310b57cec5SDimitry Andric         break;
52320b57cec5SDimitry Andric 
52330b57cec5SDimitry Andric       // Source instruction is %SDst = VMOVS %SSrc, 14, %noreg (; implicits)
52340b57cec5SDimitry Andric       DstReg = MI.getOperand(0).getReg();
52350b57cec5SDimitry Andric       SrcReg = MI.getOperand(1).getReg();
52360b57cec5SDimitry Andric 
52370b57cec5SDimitry Andric       unsigned DstLane = 0, SrcLane = 0, DDst, DSrc;
52380b57cec5SDimitry Andric       DDst = getCorrespondingDRegAndLane(TRI, DstReg, DstLane);
52390b57cec5SDimitry Andric       DSrc = getCorrespondingDRegAndLane(TRI, SrcReg, SrcLane);
52400b57cec5SDimitry Andric 
52410b57cec5SDimitry Andric       unsigned ImplicitSReg;
52420b57cec5SDimitry Andric       if (!getImplicitSPRUseForDPRUse(TRI, MI, DSrc, SrcLane, ImplicitSReg))
52430b57cec5SDimitry Andric         break;
52440b57cec5SDimitry Andric 
52450b57cec5SDimitry Andric       for (unsigned i = MI.getDesc().getNumOperands(); i; --i)
524681ad6265SDimitry Andric         MI.removeOperand(i - 1);
52470b57cec5SDimitry Andric 
52480b57cec5SDimitry Andric       if (DSrc == DDst) {
52490b57cec5SDimitry Andric         // Destination can be:
52500b57cec5SDimitry Andric         //     %DDst = VDUPLN32d %DDst, Lane, 14, %noreg (; implicits)
52510b57cec5SDimitry Andric         MI.setDesc(get(ARM::VDUPLN32d));
52520b57cec5SDimitry Andric         MIB.addReg(DDst, RegState::Define)
52530b57cec5SDimitry Andric             .addReg(DDst, getUndefRegState(!MI.readsRegister(DDst, TRI)))
52540b57cec5SDimitry Andric             .addImm(SrcLane)
52550b57cec5SDimitry Andric             .add(predOps(ARMCC::AL));
52560b57cec5SDimitry Andric 
52570b57cec5SDimitry Andric         // Neither the source or the destination are naturally represented any
52580b57cec5SDimitry Andric         // more, so add them in manually.
52590b57cec5SDimitry Andric         MIB.addReg(DstReg, RegState::Implicit | RegState::Define);
52600b57cec5SDimitry Andric         MIB.addReg(SrcReg, RegState::Implicit);
52610b57cec5SDimitry Andric         if (ImplicitSReg != 0)
52620b57cec5SDimitry Andric           MIB.addReg(ImplicitSReg, RegState::Implicit);
52630b57cec5SDimitry Andric         break;
52640b57cec5SDimitry Andric       }
52650b57cec5SDimitry Andric 
52660b57cec5SDimitry Andric       // In general there's no single instruction that can perform an S <-> S
52670b57cec5SDimitry Andric       // move in NEON space, but a pair of VEXT instructions *can* do the
52680b57cec5SDimitry Andric       // job. It turns out that the VEXTs needed will only use DSrc once, with
52690b57cec5SDimitry Andric       // the position based purely on the combination of lane-0 and lane-1
52700b57cec5SDimitry Andric       // involved. For example
52710b57cec5SDimitry Andric       //     vmov s0, s2 -> vext.32 d0, d0, d1, #1  vext.32 d0, d0, d0, #1
52720b57cec5SDimitry Andric       //     vmov s1, s3 -> vext.32 d0, d1, d0, #1  vext.32 d0, d0, d0, #1
52730b57cec5SDimitry Andric       //     vmov s0, s3 -> vext.32 d0, d0, d0, #1  vext.32 d0, d1, d0, #1
52740b57cec5SDimitry Andric       //     vmov s1, s2 -> vext.32 d0, d0, d0, #1  vext.32 d0, d0, d1, #1
52750b57cec5SDimitry Andric       //
52760b57cec5SDimitry Andric       // Pattern of the MachineInstrs is:
52770b57cec5SDimitry Andric       //     %DDst = VEXTd32 %DSrc1, %DSrc2, Lane, 14, %noreg (;implicits)
52780b57cec5SDimitry Andric       MachineInstrBuilder NewMIB;
52790b57cec5SDimitry Andric       NewMIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::VEXTd32),
52800b57cec5SDimitry Andric                        DDst);
52810b57cec5SDimitry Andric 
52820b57cec5SDimitry Andric       // On the first instruction, both DSrc and DDst may be undef if present.
52830b57cec5SDimitry Andric       // Specifically when the original instruction didn't have them as an
52840b57cec5SDimitry Andric       // <imp-use>.
52850b57cec5SDimitry Andric       unsigned CurReg = SrcLane == 1 && DstLane == 1 ? DSrc : DDst;
52860b57cec5SDimitry Andric       bool CurUndef = !MI.readsRegister(CurReg, TRI);
52870b57cec5SDimitry Andric       NewMIB.addReg(CurReg, getUndefRegState(CurUndef));
52880b57cec5SDimitry Andric 
52890b57cec5SDimitry Andric       CurReg = SrcLane == 0 && DstLane == 0 ? DSrc : DDst;
52900b57cec5SDimitry Andric       CurUndef = !MI.readsRegister(CurReg, TRI);
52910b57cec5SDimitry Andric       NewMIB.addReg(CurReg, getUndefRegState(CurUndef))
52920b57cec5SDimitry Andric             .addImm(1)
52930b57cec5SDimitry Andric             .add(predOps(ARMCC::AL));
52940b57cec5SDimitry Andric 
52950b57cec5SDimitry Andric       if (SrcLane == DstLane)
52960b57cec5SDimitry Andric         NewMIB.addReg(SrcReg, RegState::Implicit);
52970b57cec5SDimitry Andric 
52980b57cec5SDimitry Andric       MI.setDesc(get(ARM::VEXTd32));
52990b57cec5SDimitry Andric       MIB.addReg(DDst, RegState::Define);
53000b57cec5SDimitry Andric 
53010b57cec5SDimitry Andric       // On the second instruction, DDst has definitely been defined above, so
53020b57cec5SDimitry Andric       // it is not undef. DSrc, if present, can be undef as above.
53030b57cec5SDimitry Andric       CurReg = SrcLane == 1 && DstLane == 0 ? DSrc : DDst;
53040b57cec5SDimitry Andric       CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
53050b57cec5SDimitry Andric       MIB.addReg(CurReg, getUndefRegState(CurUndef));
53060b57cec5SDimitry Andric 
53070b57cec5SDimitry Andric       CurReg = SrcLane == 0 && DstLane == 1 ? DSrc : DDst;
53080b57cec5SDimitry Andric       CurUndef = CurReg == DSrc && !MI.readsRegister(CurReg, TRI);
53090b57cec5SDimitry Andric       MIB.addReg(CurReg, getUndefRegState(CurUndef))
53100b57cec5SDimitry Andric          .addImm(1)
53110b57cec5SDimitry Andric          .add(predOps(ARMCC::AL));
53120b57cec5SDimitry Andric 
53130b57cec5SDimitry Andric       if (SrcLane != DstLane)
53140b57cec5SDimitry Andric         MIB.addReg(SrcReg, RegState::Implicit);
53150b57cec5SDimitry Andric 
53160b57cec5SDimitry Andric       // As before, the original destination is no longer represented, add it
53170b57cec5SDimitry Andric       // implicitly.
53180b57cec5SDimitry Andric       MIB.addReg(DstReg, RegState::Define | RegState::Implicit);
53190b57cec5SDimitry Andric       if (ImplicitSReg != 0)
53200b57cec5SDimitry Andric         MIB.addReg(ImplicitSReg, RegState::Implicit);
53210b57cec5SDimitry Andric       break;
53220b57cec5SDimitry Andric     }
53230b57cec5SDimitry Andric   }
53240b57cec5SDimitry Andric }
53250b57cec5SDimitry Andric 
53260b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
53270b57cec5SDimitry Andric // Partial register updates
53280b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
53290b57cec5SDimitry Andric //
53300b57cec5SDimitry Andric // Swift renames NEON registers with 64-bit granularity.  That means any
53310b57cec5SDimitry Andric // instruction writing an S-reg implicitly reads the containing D-reg.  The
53320b57cec5SDimitry Andric // problem is mostly avoided by translating f32 operations to v2f32 operations
53330b57cec5SDimitry Andric // on D-registers, but f32 loads are still a problem.
53340b57cec5SDimitry Andric //
53350b57cec5SDimitry Andric // These instructions can load an f32 into a NEON register:
53360b57cec5SDimitry Andric //
53370b57cec5SDimitry Andric // VLDRS - Only writes S, partial D update.
53380b57cec5SDimitry Andric // VLD1LNd32 - Writes all D-regs, explicit partial D update, 2 uops.
53390b57cec5SDimitry Andric // VLD1DUPd32 - Writes all D-regs, no partial reg update, 2 uops.
53400b57cec5SDimitry Andric //
53410b57cec5SDimitry Andric // FCONSTD can be used as a dependency-breaking instruction.
53420b57cec5SDimitry Andric unsigned ARMBaseInstrInfo::getPartialRegUpdateClearance(
53430b57cec5SDimitry Andric     const MachineInstr &MI, unsigned OpNum,
53440b57cec5SDimitry Andric     const TargetRegisterInfo *TRI) const {
53450b57cec5SDimitry Andric   auto PartialUpdateClearance = Subtarget.getPartialUpdateClearance();
53460b57cec5SDimitry Andric   if (!PartialUpdateClearance)
53470b57cec5SDimitry Andric     return 0;
53480b57cec5SDimitry Andric 
53490b57cec5SDimitry Andric   assert(TRI && "Need TRI instance");
53500b57cec5SDimitry Andric 
53510b57cec5SDimitry Andric   const MachineOperand &MO = MI.getOperand(OpNum);
53520b57cec5SDimitry Andric   if (MO.readsReg())
53530b57cec5SDimitry Andric     return 0;
53548bcb0991SDimitry Andric   Register Reg = MO.getReg();
53550b57cec5SDimitry Andric   int UseOp = -1;
53560b57cec5SDimitry Andric 
53570b57cec5SDimitry Andric   switch (MI.getOpcode()) {
53580b57cec5SDimitry Andric   // Normal instructions writing only an S-register.
53590b57cec5SDimitry Andric   case ARM::VLDRS:
53600b57cec5SDimitry Andric   case ARM::FCONSTS:
53610b57cec5SDimitry Andric   case ARM::VMOVSR:
53620b57cec5SDimitry Andric   case ARM::VMOVv8i8:
53630b57cec5SDimitry Andric   case ARM::VMOVv4i16:
53640b57cec5SDimitry Andric   case ARM::VMOVv2i32:
53650b57cec5SDimitry Andric   case ARM::VMOVv2f32:
53660b57cec5SDimitry Andric   case ARM::VMOVv1i64:
53670b57cec5SDimitry Andric     UseOp = MI.findRegisterUseOperandIdx(Reg, false, TRI);
53680b57cec5SDimitry Andric     break;
53690b57cec5SDimitry Andric 
53700b57cec5SDimitry Andric     // Explicitly reads the dependency.
53710b57cec5SDimitry Andric   case ARM::VLD1LNd32:
53720b57cec5SDimitry Andric     UseOp = 3;
53730b57cec5SDimitry Andric     break;
53740b57cec5SDimitry Andric   default:
53750b57cec5SDimitry Andric     return 0;
53760b57cec5SDimitry Andric   }
53770b57cec5SDimitry Andric 
53780b57cec5SDimitry Andric   // If this instruction actually reads a value from Reg, there is no unwanted
53790b57cec5SDimitry Andric   // dependency.
53800b57cec5SDimitry Andric   if (UseOp != -1 && MI.getOperand(UseOp).readsReg())
53810b57cec5SDimitry Andric     return 0;
53820b57cec5SDimitry Andric 
53830b57cec5SDimitry Andric   // We must be able to clobber the whole D-reg.
5384bdd1243dSDimitry Andric   if (Reg.isVirtual()) {
53850b57cec5SDimitry Andric     // Virtual register must be a def undef foo:ssub_0 operand.
53860b57cec5SDimitry Andric     if (!MO.getSubReg() || MI.readsVirtualRegister(Reg))
53870b57cec5SDimitry Andric       return 0;
53880b57cec5SDimitry Andric   } else if (ARM::SPRRegClass.contains(Reg)) {
53890b57cec5SDimitry Andric     // Physical register: MI must define the full D-reg.
53900b57cec5SDimitry Andric     unsigned DReg = TRI->getMatchingSuperReg(Reg, ARM::ssub_0,
53910b57cec5SDimitry Andric                                              &ARM::DPRRegClass);
53920b57cec5SDimitry Andric     if (!DReg || !MI.definesRegister(DReg, TRI))
53930b57cec5SDimitry Andric       return 0;
53940b57cec5SDimitry Andric   }
53950b57cec5SDimitry Andric 
53960b57cec5SDimitry Andric   // MI has an unwanted D-register dependency.
53970b57cec5SDimitry Andric   // Avoid defs in the previous N instructrions.
53980b57cec5SDimitry Andric   return PartialUpdateClearance;
53990b57cec5SDimitry Andric }
54000b57cec5SDimitry Andric 
54010b57cec5SDimitry Andric // Break a partial register dependency after getPartialRegUpdateClearance
54020b57cec5SDimitry Andric // returned non-zero.
54030b57cec5SDimitry Andric void ARMBaseInstrInfo::breakPartialRegDependency(
54040b57cec5SDimitry Andric     MachineInstr &MI, unsigned OpNum, const TargetRegisterInfo *TRI) const {
54050b57cec5SDimitry Andric   assert(OpNum < MI.getDesc().getNumDefs() && "OpNum is not a def");
54060b57cec5SDimitry Andric   assert(TRI && "Need TRI instance");
54070b57cec5SDimitry Andric 
54080b57cec5SDimitry Andric   const MachineOperand &MO = MI.getOperand(OpNum);
54098bcb0991SDimitry Andric   Register Reg = MO.getReg();
5410bdd1243dSDimitry Andric   assert(Reg.isPhysical() && "Can't break virtual register dependencies.");
54110b57cec5SDimitry Andric   unsigned DReg = Reg;
54120b57cec5SDimitry Andric 
54130b57cec5SDimitry Andric   // If MI defines an S-reg, find the corresponding D super-register.
54140b57cec5SDimitry Andric   if (ARM::SPRRegClass.contains(Reg)) {
54150b57cec5SDimitry Andric     DReg = ARM::D0 + (Reg - ARM::S0) / 2;
54160b57cec5SDimitry Andric     assert(TRI->isSuperRegister(Reg, DReg) && "Register enums broken");
54170b57cec5SDimitry Andric   }
54180b57cec5SDimitry Andric 
54190b57cec5SDimitry Andric   assert(ARM::DPRRegClass.contains(DReg) && "Can only break D-reg deps");
54200b57cec5SDimitry Andric   assert(MI.definesRegister(DReg, TRI) && "MI doesn't clobber full D-reg");
54210b57cec5SDimitry Andric 
54220b57cec5SDimitry Andric   // FIXME: In some cases, VLDRS can be changed to a VLD1DUPd32 which defines
54230b57cec5SDimitry Andric   // the full D-register by loading the same value to both lanes.  The
54240b57cec5SDimitry Andric   // instruction is micro-coded with 2 uops, so don't do this until we can
54250b57cec5SDimitry Andric   // properly schedule micro-coded instructions.  The dispatcher stalls cause
54260b57cec5SDimitry Andric   // too big regressions.
54270b57cec5SDimitry Andric 
54280b57cec5SDimitry Andric   // Insert the dependency-breaking FCONSTD before MI.
54290b57cec5SDimitry Andric   // 96 is the encoding of 0.5, but the actual value doesn't matter here.
54300b57cec5SDimitry Andric   BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), get(ARM::FCONSTD), DReg)
54310b57cec5SDimitry Andric       .addImm(96)
54320b57cec5SDimitry Andric       .add(predOps(ARMCC::AL));
54330b57cec5SDimitry Andric   MI.addRegisterKilled(DReg, TRI, true);
54340b57cec5SDimitry Andric }
54350b57cec5SDimitry Andric 
54360b57cec5SDimitry Andric bool ARMBaseInstrInfo::hasNOP() const {
543706c3fb27SDimitry Andric   return Subtarget.hasFeature(ARM::HasV6KOps);
54380b57cec5SDimitry Andric }
54390b57cec5SDimitry Andric 
54400b57cec5SDimitry Andric bool ARMBaseInstrInfo::isSwiftFastImmShift(const MachineInstr *MI) const {
54410b57cec5SDimitry Andric   if (MI->getNumOperands() < 4)
54420b57cec5SDimitry Andric     return true;
54430b57cec5SDimitry Andric   unsigned ShOpVal = MI->getOperand(3).getImm();
54440b57cec5SDimitry Andric   unsigned ShImm = ARM_AM::getSORegOffset(ShOpVal);
54450b57cec5SDimitry Andric   // Swift supports faster shifts for: lsl 2, lsl 1, and lsr 1.
54460b57cec5SDimitry Andric   if ((ShImm == 1 && ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsr) ||
54470b57cec5SDimitry Andric       ((ShImm == 1 || ShImm == 2) &&
54480b57cec5SDimitry Andric        ARM_AM::getSORegShOp(ShOpVal) == ARM_AM::lsl))
54490b57cec5SDimitry Andric     return true;
54500b57cec5SDimitry Andric 
54510b57cec5SDimitry Andric   return false;
54520b57cec5SDimitry Andric }
54530b57cec5SDimitry Andric 
54540b57cec5SDimitry Andric bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
54550b57cec5SDimitry Andric     const MachineInstr &MI, unsigned DefIdx,
54560b57cec5SDimitry Andric     SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
54570b57cec5SDimitry Andric   assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
54580b57cec5SDimitry Andric   assert(MI.isRegSequenceLike() && "Invalid kind of instruction");
54590b57cec5SDimitry Andric 
54600b57cec5SDimitry Andric   switch (MI.getOpcode()) {
54610b57cec5SDimitry Andric   case ARM::VMOVDRR:
54620b57cec5SDimitry Andric     // dX = VMOVDRR rY, rZ
54630b57cec5SDimitry Andric     // is the same as:
54640b57cec5SDimitry Andric     // dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1
54650b57cec5SDimitry Andric     // Populate the InputRegs accordingly.
54660b57cec5SDimitry Andric     // rY
54670b57cec5SDimitry Andric     const MachineOperand *MOReg = &MI.getOperand(1);
54680b57cec5SDimitry Andric     if (!MOReg->isUndef())
54690b57cec5SDimitry Andric       InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
54700b57cec5SDimitry Andric                                               MOReg->getSubReg(), ARM::ssub_0));
54710b57cec5SDimitry Andric     // rZ
54720b57cec5SDimitry Andric     MOReg = &MI.getOperand(2);
54730b57cec5SDimitry Andric     if (!MOReg->isUndef())
54740b57cec5SDimitry Andric       InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
54750b57cec5SDimitry Andric                                               MOReg->getSubReg(), ARM::ssub_1));
54760b57cec5SDimitry Andric     return true;
54770b57cec5SDimitry Andric   }
54780b57cec5SDimitry Andric   llvm_unreachable("Target dependent opcode missing");
54790b57cec5SDimitry Andric }
54800b57cec5SDimitry Andric 
54810b57cec5SDimitry Andric bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
54820b57cec5SDimitry Andric     const MachineInstr &MI, unsigned DefIdx,
54830b57cec5SDimitry Andric     RegSubRegPairAndIdx &InputReg) const {
54840b57cec5SDimitry Andric   assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
54850b57cec5SDimitry Andric   assert(MI.isExtractSubregLike() && "Invalid kind of instruction");
54860b57cec5SDimitry Andric 
54870b57cec5SDimitry Andric   switch (MI.getOpcode()) {
54880b57cec5SDimitry Andric   case ARM::VMOVRRD:
54890b57cec5SDimitry Andric     // rX, rY = VMOVRRD dZ
54900b57cec5SDimitry Andric     // is the same as:
54910b57cec5SDimitry Andric     // rX = EXTRACT_SUBREG dZ, ssub_0
54920b57cec5SDimitry Andric     // rY = EXTRACT_SUBREG dZ, ssub_1
54930b57cec5SDimitry Andric     const MachineOperand &MOReg = MI.getOperand(2);
54940b57cec5SDimitry Andric     if (MOReg.isUndef())
54950b57cec5SDimitry Andric       return false;
54960b57cec5SDimitry Andric     InputReg.Reg = MOReg.getReg();
54970b57cec5SDimitry Andric     InputReg.SubReg = MOReg.getSubReg();
54980b57cec5SDimitry Andric     InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
54990b57cec5SDimitry Andric     return true;
55000b57cec5SDimitry Andric   }
55010b57cec5SDimitry Andric   llvm_unreachable("Target dependent opcode missing");
55020b57cec5SDimitry Andric }
55030b57cec5SDimitry Andric 
55040b57cec5SDimitry Andric bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
55050b57cec5SDimitry Andric     const MachineInstr &MI, unsigned DefIdx, RegSubRegPair &BaseReg,
55060b57cec5SDimitry Andric     RegSubRegPairAndIdx &InsertedReg) const {
55070b57cec5SDimitry Andric   assert(DefIdx < MI.getDesc().getNumDefs() && "Invalid definition index");
55080b57cec5SDimitry Andric   assert(MI.isInsertSubregLike() && "Invalid kind of instruction");
55090b57cec5SDimitry Andric 
55100b57cec5SDimitry Andric   switch (MI.getOpcode()) {
55110b57cec5SDimitry Andric   case ARM::VSETLNi32:
5512fe6060f1SDimitry Andric   case ARM::MVE_VMOV_to_lane_32:
55130b57cec5SDimitry Andric     // dX = VSETLNi32 dY, rZ, imm
5514fe6060f1SDimitry Andric     // qX = MVE_VMOV_to_lane_32 qY, rZ, imm
55150b57cec5SDimitry Andric     const MachineOperand &MOBaseReg = MI.getOperand(1);
55160b57cec5SDimitry Andric     const MachineOperand &MOInsertedReg = MI.getOperand(2);
55170b57cec5SDimitry Andric     if (MOInsertedReg.isUndef())
55180b57cec5SDimitry Andric       return false;
55190b57cec5SDimitry Andric     const MachineOperand &MOIndex = MI.getOperand(3);
55200b57cec5SDimitry Andric     BaseReg.Reg = MOBaseReg.getReg();
55210b57cec5SDimitry Andric     BaseReg.SubReg = MOBaseReg.getSubReg();
55220b57cec5SDimitry Andric 
55230b57cec5SDimitry Andric     InsertedReg.Reg = MOInsertedReg.getReg();
55240b57cec5SDimitry Andric     InsertedReg.SubReg = MOInsertedReg.getSubReg();
5525fe6060f1SDimitry Andric     InsertedReg.SubIdx = ARM::ssub_0 + MOIndex.getImm();
55260b57cec5SDimitry Andric     return true;
55270b57cec5SDimitry Andric   }
55280b57cec5SDimitry Andric   llvm_unreachable("Target dependent opcode missing");
55290b57cec5SDimitry Andric }
55300b57cec5SDimitry Andric 
55310b57cec5SDimitry Andric std::pair<unsigned, unsigned>
55320b57cec5SDimitry Andric ARMBaseInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
55330b57cec5SDimitry Andric   const unsigned Mask = ARMII::MO_OPTION_MASK;
55340b57cec5SDimitry Andric   return std::make_pair(TF & Mask, TF & ~Mask);
55350b57cec5SDimitry Andric }
55360b57cec5SDimitry Andric 
55370b57cec5SDimitry Andric ArrayRef<std::pair<unsigned, const char *>>
55380b57cec5SDimitry Andric ARMBaseInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
55390b57cec5SDimitry Andric   using namespace ARMII;
55400b57cec5SDimitry Andric 
55410b57cec5SDimitry Andric   static const std::pair<unsigned, const char *> TargetFlags[] = {
554206c3fb27SDimitry Andric       {MO_LO16, "arm-lo16"},       {MO_HI16, "arm-hi16"},
554306c3fb27SDimitry Andric       {MO_LO_0_7, "arm-lo-0-7"},   {MO_HI_0_7, "arm-hi-0-7"},
554406c3fb27SDimitry Andric       {MO_LO_8_15, "arm-lo-8-15"}, {MO_HI_8_15, "arm-hi-8-15"},
554506c3fb27SDimitry Andric   };
5546bdd1243dSDimitry Andric   return ArrayRef(TargetFlags);
55470b57cec5SDimitry Andric }
55480b57cec5SDimitry Andric 
55490b57cec5SDimitry Andric ArrayRef<std::pair<unsigned, const char *>>
55500b57cec5SDimitry Andric ARMBaseInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
55510b57cec5SDimitry Andric   using namespace ARMII;
55520b57cec5SDimitry Andric 
55530b57cec5SDimitry Andric   static const std::pair<unsigned, const char *> TargetFlags[] = {
55540b57cec5SDimitry Andric       {MO_COFFSTUB, "arm-coffstub"},
55550b57cec5SDimitry Andric       {MO_GOT, "arm-got"},
55560b57cec5SDimitry Andric       {MO_SBREL, "arm-sbrel"},
55570b57cec5SDimitry Andric       {MO_DLLIMPORT, "arm-dllimport"},
55580b57cec5SDimitry Andric       {MO_SECREL, "arm-secrel"},
55590b57cec5SDimitry Andric       {MO_NONLAZY, "arm-nonlazy"}};
5560bdd1243dSDimitry Andric   return ArrayRef(TargetFlags);
55610b57cec5SDimitry Andric }
55620b57cec5SDimitry Andric 
5563bdd1243dSDimitry Andric std::optional<RegImmPair>
5564bdd1243dSDimitry Andric ARMBaseInstrInfo::isAddImmediate(const MachineInstr &MI, Register Reg) const {
5565480093f4SDimitry Andric   int Sign = 1;
5566480093f4SDimitry Andric   unsigned Opcode = MI.getOpcode();
5567480093f4SDimitry Andric   int64_t Offset = 0;
5568480093f4SDimitry Andric 
5569480093f4SDimitry Andric   // TODO: Handle cases where Reg is a super- or sub-register of the
5570480093f4SDimitry Andric   // destination register.
55715ffd83dbSDimitry Andric   const MachineOperand &Op0 = MI.getOperand(0);
55725ffd83dbSDimitry Andric   if (!Op0.isReg() || Reg != Op0.getReg())
5573bdd1243dSDimitry Andric     return std::nullopt;
5574480093f4SDimitry Andric 
5575480093f4SDimitry Andric   // We describe SUBri or ADDri instructions.
5576480093f4SDimitry Andric   if (Opcode == ARM::SUBri)
5577480093f4SDimitry Andric     Sign = -1;
5578480093f4SDimitry Andric   else if (Opcode != ARM::ADDri)
5579bdd1243dSDimitry Andric     return std::nullopt;
5580480093f4SDimitry Andric 
5581480093f4SDimitry Andric   // TODO: Third operand can be global address (usually some string). Since
5582480093f4SDimitry Andric   //       strings can be relocated we cannot calculate their offsets for
5583480093f4SDimitry Andric   //       now.
55845ffd83dbSDimitry Andric   if (!MI.getOperand(1).isReg() || !MI.getOperand(2).isImm())
5585bdd1243dSDimitry Andric     return std::nullopt;
5586480093f4SDimitry Andric 
5587480093f4SDimitry Andric   Offset = MI.getOperand(2).getImm() * Sign;
5588480093f4SDimitry Andric   return RegImmPair{MI.getOperand(1).getReg(), Offset};
5589480093f4SDimitry Andric }
5590480093f4SDimitry Andric 
55910b57cec5SDimitry Andric bool llvm::registerDefinedBetween(unsigned Reg,
55920b57cec5SDimitry Andric                                   MachineBasicBlock::iterator From,
55930b57cec5SDimitry Andric                                   MachineBasicBlock::iterator To,
55940b57cec5SDimitry Andric                                   const TargetRegisterInfo *TRI) {
55950b57cec5SDimitry Andric   for (auto I = From; I != To; ++I)
55960b57cec5SDimitry Andric     if (I->modifiesRegister(Reg, TRI))
55970b57cec5SDimitry Andric       return true;
55980b57cec5SDimitry Andric   return false;
55990b57cec5SDimitry Andric }
56000b57cec5SDimitry Andric 
56010b57cec5SDimitry Andric MachineInstr *llvm::findCMPToFoldIntoCBZ(MachineInstr *Br,
56020b57cec5SDimitry Andric                                          const TargetRegisterInfo *TRI) {
56030b57cec5SDimitry Andric   // Search backwards to the instruction that defines CSPR. This may or not
56040b57cec5SDimitry Andric   // be a CMP, we check that after this loop. If we find another instruction
56050b57cec5SDimitry Andric   // that reads cpsr, we return nullptr.
56060b57cec5SDimitry Andric   MachineBasicBlock::iterator CmpMI = Br;
56070b57cec5SDimitry Andric   while (CmpMI != Br->getParent()->begin()) {
56080b57cec5SDimitry Andric     --CmpMI;
56090b57cec5SDimitry Andric     if (CmpMI->modifiesRegister(ARM::CPSR, TRI))
56100b57cec5SDimitry Andric       break;
56110b57cec5SDimitry Andric     if (CmpMI->readsRegister(ARM::CPSR, TRI))
56120b57cec5SDimitry Andric       break;
56130b57cec5SDimitry Andric   }
56140b57cec5SDimitry Andric 
56150b57cec5SDimitry Andric   // Check that this inst is a CMP r[0-7], #0 and that the register
56160b57cec5SDimitry Andric   // is not redefined between the cmp and the br.
56170b57cec5SDimitry Andric   if (CmpMI->getOpcode() != ARM::tCMPi8 && CmpMI->getOpcode() != ARM::t2CMPri)
56180b57cec5SDimitry Andric     return nullptr;
56198bcb0991SDimitry Andric   Register Reg = CmpMI->getOperand(0).getReg();
56205ffd83dbSDimitry Andric   Register PredReg;
56210b57cec5SDimitry Andric   ARMCC::CondCodes Pred = getInstrPredicate(*CmpMI, PredReg);
56220b57cec5SDimitry Andric   if (Pred != ARMCC::AL || CmpMI->getOperand(1).getImm() != 0)
56230b57cec5SDimitry Andric     return nullptr;
56240b57cec5SDimitry Andric   if (!isARMLowRegister(Reg))
56250b57cec5SDimitry Andric     return nullptr;
56260b57cec5SDimitry Andric   if (registerDefinedBetween(Reg, CmpMI->getNextNode(), Br, TRI))
56270b57cec5SDimitry Andric     return nullptr;
56280b57cec5SDimitry Andric 
56290b57cec5SDimitry Andric   return &*CmpMI;
56300b57cec5SDimitry Andric }
56318bcb0991SDimitry Andric 
56328bcb0991SDimitry Andric unsigned llvm::ConstantMaterializationCost(unsigned Val,
56338bcb0991SDimitry Andric                                            const ARMSubtarget *Subtarget,
56348bcb0991SDimitry Andric                                            bool ForCodesize) {
56358bcb0991SDimitry Andric   if (Subtarget->isThumb()) {
56368bcb0991SDimitry Andric     if (Val <= 255) // MOV
56378bcb0991SDimitry Andric       return ForCodesize ? 2 : 1;
56388bcb0991SDimitry Andric     if (Subtarget->hasV6T2Ops() && (Val <= 0xffff ||                    // MOV
56398bcb0991SDimitry Andric                                     ARM_AM::getT2SOImmVal(Val) != -1 || // MOVW
56408bcb0991SDimitry Andric                                     ARM_AM::getT2SOImmVal(~Val) != -1)) // MVN
56418bcb0991SDimitry Andric       return ForCodesize ? 4 : 1;
56428bcb0991SDimitry Andric     if (Val <= 510) // MOV + ADDi8
56438bcb0991SDimitry Andric       return ForCodesize ? 4 : 2;
56448bcb0991SDimitry Andric     if (~Val <= 255) // MOV + MVN
56458bcb0991SDimitry Andric       return ForCodesize ? 4 : 2;
56468bcb0991SDimitry Andric     if (ARM_AM::isThumbImmShiftedVal(Val)) // MOV + LSL
56478bcb0991SDimitry Andric       return ForCodesize ? 4 : 2;
56488bcb0991SDimitry Andric   } else {
56498bcb0991SDimitry Andric     if (ARM_AM::getSOImmVal(Val) != -1) // MOV
56508bcb0991SDimitry Andric       return ForCodesize ? 4 : 1;
56518bcb0991SDimitry Andric     if (ARM_AM::getSOImmVal(~Val) != -1) // MVN
56528bcb0991SDimitry Andric       return ForCodesize ? 4 : 1;
56538bcb0991SDimitry Andric     if (Subtarget->hasV6T2Ops() && Val <= 0xffff) // MOVW
56548bcb0991SDimitry Andric       return ForCodesize ? 4 : 1;
56558bcb0991SDimitry Andric     if (ARM_AM::isSOImmTwoPartVal(Val)) // two instrs
56568bcb0991SDimitry Andric       return ForCodesize ? 8 : 2;
5657e8d8bef9SDimitry Andric     if (ARM_AM::isSOImmTwoPartValNeg(Val)) // two instrs
5658e8d8bef9SDimitry Andric       return ForCodesize ? 8 : 2;
56598bcb0991SDimitry Andric   }
56608bcb0991SDimitry Andric   if (Subtarget->useMovt()) // MOVW + MOVT
56618bcb0991SDimitry Andric     return ForCodesize ? 8 : 2;
56628bcb0991SDimitry Andric   return ForCodesize ? 8 : 3; // Literal pool load
56638bcb0991SDimitry Andric }
56648bcb0991SDimitry Andric 
56658bcb0991SDimitry Andric bool llvm::HasLowerConstantMaterializationCost(unsigned Val1, unsigned Val2,
56668bcb0991SDimitry Andric                                                const ARMSubtarget *Subtarget,
56678bcb0991SDimitry Andric                                                bool ForCodesize) {
56688bcb0991SDimitry Andric   // Check with ForCodesize
56698bcb0991SDimitry Andric   unsigned Cost1 = ConstantMaterializationCost(Val1, Subtarget, ForCodesize);
56708bcb0991SDimitry Andric   unsigned Cost2 = ConstantMaterializationCost(Val2, Subtarget, ForCodesize);
56718bcb0991SDimitry Andric   if (Cost1 < Cost2)
56728bcb0991SDimitry Andric     return true;
56738bcb0991SDimitry Andric   if (Cost1 > Cost2)
56748bcb0991SDimitry Andric     return false;
56758bcb0991SDimitry Andric 
56768bcb0991SDimitry Andric   // If they are equal, try with !ForCodesize
56778bcb0991SDimitry Andric   return ConstantMaterializationCost(Val1, Subtarget, !ForCodesize) <
56788bcb0991SDimitry Andric          ConstantMaterializationCost(Val2, Subtarget, !ForCodesize);
56798bcb0991SDimitry Andric }
56805ffd83dbSDimitry Andric 
56815ffd83dbSDimitry Andric /// Constants defining how certain sequences should be outlined.
56825ffd83dbSDimitry Andric /// This encompasses how an outlined function should be called, and what kind of
56835ffd83dbSDimitry Andric /// frame should be emitted for that outlined function.
56845ffd83dbSDimitry Andric ///
56855ffd83dbSDimitry Andric /// \p MachineOutlinerTailCall implies that the function is being created from
56865ffd83dbSDimitry Andric /// a sequence of instructions ending in a return.
56875ffd83dbSDimitry Andric ///
56885ffd83dbSDimitry Andric /// That is,
56895ffd83dbSDimitry Andric ///
56905ffd83dbSDimitry Andric /// I1                                OUTLINED_FUNCTION:
56915ffd83dbSDimitry Andric /// I2    --> B OUTLINED_FUNCTION     I1
56925ffd83dbSDimitry Andric /// BX LR                             I2
56935ffd83dbSDimitry Andric ///                                   BX LR
56945ffd83dbSDimitry Andric ///
56955ffd83dbSDimitry Andric /// +-------------------------+--------+-----+
56965ffd83dbSDimitry Andric /// |                         | Thumb2 | ARM |
56975ffd83dbSDimitry Andric /// +-------------------------+--------+-----+
56985ffd83dbSDimitry Andric /// | Call overhead in Bytes  |      4 |   4 |
56995ffd83dbSDimitry Andric /// | Frame overhead in Bytes |      0 |   0 |
57005ffd83dbSDimitry Andric /// | Stack fixup required    |     No |  No |
57015ffd83dbSDimitry Andric /// +-------------------------+--------+-----+
57025ffd83dbSDimitry Andric ///
57035ffd83dbSDimitry Andric /// \p MachineOutlinerThunk implies that the function is being created from
57045ffd83dbSDimitry Andric /// a sequence of instructions ending in a call. The outlined function is
57055ffd83dbSDimitry Andric /// called with a BL instruction, and the outlined function tail-calls the
57065ffd83dbSDimitry Andric /// original call destination.
57075ffd83dbSDimitry Andric ///
57085ffd83dbSDimitry Andric /// That is,
57095ffd83dbSDimitry Andric ///
57105ffd83dbSDimitry Andric /// I1                                OUTLINED_FUNCTION:
57115ffd83dbSDimitry Andric /// I2   --> BL OUTLINED_FUNCTION     I1
57125ffd83dbSDimitry Andric /// BL f                              I2
57135ffd83dbSDimitry Andric ///                                   B f
57145ffd83dbSDimitry Andric ///
57155ffd83dbSDimitry Andric /// +-------------------------+--------+-----+
57165ffd83dbSDimitry Andric /// |                         | Thumb2 | ARM |
57175ffd83dbSDimitry Andric /// +-------------------------+--------+-----+
57185ffd83dbSDimitry Andric /// | Call overhead in Bytes  |      4 |   4 |
57195ffd83dbSDimitry Andric /// | Frame overhead in Bytes |      0 |   0 |
57205ffd83dbSDimitry Andric /// | Stack fixup required    |     No |  No |
57215ffd83dbSDimitry Andric /// +-------------------------+--------+-----+
57225ffd83dbSDimitry Andric ///
57235ffd83dbSDimitry Andric /// \p MachineOutlinerNoLRSave implies that the function should be called using
57245ffd83dbSDimitry Andric /// a BL instruction, but doesn't require LR to be saved and restored. This
57255ffd83dbSDimitry Andric /// happens when LR is known to be dead.
57265ffd83dbSDimitry Andric ///
57275ffd83dbSDimitry Andric /// That is,
57285ffd83dbSDimitry Andric ///
57295ffd83dbSDimitry Andric /// I1                                OUTLINED_FUNCTION:
57305ffd83dbSDimitry Andric /// I2 --> BL OUTLINED_FUNCTION       I1
57315ffd83dbSDimitry Andric /// I3                                I2
57325ffd83dbSDimitry Andric ///                                   I3
57335ffd83dbSDimitry Andric ///                                   BX LR
57345ffd83dbSDimitry Andric ///
57355ffd83dbSDimitry Andric /// +-------------------------+--------+-----+
57365ffd83dbSDimitry Andric /// |                         | Thumb2 | ARM |
57375ffd83dbSDimitry Andric /// +-------------------------+--------+-----+
57385ffd83dbSDimitry Andric /// | Call overhead in Bytes  |      4 |   4 |
57390eae32dcSDimitry Andric /// | Frame overhead in Bytes |      2 |   4 |
57405ffd83dbSDimitry Andric /// | Stack fixup required    |     No |  No |
57415ffd83dbSDimitry Andric /// +-------------------------+--------+-----+
57425ffd83dbSDimitry Andric ///
57435ffd83dbSDimitry Andric /// \p MachineOutlinerRegSave implies that the function should be called with a
57445ffd83dbSDimitry Andric /// save and restore of LR to an available register. This allows us to avoid
57455ffd83dbSDimitry Andric /// stack fixups. Note that this outlining variant is compatible with the
57465ffd83dbSDimitry Andric /// NoLRSave case.
57475ffd83dbSDimitry Andric ///
57485ffd83dbSDimitry Andric /// That is,
57495ffd83dbSDimitry Andric ///
57505ffd83dbSDimitry Andric /// I1     Save LR                    OUTLINED_FUNCTION:
57515ffd83dbSDimitry Andric /// I2 --> BL OUTLINED_FUNCTION       I1
57525ffd83dbSDimitry Andric /// I3     Restore LR                 I2
57535ffd83dbSDimitry Andric ///                                   I3
57545ffd83dbSDimitry Andric ///                                   BX LR
57555ffd83dbSDimitry Andric ///
57565ffd83dbSDimitry Andric /// +-------------------------+--------+-----+
57575ffd83dbSDimitry Andric /// |                         | Thumb2 | ARM |
57585ffd83dbSDimitry Andric /// +-------------------------+--------+-----+
57595ffd83dbSDimitry Andric /// | Call overhead in Bytes  |      8 |  12 |
57605ffd83dbSDimitry Andric /// | Frame overhead in Bytes |      2 |   4 |
57615ffd83dbSDimitry Andric /// | Stack fixup required    |     No |  No |
57625ffd83dbSDimitry Andric /// +-------------------------+--------+-----+
5763e8d8bef9SDimitry Andric ///
5764e8d8bef9SDimitry Andric /// \p MachineOutlinerDefault implies that the function should be called with
5765e8d8bef9SDimitry Andric /// a save and restore of LR to the stack.
5766e8d8bef9SDimitry Andric ///
5767e8d8bef9SDimitry Andric /// That is,
5768e8d8bef9SDimitry Andric ///
5769e8d8bef9SDimitry Andric /// I1     Save LR                    OUTLINED_FUNCTION:
5770e8d8bef9SDimitry Andric /// I2 --> BL OUTLINED_FUNCTION       I1
5771e8d8bef9SDimitry Andric /// I3     Restore LR                 I2
5772e8d8bef9SDimitry Andric ///                                   I3
5773e8d8bef9SDimitry Andric ///                                   BX LR
5774e8d8bef9SDimitry Andric ///
5775e8d8bef9SDimitry Andric /// +-------------------------+--------+-----+
5776e8d8bef9SDimitry Andric /// |                         | Thumb2 | ARM |
5777e8d8bef9SDimitry Andric /// +-------------------------+--------+-----+
5778e8d8bef9SDimitry Andric /// | Call overhead in Bytes  |      8 |  12 |
5779e8d8bef9SDimitry Andric /// | Frame overhead in Bytes |      2 |   4 |
5780e8d8bef9SDimitry Andric /// | Stack fixup required    |    Yes | Yes |
5781e8d8bef9SDimitry Andric /// +-------------------------+--------+-----+
57825ffd83dbSDimitry Andric 
57835ffd83dbSDimitry Andric enum MachineOutlinerClass {
57845ffd83dbSDimitry Andric   MachineOutlinerTailCall,
57855ffd83dbSDimitry Andric   MachineOutlinerThunk,
57865ffd83dbSDimitry Andric   MachineOutlinerNoLRSave,
5787e8d8bef9SDimitry Andric   MachineOutlinerRegSave,
5788e8d8bef9SDimitry Andric   MachineOutlinerDefault
57895ffd83dbSDimitry Andric };
57905ffd83dbSDimitry Andric 
57915ffd83dbSDimitry Andric enum MachineOutlinerMBBFlags {
57925ffd83dbSDimitry Andric   LRUnavailableSomewhere = 0x2,
57935ffd83dbSDimitry Andric   HasCalls = 0x4,
57945ffd83dbSDimitry Andric   UnsafeRegsDead = 0x8
57955ffd83dbSDimitry Andric };
57965ffd83dbSDimitry Andric 
57975ffd83dbSDimitry Andric struct OutlinerCosts {
57984824e7fdSDimitry Andric   int CallTailCall;
57994824e7fdSDimitry Andric   int FrameTailCall;
58004824e7fdSDimitry Andric   int CallThunk;
58014824e7fdSDimitry Andric   int FrameThunk;
58024824e7fdSDimitry Andric   int CallNoLRSave;
58034824e7fdSDimitry Andric   int FrameNoLRSave;
58044824e7fdSDimitry Andric   int CallRegSave;
58054824e7fdSDimitry Andric   int FrameRegSave;
58064824e7fdSDimitry Andric   int CallDefault;
58074824e7fdSDimitry Andric   int FrameDefault;
58084824e7fdSDimitry Andric   int SaveRestoreLROnStack;
58095ffd83dbSDimitry Andric 
58105ffd83dbSDimitry Andric   OutlinerCosts(const ARMSubtarget &target)
58115ffd83dbSDimitry Andric       : CallTailCall(target.isThumb() ? 4 : 4),
58125ffd83dbSDimitry Andric         FrameTailCall(target.isThumb() ? 0 : 0),
58135ffd83dbSDimitry Andric         CallThunk(target.isThumb() ? 4 : 4),
58145ffd83dbSDimitry Andric         FrameThunk(target.isThumb() ? 0 : 0),
58155ffd83dbSDimitry Andric         CallNoLRSave(target.isThumb() ? 4 : 4),
58160eae32dcSDimitry Andric         FrameNoLRSave(target.isThumb() ? 2 : 4),
58175ffd83dbSDimitry Andric         CallRegSave(target.isThumb() ? 8 : 12),
5818e8d8bef9SDimitry Andric         FrameRegSave(target.isThumb() ? 2 : 4),
5819e8d8bef9SDimitry Andric         CallDefault(target.isThumb() ? 8 : 12),
5820e8d8bef9SDimitry Andric         FrameDefault(target.isThumb() ? 2 : 4),
5821e8d8bef9SDimitry Andric         SaveRestoreLROnStack(target.isThumb() ? 8 : 8) {}
58225ffd83dbSDimitry Andric };
58235ffd83dbSDimitry Andric 
582481ad6265SDimitry Andric Register
582581ad6265SDimitry Andric ARMBaseInstrInfo::findRegisterToSaveLRTo(outliner::Candidate &C) const {
58265ffd83dbSDimitry Andric   MachineFunction *MF = C.getMF();
582781ad6265SDimitry Andric   const TargetRegisterInfo &TRI = *MF->getSubtarget().getRegisterInfo();
582881ad6265SDimitry Andric   const ARMBaseRegisterInfo *ARI =
582981ad6265SDimitry Andric       static_cast<const ARMBaseRegisterInfo *>(&TRI);
58305ffd83dbSDimitry Andric 
58315ffd83dbSDimitry Andric   BitVector regsReserved = ARI->getReservedRegs(*MF);
58325ffd83dbSDimitry Andric   // Check if there is an available register across the sequence that we can
58335ffd83dbSDimitry Andric   // use.
583481ad6265SDimitry Andric   for (Register Reg : ARM::rGPRRegClass) {
58355ffd83dbSDimitry Andric     if (!(Reg < regsReserved.size() && regsReserved.test(Reg)) &&
58365ffd83dbSDimitry Andric         Reg != ARM::LR &&  // LR is not reserved, but don't use it.
58375ffd83dbSDimitry Andric         Reg != ARM::R12 && // R12 is not guaranteed to be preserved.
583881ad6265SDimitry Andric         C.isAvailableAcrossAndOutOfSeq(Reg, TRI) &&
583981ad6265SDimitry Andric         C.isAvailableInsideSeq(Reg, TRI))
58405ffd83dbSDimitry Andric       return Reg;
58415ffd83dbSDimitry Andric   }
584281ad6265SDimitry Andric   return Register();
58435ffd83dbSDimitry Andric }
58445ffd83dbSDimitry Andric 
5845e8d8bef9SDimitry Andric // Compute liveness of LR at the point after the interval [I, E), which
5846e8d8bef9SDimitry Andric // denotes a *backward* iteration through instructions. Used only for return
5847e8d8bef9SDimitry Andric // basic blocks, which do not end with a tail call.
5848e8d8bef9SDimitry Andric static bool isLRAvailable(const TargetRegisterInfo &TRI,
5849e8d8bef9SDimitry Andric                           MachineBasicBlock::reverse_iterator I,
5850e8d8bef9SDimitry Andric                           MachineBasicBlock::reverse_iterator E) {
5851e8d8bef9SDimitry Andric   // At the end of the function LR dead.
5852e8d8bef9SDimitry Andric   bool Live = false;
5853e8d8bef9SDimitry Andric   for (; I != E; ++I) {
5854e8d8bef9SDimitry Andric     const MachineInstr &MI = *I;
5855e8d8bef9SDimitry Andric 
5856e8d8bef9SDimitry Andric     // Check defs of LR.
5857e8d8bef9SDimitry Andric     if (MI.modifiesRegister(ARM::LR, &TRI))
5858e8d8bef9SDimitry Andric       Live = false;
5859e8d8bef9SDimitry Andric 
5860e8d8bef9SDimitry Andric     // Check uses of LR.
5861e8d8bef9SDimitry Andric     unsigned Opcode = MI.getOpcode();
5862e8d8bef9SDimitry Andric     if (Opcode == ARM::BX_RET || Opcode == ARM::MOVPCLR ||
5863e8d8bef9SDimitry Andric         Opcode == ARM::SUBS_PC_LR || Opcode == ARM::tBX_RET ||
5864e8d8bef9SDimitry Andric         Opcode == ARM::tBXNS_RET) {
5865e8d8bef9SDimitry Andric       // These instructions use LR, but it's not an (explicit or implicit)
5866e8d8bef9SDimitry Andric       // operand.
5867e8d8bef9SDimitry Andric       Live = true;
5868e8d8bef9SDimitry Andric       continue;
5869e8d8bef9SDimitry Andric     }
5870e8d8bef9SDimitry Andric     if (MI.readsRegister(ARM::LR, &TRI))
5871e8d8bef9SDimitry Andric       Live = true;
5872e8d8bef9SDimitry Andric   }
5873e8d8bef9SDimitry Andric   return !Live;
5874e8d8bef9SDimitry Andric }
5875e8d8bef9SDimitry Andric 
587606c3fb27SDimitry Andric std::optional<outliner::OutlinedFunction>
587706c3fb27SDimitry Andric ARMBaseInstrInfo::getOutliningCandidateInfo(
58785ffd83dbSDimitry Andric     std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
58795ffd83dbSDimitry Andric   outliner::Candidate &FirstCand = RepeatedSequenceLocs[0];
58805ffd83dbSDimitry Andric   unsigned SequenceSize =
58815ffd83dbSDimitry Andric       std::accumulate(FirstCand.front(), std::next(FirstCand.back()), 0,
58825ffd83dbSDimitry Andric                       [this](unsigned Sum, const MachineInstr &MI) {
58835ffd83dbSDimitry Andric                         return Sum + getInstSizeInBytes(MI);
58845ffd83dbSDimitry Andric                       });
58855ffd83dbSDimitry Andric 
58865ffd83dbSDimitry Andric   // Properties about candidate MBBs that hold for all of them.
58875ffd83dbSDimitry Andric   unsigned FlagsSetInAll = 0xF;
58885ffd83dbSDimitry Andric 
58895ffd83dbSDimitry Andric   // Compute liveness information for each candidate, and set FlagsSetInAll.
58905ffd83dbSDimitry Andric   const TargetRegisterInfo &TRI = getRegisterInfo();
589181ad6265SDimitry Andric   for (outliner::Candidate &C : RepeatedSequenceLocs)
589281ad6265SDimitry Andric     FlagsSetInAll &= C.Flags;
58935ffd83dbSDimitry Andric 
58945ffd83dbSDimitry Andric   // According to the ARM Procedure Call Standard, the following are
58955ffd83dbSDimitry Andric   // undefined on entry/exit from a function call:
58965ffd83dbSDimitry Andric   //
58975ffd83dbSDimitry Andric   // * Register R12(IP),
58985ffd83dbSDimitry Andric   // * Condition codes (and thus the CPSR register)
58995ffd83dbSDimitry Andric   //
59005ffd83dbSDimitry Andric   // Since we control the instructions which are part of the outlined regions
59015ffd83dbSDimitry Andric   // we don't need to be fully compliant with the AAPCS, but we have to
59025ffd83dbSDimitry Andric   // guarantee that if a veneer is inserted at link time the code is still
59035ffd83dbSDimitry Andric   // correct.  Because of this, we can't outline any sequence of instructions
59045ffd83dbSDimitry Andric   // where one of these registers is live into/across it. Thus, we need to
59055ffd83dbSDimitry Andric   // delete those candidates.
59065ffd83dbSDimitry Andric   auto CantGuaranteeValueAcrossCall = [&TRI](outliner::Candidate &C) {
59075ffd83dbSDimitry Andric     // If the unsafe registers in this block are all dead, then we don't need
59085ffd83dbSDimitry Andric     // to compute liveness here.
59095ffd83dbSDimitry Andric     if (C.Flags & UnsafeRegsDead)
59105ffd83dbSDimitry Andric       return false;
591181ad6265SDimitry Andric     return C.isAnyUnavailableAcrossOrOutOfSeq({ARM::R12, ARM::CPSR}, TRI);
59125ffd83dbSDimitry Andric   };
59135ffd83dbSDimitry Andric 
59145ffd83dbSDimitry Andric   // Are there any candidates where those registers are live?
59155ffd83dbSDimitry Andric   if (!(FlagsSetInAll & UnsafeRegsDead)) {
59165ffd83dbSDimitry Andric     // Erase every candidate that violates the restrictions above. (It could be
59175ffd83dbSDimitry Andric     // true that we have viable candidates, so it's not worth bailing out in
59185ffd83dbSDimitry Andric     // the case that, say, 1 out of 20 candidates violate the restructions.)
5919e8d8bef9SDimitry Andric     llvm::erase_if(RepeatedSequenceLocs, CantGuaranteeValueAcrossCall);
59205ffd83dbSDimitry Andric 
59215ffd83dbSDimitry Andric     // If the sequence doesn't have enough candidates left, then we're done.
59225ffd83dbSDimitry Andric     if (RepeatedSequenceLocs.size() < 2)
592306c3fb27SDimitry Andric       return std::nullopt;
59245ffd83dbSDimitry Andric   }
59255ffd83dbSDimitry Andric 
59260eae32dcSDimitry Andric   // We expect the majority of the outlining candidates to be in consensus with
59270eae32dcSDimitry Andric   // regard to return address sign and authentication, and branch target
59280eae32dcSDimitry Andric   // enforcement, in other words, partitioning according to all the four
59290eae32dcSDimitry Andric   // possible combinations of PAC-RET and BTI is going to yield one big subset
59300eae32dcSDimitry Andric   // and three small (likely empty) subsets. That allows us to cull incompatible
59310eae32dcSDimitry Andric   // candidates separately for PAC-RET and BTI.
59320eae32dcSDimitry Andric 
59334824e7fdSDimitry Andric   // Partition the candidates in two sets: one with BTI enabled and one with BTI
59340eae32dcSDimitry Andric   // disabled. Remove the candidates from the smaller set. If they are the same
59350eae32dcSDimitry Andric   // number prefer the non-BTI ones for outlining, since they have less
59360eae32dcSDimitry Andric   // overhead.
59374824e7fdSDimitry Andric   auto NoBTI =
59384824e7fdSDimitry Andric       llvm::partition(RepeatedSequenceLocs, [](const outliner::Candidate &C) {
59394824e7fdSDimitry Andric         const ARMFunctionInfo &AFI = *C.getMF()->getInfo<ARMFunctionInfo>();
59404824e7fdSDimitry Andric         return AFI.branchTargetEnforcement();
59414824e7fdSDimitry Andric       });
59424824e7fdSDimitry Andric   if (std::distance(RepeatedSequenceLocs.begin(), NoBTI) >
59434824e7fdSDimitry Andric       std::distance(NoBTI, RepeatedSequenceLocs.end()))
59444824e7fdSDimitry Andric     RepeatedSequenceLocs.erase(NoBTI, RepeatedSequenceLocs.end());
59454824e7fdSDimitry Andric   else
59464824e7fdSDimitry Andric     RepeatedSequenceLocs.erase(RepeatedSequenceLocs.begin(), NoBTI);
59470eae32dcSDimitry Andric 
59480eae32dcSDimitry Andric   if (RepeatedSequenceLocs.size() < 2)
594906c3fb27SDimitry Andric     return std::nullopt;
59500eae32dcSDimitry Andric 
59510eae32dcSDimitry Andric   // Likewise, partition the candidates according to PAC-RET enablement.
59520eae32dcSDimitry Andric   auto NoPAC =
59530eae32dcSDimitry Andric       llvm::partition(RepeatedSequenceLocs, [](const outliner::Candidate &C) {
59540eae32dcSDimitry Andric         const ARMFunctionInfo &AFI = *C.getMF()->getInfo<ARMFunctionInfo>();
59550eae32dcSDimitry Andric         // If the function happens to not spill the LR, do not disqualify it
59560eae32dcSDimitry Andric         // from the outlining.
59570eae32dcSDimitry Andric         return AFI.shouldSignReturnAddress(true);
59580eae32dcSDimitry Andric       });
59590eae32dcSDimitry Andric   if (std::distance(RepeatedSequenceLocs.begin(), NoPAC) >
59600eae32dcSDimitry Andric       std::distance(NoPAC, RepeatedSequenceLocs.end()))
59610eae32dcSDimitry Andric     RepeatedSequenceLocs.erase(NoPAC, RepeatedSequenceLocs.end());
59620eae32dcSDimitry Andric   else
59630eae32dcSDimitry Andric     RepeatedSequenceLocs.erase(RepeatedSequenceLocs.begin(), NoPAC);
59640eae32dcSDimitry Andric 
59654824e7fdSDimitry Andric   if (RepeatedSequenceLocs.size() < 2)
596606c3fb27SDimitry Andric     return std::nullopt;
59674824e7fdSDimitry Andric 
59685ffd83dbSDimitry Andric   // At this point, we have only "safe" candidates to outline. Figure out
59695ffd83dbSDimitry Andric   // frame + call instruction information.
59705ffd83dbSDimitry Andric 
59715ffd83dbSDimitry Andric   unsigned LastInstrOpcode = RepeatedSequenceLocs[0].back()->getOpcode();
59725ffd83dbSDimitry Andric 
59735ffd83dbSDimitry Andric   // Helper lambda which sets call information for every candidate.
59745ffd83dbSDimitry Andric   auto SetCandidateCallInfo =
59755ffd83dbSDimitry Andric       [&RepeatedSequenceLocs](unsigned CallID, unsigned NumBytesForCall) {
59765ffd83dbSDimitry Andric         for (outliner::Candidate &C : RepeatedSequenceLocs)
59775ffd83dbSDimitry Andric           C.setCallInfo(CallID, NumBytesForCall);
59785ffd83dbSDimitry Andric       };
59795ffd83dbSDimitry Andric 
59805ffd83dbSDimitry Andric   OutlinerCosts Costs(Subtarget);
59810eae32dcSDimitry Andric 
59824824e7fdSDimitry Andric   const auto &SomeMFI =
59834824e7fdSDimitry Andric       *RepeatedSequenceLocs.front().getMF()->getInfo<ARMFunctionInfo>();
59844824e7fdSDimitry Andric   // Adjust costs to account for the BTI instructions.
59854824e7fdSDimitry Andric   if (SomeMFI.branchTargetEnforcement()) {
59864824e7fdSDimitry Andric     Costs.FrameDefault += 4;
59874824e7fdSDimitry Andric     Costs.FrameNoLRSave += 4;
59884824e7fdSDimitry Andric     Costs.FrameRegSave += 4;
59894824e7fdSDimitry Andric     Costs.FrameTailCall += 4;
59904824e7fdSDimitry Andric     Costs.FrameThunk += 4;
59914824e7fdSDimitry Andric   }
59920eae32dcSDimitry Andric 
59930eae32dcSDimitry Andric   // Adjust costs to account for sign and authentication instructions.
59940eae32dcSDimitry Andric   if (SomeMFI.shouldSignReturnAddress(true)) {
59950eae32dcSDimitry Andric     Costs.CallDefault += 8;          // +PAC instr, +AUT instr
59960eae32dcSDimitry Andric     Costs.SaveRestoreLROnStack += 8; // +PAC instr, +AUT instr
59970eae32dcSDimitry Andric   }
59980eae32dcSDimitry Andric 
5999e8d8bef9SDimitry Andric   unsigned FrameID = MachineOutlinerDefault;
6000e8d8bef9SDimitry Andric   unsigned NumBytesToCreateFrame = Costs.FrameDefault;
60015ffd83dbSDimitry Andric 
60025ffd83dbSDimitry Andric   // If the last instruction in any candidate is a terminator, then we should
60035ffd83dbSDimitry Andric   // tail call all of the candidates.
60045ffd83dbSDimitry Andric   if (RepeatedSequenceLocs[0].back()->isTerminator()) {
60055ffd83dbSDimitry Andric     FrameID = MachineOutlinerTailCall;
60065ffd83dbSDimitry Andric     NumBytesToCreateFrame = Costs.FrameTailCall;
60075ffd83dbSDimitry Andric     SetCandidateCallInfo(MachineOutlinerTailCall, Costs.CallTailCall);
60085ffd83dbSDimitry Andric   } else if (LastInstrOpcode == ARM::BL || LastInstrOpcode == ARM::BLX ||
6009e8d8bef9SDimitry Andric              LastInstrOpcode == ARM::BLX_noip || LastInstrOpcode == ARM::tBL ||
6010e8d8bef9SDimitry Andric              LastInstrOpcode == ARM::tBLXr ||
6011e8d8bef9SDimitry Andric              LastInstrOpcode == ARM::tBLXr_noip ||
60125ffd83dbSDimitry Andric              LastInstrOpcode == ARM::tBLXi) {
60135ffd83dbSDimitry Andric     FrameID = MachineOutlinerThunk;
60145ffd83dbSDimitry Andric     NumBytesToCreateFrame = Costs.FrameThunk;
60155ffd83dbSDimitry Andric     SetCandidateCallInfo(MachineOutlinerThunk, Costs.CallThunk);
60165ffd83dbSDimitry Andric   } else {
60175ffd83dbSDimitry Andric     // We need to decide how to emit calls + frames. We can always emit the same
6018e8d8bef9SDimitry Andric     // frame if we don't need to save to the stack. If we have to save to the
6019e8d8bef9SDimitry Andric     // stack, then we need a different frame.
60205ffd83dbSDimitry Andric     unsigned NumBytesNoStackCalls = 0;
60215ffd83dbSDimitry Andric     std::vector<outliner::Candidate> CandidatesWithoutStackFixups;
60225ffd83dbSDimitry Andric 
60235ffd83dbSDimitry Andric     for (outliner::Candidate &C : RepeatedSequenceLocs) {
6024e8d8bef9SDimitry Andric       // LR liveness is overestimated in return blocks, unless they end with a
6025e8d8bef9SDimitry Andric       // tail call.
6026e8d8bef9SDimitry Andric       const auto Last = C.getMBB()->rbegin();
6027e8d8bef9SDimitry Andric       const bool LRIsAvailable =
6028e8d8bef9SDimitry Andric           C.getMBB()->isReturnBlock() && !Last->isCall()
6029e8d8bef9SDimitry Andric               ? isLRAvailable(TRI, Last,
6030e8d8bef9SDimitry Andric                               (MachineBasicBlock::reverse_iterator)C.front())
603181ad6265SDimitry Andric               : C.isAvailableAcrossAndOutOfSeq(ARM::LR, TRI);
6032e8d8bef9SDimitry Andric       if (LRIsAvailable) {
60335ffd83dbSDimitry Andric         FrameID = MachineOutlinerNoLRSave;
60345ffd83dbSDimitry Andric         NumBytesNoStackCalls += Costs.CallNoLRSave;
60355ffd83dbSDimitry Andric         C.setCallInfo(MachineOutlinerNoLRSave, Costs.CallNoLRSave);
60365ffd83dbSDimitry Andric         CandidatesWithoutStackFixups.push_back(C);
60375ffd83dbSDimitry Andric       }
60385ffd83dbSDimitry Andric 
60395ffd83dbSDimitry Andric       // Is an unused register available? If so, we won't modify the stack, so
60405ffd83dbSDimitry Andric       // we can outline with the same frame type as those that don't save LR.
60415ffd83dbSDimitry Andric       else if (findRegisterToSaveLRTo(C)) {
60425ffd83dbSDimitry Andric         FrameID = MachineOutlinerRegSave;
60435ffd83dbSDimitry Andric         NumBytesNoStackCalls += Costs.CallRegSave;
60445ffd83dbSDimitry Andric         C.setCallInfo(MachineOutlinerRegSave, Costs.CallRegSave);
60455ffd83dbSDimitry Andric         CandidatesWithoutStackFixups.push_back(C);
60465ffd83dbSDimitry Andric       }
6047e8d8bef9SDimitry Andric 
6048e8d8bef9SDimitry Andric       // Is SP used in the sequence at all? If not, we don't have to modify
6049e8d8bef9SDimitry Andric       // the stack, so we are guaranteed to get the same frame.
605081ad6265SDimitry Andric       else if (C.isAvailableInsideSeq(ARM::SP, TRI)) {
6051e8d8bef9SDimitry Andric         NumBytesNoStackCalls += Costs.CallDefault;
6052e8d8bef9SDimitry Andric         C.setCallInfo(MachineOutlinerDefault, Costs.CallDefault);
6053e8d8bef9SDimitry Andric         CandidatesWithoutStackFixups.push_back(C);
60545ffd83dbSDimitry Andric       }
60555ffd83dbSDimitry Andric 
6056e8d8bef9SDimitry Andric       // If we outline this, we need to modify the stack. Pretend we don't
6057e8d8bef9SDimitry Andric       // outline this by saving all of its bytes.
6058e8d8bef9SDimitry Andric       else
6059e8d8bef9SDimitry Andric         NumBytesNoStackCalls += SequenceSize;
6060e8d8bef9SDimitry Andric     }
6061e8d8bef9SDimitry Andric 
6062e8d8bef9SDimitry Andric     // If there are no places where we have to save LR, then note that we don't
6063e8d8bef9SDimitry Andric     // have to update the stack. Otherwise, give every candidate the default
6064e8d8bef9SDimitry Andric     // call type
6065e8d8bef9SDimitry Andric     if (NumBytesNoStackCalls <=
6066e8d8bef9SDimitry Andric         RepeatedSequenceLocs.size() * Costs.CallDefault) {
60675ffd83dbSDimitry Andric       RepeatedSequenceLocs = CandidatesWithoutStackFixups;
6068e8d8bef9SDimitry Andric       FrameID = MachineOutlinerNoLRSave;
60695ffd83dbSDimitry Andric     } else
6070e8d8bef9SDimitry Andric       SetCandidateCallInfo(MachineOutlinerDefault, Costs.CallDefault);
6071e8d8bef9SDimitry Andric   }
6072e8d8bef9SDimitry Andric 
6073e8d8bef9SDimitry Andric   // Does every candidate's MBB contain a call?  If so, then we might have a
6074e8d8bef9SDimitry Andric   // call in the range.
6075e8d8bef9SDimitry Andric   if (FlagsSetInAll & MachineOutlinerMBBFlags::HasCalls) {
6076e8d8bef9SDimitry Andric     // check if the range contains a call.  These require a save + restore of
6077e8d8bef9SDimitry Andric     // the link register.
6078e8d8bef9SDimitry Andric     if (std::any_of(FirstCand.front(), FirstCand.back(),
6079e8d8bef9SDimitry Andric                     [](const MachineInstr &MI) { return MI.isCall(); }))
6080e8d8bef9SDimitry Andric       NumBytesToCreateFrame += Costs.SaveRestoreLROnStack;
6081e8d8bef9SDimitry Andric 
6082e8d8bef9SDimitry Andric     // Handle the last instruction separately.  If it is tail call, then the
6083e8d8bef9SDimitry Andric     // last instruction is a call, we don't want to save + restore in this
6084e8d8bef9SDimitry Andric     // case.  However, it could be possible that the last instruction is a
6085e8d8bef9SDimitry Andric     // call without it being valid to tail call this sequence.  We should
6086e8d8bef9SDimitry Andric     // consider this as well.
6087e8d8bef9SDimitry Andric     else if (FrameID != MachineOutlinerThunk &&
6088e8d8bef9SDimitry Andric              FrameID != MachineOutlinerTailCall && FirstCand.back()->isCall())
6089e8d8bef9SDimitry Andric       NumBytesToCreateFrame += Costs.SaveRestoreLROnStack;
60905ffd83dbSDimitry Andric   }
60915ffd83dbSDimitry Andric 
60925ffd83dbSDimitry Andric   return outliner::OutlinedFunction(RepeatedSequenceLocs, SequenceSize,
60935ffd83dbSDimitry Andric                                     NumBytesToCreateFrame, FrameID);
60945ffd83dbSDimitry Andric }
60955ffd83dbSDimitry Andric 
6096e8d8bef9SDimitry Andric bool ARMBaseInstrInfo::checkAndUpdateStackOffset(MachineInstr *MI,
6097e8d8bef9SDimitry Andric                                                  int64_t Fixup,
6098e8d8bef9SDimitry Andric                                                  bool Updt) const {
6099e8d8bef9SDimitry Andric   int SPIdx = MI->findRegisterUseOperandIdx(ARM::SP);
6100e8d8bef9SDimitry Andric   unsigned AddrMode = (MI->getDesc().TSFlags & ARMII::AddrModeMask);
6101e8d8bef9SDimitry Andric   if (SPIdx < 0)
6102e8d8bef9SDimitry Andric     // No SP operand
6103e8d8bef9SDimitry Andric     return true;
6104e8d8bef9SDimitry Andric   else if (SPIdx != 1 && (AddrMode != ARMII::AddrModeT2_i8s4 || SPIdx != 2))
6105e8d8bef9SDimitry Andric     // If SP is not the base register we can't do much
6106e8d8bef9SDimitry Andric     return false;
6107e8d8bef9SDimitry Andric 
6108e8d8bef9SDimitry Andric   // Stack might be involved but addressing mode doesn't handle any offset.
6109e8d8bef9SDimitry Andric   // Rq: AddrModeT1_[1|2|4] don't operate on SP
61104824e7fdSDimitry Andric   if (AddrMode == ARMII::AddrMode1 ||       // Arithmetic instructions
61114824e7fdSDimitry Andric       AddrMode == ARMII::AddrMode4 ||       // Load/Store Multiple
61124824e7fdSDimitry Andric       AddrMode == ARMII::AddrMode6 ||       // Neon Load/Store Multiple
61134824e7fdSDimitry Andric       AddrMode == ARMII::AddrModeT2_so ||   // SP can't be used as based register
61144824e7fdSDimitry Andric       AddrMode == ARMII::AddrModeT2_pc ||   // PCrel access
61154824e7fdSDimitry Andric       AddrMode == ARMII::AddrMode2 ||       // Used by PRE and POST indexed LD/ST
61164824e7fdSDimitry Andric       AddrMode == ARMII::AddrModeT2_i7 ||   // v8.1-M MVE
61174824e7fdSDimitry Andric       AddrMode == ARMII::AddrModeT2_i7s2 || // v8.1-M MVE
61184824e7fdSDimitry Andric       AddrMode == ARMII::AddrModeT2_i7s4 || // v8.1-M sys regs VLDR/VSTR
61194824e7fdSDimitry Andric       AddrMode == ARMII::AddrModeNone ||
61204824e7fdSDimitry Andric       AddrMode == ARMII::AddrModeT2_i8 ||   // Pre/Post inc instructions
61214824e7fdSDimitry Andric       AddrMode == ARMII::AddrModeT2_i8neg)  // Always negative imm
6122e8d8bef9SDimitry Andric     return false;
6123e8d8bef9SDimitry Andric 
6124e8d8bef9SDimitry Andric   unsigned NumOps = MI->getDesc().getNumOperands();
6125e8d8bef9SDimitry Andric   unsigned ImmIdx = NumOps - 3;
6126e8d8bef9SDimitry Andric 
6127e8d8bef9SDimitry Andric   const MachineOperand &Offset = MI->getOperand(ImmIdx);
6128e8d8bef9SDimitry Andric   assert(Offset.isImm() && "Is not an immediate");
6129e8d8bef9SDimitry Andric   int64_t OffVal = Offset.getImm();
6130e8d8bef9SDimitry Andric 
6131e8d8bef9SDimitry Andric   if (OffVal < 0)
6132e8d8bef9SDimitry Andric     // Don't override data if the are below SP.
6133e8d8bef9SDimitry Andric     return false;
6134e8d8bef9SDimitry Andric 
6135e8d8bef9SDimitry Andric   unsigned NumBits = 0;
6136e8d8bef9SDimitry Andric   unsigned Scale = 1;
6137e8d8bef9SDimitry Andric 
6138e8d8bef9SDimitry Andric   switch (AddrMode) {
6139e8d8bef9SDimitry Andric   case ARMII::AddrMode3:
6140e8d8bef9SDimitry Andric     if (ARM_AM::getAM3Op(OffVal) == ARM_AM::sub)
6141e8d8bef9SDimitry Andric       return false;
6142e8d8bef9SDimitry Andric     OffVal = ARM_AM::getAM3Offset(OffVal);
6143e8d8bef9SDimitry Andric     NumBits = 8;
6144e8d8bef9SDimitry Andric     break;
6145e8d8bef9SDimitry Andric   case ARMII::AddrMode5:
6146e8d8bef9SDimitry Andric     if (ARM_AM::getAM5Op(OffVal) == ARM_AM::sub)
6147e8d8bef9SDimitry Andric       return false;
6148e8d8bef9SDimitry Andric     OffVal = ARM_AM::getAM5Offset(OffVal);
6149e8d8bef9SDimitry Andric     NumBits = 8;
6150e8d8bef9SDimitry Andric     Scale = 4;
6151e8d8bef9SDimitry Andric     break;
6152e8d8bef9SDimitry Andric   case ARMII::AddrMode5FP16:
6153e8d8bef9SDimitry Andric     if (ARM_AM::getAM5FP16Op(OffVal) == ARM_AM::sub)
6154e8d8bef9SDimitry Andric       return false;
6155e8d8bef9SDimitry Andric     OffVal = ARM_AM::getAM5FP16Offset(OffVal);
6156e8d8bef9SDimitry Andric     NumBits = 8;
6157e8d8bef9SDimitry Andric     Scale = 2;
6158e8d8bef9SDimitry Andric     break;
61594824e7fdSDimitry Andric   case ARMII::AddrModeT2_i8pos:
6160e8d8bef9SDimitry Andric     NumBits = 8;
6161e8d8bef9SDimitry Andric     break;
6162e8d8bef9SDimitry Andric   case ARMII::AddrModeT2_i8s4:
616323408297SDimitry Andric     // FIXME: Values are already scaled in this addressing mode.
616423408297SDimitry Andric     assert((Fixup & 3) == 0 && "Can't encode this offset!");
616523408297SDimitry Andric     NumBits = 10;
616623408297SDimitry Andric     break;
6167e8d8bef9SDimitry Andric   case ARMII::AddrModeT2_ldrex:
6168e8d8bef9SDimitry Andric     NumBits = 8;
6169e8d8bef9SDimitry Andric     Scale = 4;
6170e8d8bef9SDimitry Andric     break;
6171e8d8bef9SDimitry Andric   case ARMII::AddrModeT2_i12:
6172e8d8bef9SDimitry Andric   case ARMII::AddrMode_i12:
6173e8d8bef9SDimitry Andric     NumBits = 12;
6174e8d8bef9SDimitry Andric     break;
6175e8d8bef9SDimitry Andric   case ARMII::AddrModeT1_s: // SP-relative LD/ST
6176e8d8bef9SDimitry Andric     NumBits = 8;
6177e8d8bef9SDimitry Andric     Scale = 4;
6178e8d8bef9SDimitry Andric     break;
6179e8d8bef9SDimitry Andric   default:
6180e8d8bef9SDimitry Andric     llvm_unreachable("Unsupported addressing mode!");
6181e8d8bef9SDimitry Andric   }
6182e8d8bef9SDimitry Andric   // Make sure the offset is encodable for instructions that scale the
6183e8d8bef9SDimitry Andric   // immediate.
618423408297SDimitry Andric   assert(((OffVal * Scale + Fixup) & (Scale - 1)) == 0 &&
618523408297SDimitry Andric          "Can't encode this offset!");
6186e8d8bef9SDimitry Andric   OffVal += Fixup / Scale;
6187e8d8bef9SDimitry Andric 
6188e8d8bef9SDimitry Andric   unsigned Mask = (1 << NumBits) - 1;
6189e8d8bef9SDimitry Andric 
6190e8d8bef9SDimitry Andric   if (OffVal <= Mask) {
6191e8d8bef9SDimitry Andric     if (Updt)
6192e8d8bef9SDimitry Andric       MI->getOperand(ImmIdx).setImm(OffVal);
6193e8d8bef9SDimitry Andric     return true;
6194e8d8bef9SDimitry Andric   }
6195e8d8bef9SDimitry Andric 
6196e8d8bef9SDimitry Andric   return false;
61974824e7fdSDimitry Andric }
6198e8d8bef9SDimitry Andric 
61994824e7fdSDimitry Andric void ARMBaseInstrInfo::mergeOutliningCandidateAttributes(
62004824e7fdSDimitry Andric     Function &F, std::vector<outliner::Candidate> &Candidates) const {
62014824e7fdSDimitry Andric   outliner::Candidate &C = Candidates.front();
62024824e7fdSDimitry Andric   // branch-target-enforcement is guaranteed to be consistent between all
62034824e7fdSDimitry Andric   // candidates, so we only need to look at one.
62044824e7fdSDimitry Andric   const Function &CFn = C.getMF()->getFunction();
62054824e7fdSDimitry Andric   if (CFn.hasFnAttribute("branch-target-enforcement"))
62064824e7fdSDimitry Andric     F.addFnAttr(CFn.getFnAttribute("branch-target-enforcement"));
62074824e7fdSDimitry Andric 
62084824e7fdSDimitry Andric   ARMGenInstrInfo::mergeOutliningCandidateAttributes(F, Candidates);
6209e8d8bef9SDimitry Andric }
6210e8d8bef9SDimitry Andric 
62115ffd83dbSDimitry Andric bool ARMBaseInstrInfo::isFunctionSafeToOutlineFrom(
62125ffd83dbSDimitry Andric     MachineFunction &MF, bool OutlineFromLinkOnceODRs) const {
62135ffd83dbSDimitry Andric   const Function &F = MF.getFunction();
62145ffd83dbSDimitry Andric 
62155ffd83dbSDimitry Andric   // Can F be deduplicated by the linker? If it can, don't outline from it.
62165ffd83dbSDimitry Andric   if (!OutlineFromLinkOnceODRs && F.hasLinkOnceODRLinkage())
62175ffd83dbSDimitry Andric     return false;
62185ffd83dbSDimitry Andric 
62195ffd83dbSDimitry Andric   // Don't outline from functions with section markings; the program could
62205ffd83dbSDimitry Andric   // expect that all the code is in the named section.
62215ffd83dbSDimitry Andric   // FIXME: Allow outlining from multiple functions with the same section
62225ffd83dbSDimitry Andric   // marking.
62235ffd83dbSDimitry Andric   if (F.hasSection())
62245ffd83dbSDimitry Andric     return false;
62255ffd83dbSDimitry Andric 
62265ffd83dbSDimitry Andric   // FIXME: Thumb1 outlining is not handled
62275ffd83dbSDimitry Andric   if (MF.getInfo<ARMFunctionInfo>()->isThumb1OnlyFunction())
62285ffd83dbSDimitry Andric     return false;
62295ffd83dbSDimitry Andric 
62305ffd83dbSDimitry Andric   // It's safe to outline from MF.
62315ffd83dbSDimitry Andric   return true;
62325ffd83dbSDimitry Andric }
62335ffd83dbSDimitry Andric 
62345ffd83dbSDimitry Andric bool ARMBaseInstrInfo::isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
62355ffd83dbSDimitry Andric                                               unsigned &Flags) const {
62365ffd83dbSDimitry Andric   // Check if LR is available through all of the MBB. If it's not, then set
62375ffd83dbSDimitry Andric   // a flag.
62385ffd83dbSDimitry Andric   assert(MBB.getParent()->getRegInfo().tracksLiveness() &&
62395ffd83dbSDimitry Andric          "Suitable Machine Function for outlining must track liveness");
62405ffd83dbSDimitry Andric 
62415ffd83dbSDimitry Andric   LiveRegUnits LRU(getRegisterInfo());
62425ffd83dbSDimitry Andric 
624381ad6265SDimitry Andric   for (MachineInstr &MI : llvm::reverse(MBB))
624481ad6265SDimitry Andric     LRU.accumulate(MI);
62455ffd83dbSDimitry Andric 
62465ffd83dbSDimitry Andric   // Check if each of the unsafe registers are available...
62475ffd83dbSDimitry Andric   bool R12AvailableInBlock = LRU.available(ARM::R12);
62485ffd83dbSDimitry Andric   bool CPSRAvailableInBlock = LRU.available(ARM::CPSR);
62495ffd83dbSDimitry Andric 
62505ffd83dbSDimitry Andric   // If all of these are dead (and not live out), we know we don't have to check
62515ffd83dbSDimitry Andric   // them later.
62525ffd83dbSDimitry Andric   if (R12AvailableInBlock && CPSRAvailableInBlock)
62535ffd83dbSDimitry Andric     Flags |= MachineOutlinerMBBFlags::UnsafeRegsDead;
62545ffd83dbSDimitry Andric 
62555ffd83dbSDimitry Andric   // Now, add the live outs to the set.
62565ffd83dbSDimitry Andric   LRU.addLiveOuts(MBB);
62575ffd83dbSDimitry Andric 
62585ffd83dbSDimitry Andric   // If any of these registers is available in the MBB, but also a live out of
62595ffd83dbSDimitry Andric   // the block, then we know outlining is unsafe.
62605ffd83dbSDimitry Andric   if (R12AvailableInBlock && !LRU.available(ARM::R12))
62615ffd83dbSDimitry Andric     return false;
62625ffd83dbSDimitry Andric   if (CPSRAvailableInBlock && !LRU.available(ARM::CPSR))
62635ffd83dbSDimitry Andric     return false;
62645ffd83dbSDimitry Andric 
62655ffd83dbSDimitry Andric   // Check if there's a call inside this MachineBasicBlock.  If there is, then
62665ffd83dbSDimitry Andric   // set a flag.
62675ffd83dbSDimitry Andric   if (any_of(MBB, [](MachineInstr &MI) { return MI.isCall(); }))
62685ffd83dbSDimitry Andric     Flags |= MachineOutlinerMBBFlags::HasCalls;
62695ffd83dbSDimitry Andric 
6270e8d8bef9SDimitry Andric   // LR liveness is overestimated in return blocks.
6271e8d8bef9SDimitry Andric 
6272e8d8bef9SDimitry Andric   bool LRIsAvailable =
6273e8d8bef9SDimitry Andric       MBB.isReturnBlock() && !MBB.back().isCall()
6274e8d8bef9SDimitry Andric           ? isLRAvailable(getRegisterInfo(), MBB.rbegin(), MBB.rend())
6275e8d8bef9SDimitry Andric           : LRU.available(ARM::LR);
6276e8d8bef9SDimitry Andric   if (!LRIsAvailable)
62775ffd83dbSDimitry Andric     Flags |= MachineOutlinerMBBFlags::LRUnavailableSomewhere;
62785ffd83dbSDimitry Andric 
62795ffd83dbSDimitry Andric   return true;
62805ffd83dbSDimitry Andric }
62815ffd83dbSDimitry Andric 
62825ffd83dbSDimitry Andric outliner::InstrType
628306c3fb27SDimitry Andric ARMBaseInstrInfo::getOutliningTypeImpl(MachineBasicBlock::iterator &MIT,
62845ffd83dbSDimitry Andric                                    unsigned Flags) const {
62855ffd83dbSDimitry Andric   MachineInstr &MI = *MIT;
62865ffd83dbSDimitry Andric   const TargetRegisterInfo *TRI = &getRegisterInfo();
62875ffd83dbSDimitry Andric 
62885ffd83dbSDimitry Andric   // PIC instructions contain labels, outlining them would break offset
62895ffd83dbSDimitry Andric   // computing.  unsigned Opc = MI.getOpcode();
62905ffd83dbSDimitry Andric   unsigned Opc = MI.getOpcode();
62915ffd83dbSDimitry Andric   if (Opc == ARM::tPICADD || Opc == ARM::PICADD || Opc == ARM::PICSTR ||
62925ffd83dbSDimitry Andric       Opc == ARM::PICSTRB || Opc == ARM::PICSTRH || Opc == ARM::PICLDR ||
62935ffd83dbSDimitry Andric       Opc == ARM::PICLDRB || Opc == ARM::PICLDRH || Opc == ARM::PICLDRSB ||
62945ffd83dbSDimitry Andric       Opc == ARM::PICLDRSH || Opc == ARM::t2LDRpci_pic ||
62955ffd83dbSDimitry Andric       Opc == ARM::t2MOVi16_ga_pcrel || Opc == ARM::t2MOVTi16_ga_pcrel ||
62965ffd83dbSDimitry Andric       Opc == ARM::t2MOV_ga_pcrel)
62975ffd83dbSDimitry Andric     return outliner::InstrType::Illegal;
62985ffd83dbSDimitry Andric 
62995ffd83dbSDimitry Andric   // Be conservative with ARMv8.1 MVE instructions.
63005ffd83dbSDimitry Andric   if (Opc == ARM::t2BF_LabelPseudo || Opc == ARM::t2DoLoopStart ||
6301e8d8bef9SDimitry Andric       Opc == ARM::t2DoLoopStartTP || Opc == ARM::t2WhileLoopStart ||
6302fe6060f1SDimitry Andric       Opc == ARM::t2WhileLoopStartLR || Opc == ARM::t2WhileLoopStartTP ||
6303e8d8bef9SDimitry Andric       Opc == ARM::t2LoopDec || Opc == ARM::t2LoopEnd ||
6304e8d8bef9SDimitry Andric       Opc == ARM::t2LoopEndDec)
63055ffd83dbSDimitry Andric     return outliner::InstrType::Illegal;
63065ffd83dbSDimitry Andric 
63075ffd83dbSDimitry Andric   const MCInstrDesc &MCID = MI.getDesc();
63085ffd83dbSDimitry Andric   uint64_t MIFlags = MCID.TSFlags;
63095ffd83dbSDimitry Andric   if ((MIFlags & ARMII::DomainMask) == ARMII::DomainMVE)
63105ffd83dbSDimitry Andric     return outliner::InstrType::Illegal;
63115ffd83dbSDimitry Andric 
63125ffd83dbSDimitry Andric   // Is this a terminator for a basic block?
631306c3fb27SDimitry Andric   if (MI.isTerminator())
631406c3fb27SDimitry Andric     // TargetInstrInfo::getOutliningType has already filtered out anything
631506c3fb27SDimitry Andric     // that would break this, so we can allow it here.
63165ffd83dbSDimitry Andric     return outliner::InstrType::Legal;
63175ffd83dbSDimitry Andric 
63185ffd83dbSDimitry Andric   // Don't outline if link register or program counter value are used.
63195ffd83dbSDimitry Andric   if (MI.readsRegister(ARM::LR, TRI) || MI.readsRegister(ARM::PC, TRI))
63205ffd83dbSDimitry Andric     return outliner::InstrType::Illegal;
63215ffd83dbSDimitry Andric 
63225ffd83dbSDimitry Andric   if (MI.isCall()) {
6323e8d8bef9SDimitry Andric     // Get the function associated with the call.  Look at each operand and find
6324e8d8bef9SDimitry Andric     // the one that represents the calle and get its name.
6325e8d8bef9SDimitry Andric     const Function *Callee = nullptr;
6326e8d8bef9SDimitry Andric     for (const MachineOperand &MOP : MI.operands()) {
6327e8d8bef9SDimitry Andric       if (MOP.isGlobal()) {
6328e8d8bef9SDimitry Andric         Callee = dyn_cast<Function>(MOP.getGlobal());
6329e8d8bef9SDimitry Andric         break;
6330e8d8bef9SDimitry Andric       }
6331e8d8bef9SDimitry Andric     }
6332e8d8bef9SDimitry Andric 
6333e8d8bef9SDimitry Andric     // Dont't outline calls to "mcount" like functions, in particular Linux
6334e8d8bef9SDimitry Andric     // kernel function tracing relies on it.
6335e8d8bef9SDimitry Andric     if (Callee &&
6336e8d8bef9SDimitry Andric         (Callee->getName() == "\01__gnu_mcount_nc" ||
6337e8d8bef9SDimitry Andric          Callee->getName() == "\01mcount" || Callee->getName() == "__mcount"))
6338e8d8bef9SDimitry Andric       return outliner::InstrType::Illegal;
6339e8d8bef9SDimitry Andric 
63405ffd83dbSDimitry Andric     // If we don't know anything about the callee, assume it depends on the
63415ffd83dbSDimitry Andric     // stack layout of the caller. In that case, it's only legal to outline
63425ffd83dbSDimitry Andric     // as a tail-call. Explicitly list the call instructions we know about so
63435ffd83dbSDimitry Andric     // we don't get unexpected results with call pseudo-instructions.
63445ffd83dbSDimitry Andric     auto UnknownCallOutlineType = outliner::InstrType::Illegal;
63455ffd83dbSDimitry Andric     if (Opc == ARM::BL || Opc == ARM::tBL || Opc == ARM::BLX ||
6346e8d8bef9SDimitry Andric         Opc == ARM::BLX_noip || Opc == ARM::tBLXr || Opc == ARM::tBLXr_noip ||
6347e8d8bef9SDimitry Andric         Opc == ARM::tBLXi)
63485ffd83dbSDimitry Andric       UnknownCallOutlineType = outliner::InstrType::LegalTerminator;
63495ffd83dbSDimitry Andric 
6350e8d8bef9SDimitry Andric     if (!Callee)
63515ffd83dbSDimitry Andric       return UnknownCallOutlineType;
6352e8d8bef9SDimitry Andric 
6353e8d8bef9SDimitry Andric     // We have a function we have information about.  Check if it's something we
6354e8d8bef9SDimitry Andric     // can safely outline.
6355e8d8bef9SDimitry Andric     MachineFunction *MF = MI.getParent()->getParent();
6356e8d8bef9SDimitry Andric     MachineFunction *CalleeMF = MF->getMMI().getMachineFunction(*Callee);
6357e8d8bef9SDimitry Andric 
6358e8d8bef9SDimitry Andric     // We don't know what's going on with the callee at all.  Don't touch it.
6359e8d8bef9SDimitry Andric     if (!CalleeMF)
6360e8d8bef9SDimitry Andric       return UnknownCallOutlineType;
6361e8d8bef9SDimitry Andric 
6362e8d8bef9SDimitry Andric     // Check if we know anything about the callee saves on the function. If we
6363e8d8bef9SDimitry Andric     // don't, then don't touch it, since that implies that we haven't computed
6364e8d8bef9SDimitry Andric     // anything about its stack frame yet.
6365e8d8bef9SDimitry Andric     MachineFrameInfo &MFI = CalleeMF->getFrameInfo();
6366e8d8bef9SDimitry Andric     if (!MFI.isCalleeSavedInfoValid() || MFI.getStackSize() > 0 ||
6367e8d8bef9SDimitry Andric         MFI.getNumObjects() > 0)
6368e8d8bef9SDimitry Andric       return UnknownCallOutlineType;
6369e8d8bef9SDimitry Andric 
6370e8d8bef9SDimitry Andric     // At this point, we can say that CalleeMF ought to not pass anything on the
6371e8d8bef9SDimitry Andric     // stack. Therefore, we can outline it.
6372e8d8bef9SDimitry Andric     return outliner::InstrType::Legal;
63735ffd83dbSDimitry Andric   }
63745ffd83dbSDimitry Andric 
63755ffd83dbSDimitry Andric   // Since calls are handled, don't touch LR or PC
63765ffd83dbSDimitry Andric   if (MI.modifiesRegister(ARM::LR, TRI) || MI.modifiesRegister(ARM::PC, TRI))
63775ffd83dbSDimitry Andric     return outliner::InstrType::Illegal;
63785ffd83dbSDimitry Andric 
63795ffd83dbSDimitry Andric   // Does this use the stack?
63805ffd83dbSDimitry Andric   if (MI.modifiesRegister(ARM::SP, TRI) || MI.readsRegister(ARM::SP, TRI)) {
63815ffd83dbSDimitry Andric     // True if there is no chance that any outlined candidate from this range
63825ffd83dbSDimitry Andric     // could require stack fixups. That is, both
63835ffd83dbSDimitry Andric     // * LR is available in the range (No save/restore around call)
63845ffd83dbSDimitry Andric     // * The range doesn't include calls (No save/restore in outlined frame)
63855ffd83dbSDimitry Andric     // are true.
63860eae32dcSDimitry Andric     // These conditions also ensure correctness of the return address
63870eae32dcSDimitry Andric     // authentication - we insert sign and authentication instructions only if
63880eae32dcSDimitry Andric     // we save/restore LR on stack, but then this condition ensures that the
63890eae32dcSDimitry Andric     // outlined range does not modify the SP, therefore the SP value used for
63900eae32dcSDimitry Andric     // signing is the same as the one used for authentication.
63915ffd83dbSDimitry Andric     // FIXME: This is very restrictive; the flags check the whole block,
63925ffd83dbSDimitry Andric     // not just the bit we will try to outline.
63935ffd83dbSDimitry Andric     bool MightNeedStackFixUp =
63945ffd83dbSDimitry Andric         (Flags & (MachineOutlinerMBBFlags::LRUnavailableSomewhere |
63955ffd83dbSDimitry Andric                   MachineOutlinerMBBFlags::HasCalls));
63965ffd83dbSDimitry Andric 
63975ffd83dbSDimitry Andric     if (!MightNeedStackFixUp)
63985ffd83dbSDimitry Andric       return outliner::InstrType::Legal;
63995ffd83dbSDimitry Andric 
6400e8d8bef9SDimitry Andric     // Any modification of SP will break our code to save/restore LR.
6401e8d8bef9SDimitry Andric     // FIXME: We could handle some instructions which add a constant offset to
6402e8d8bef9SDimitry Andric     // SP, with a bit more work.
6403e8d8bef9SDimitry Andric     if (MI.modifiesRegister(ARM::SP, TRI))
6404e8d8bef9SDimitry Andric       return outliner::InstrType::Illegal;
6405e8d8bef9SDimitry Andric 
6406e8d8bef9SDimitry Andric     // At this point, we have a stack instruction that we might need to fix up.
6407e8d8bef9SDimitry Andric     // up. We'll handle it if it's a load or store.
6408e8d8bef9SDimitry Andric     if (checkAndUpdateStackOffset(&MI, Subtarget.getStackAlignment().value(),
6409e8d8bef9SDimitry Andric                                   false))
6410e8d8bef9SDimitry Andric       return outliner::InstrType::Legal;
6411e8d8bef9SDimitry Andric 
6412e8d8bef9SDimitry Andric     // We can't fix it up, so don't outline it.
64135ffd83dbSDimitry Andric     return outliner::InstrType::Illegal;
64145ffd83dbSDimitry Andric   }
64155ffd83dbSDimitry Andric 
64165ffd83dbSDimitry Andric   // Be conservative with IT blocks.
64175ffd83dbSDimitry Andric   if (MI.readsRegister(ARM::ITSTATE, TRI) ||
64185ffd83dbSDimitry Andric       MI.modifiesRegister(ARM::ITSTATE, TRI))
64195ffd83dbSDimitry Andric     return outliner::InstrType::Illegal;
64205ffd83dbSDimitry Andric 
642106c3fb27SDimitry Andric   // Don't outline CFI instructions.
642206c3fb27SDimitry Andric   if (MI.isCFIInstruction())
64235ffd83dbSDimitry Andric     return outliner::InstrType::Illegal;
64245ffd83dbSDimitry Andric 
64255ffd83dbSDimitry Andric   return outliner::InstrType::Legal;
64265ffd83dbSDimitry Andric }
64275ffd83dbSDimitry Andric 
6428e8d8bef9SDimitry Andric void ARMBaseInstrInfo::fixupPostOutline(MachineBasicBlock &MBB) const {
6429e8d8bef9SDimitry Andric   for (MachineInstr &MI : MBB) {
6430e8d8bef9SDimitry Andric     checkAndUpdateStackOffset(&MI, Subtarget.getStackAlignment().value(), true);
6431e8d8bef9SDimitry Andric   }
6432e8d8bef9SDimitry Andric }
6433e8d8bef9SDimitry Andric 
6434e8d8bef9SDimitry Andric void ARMBaseInstrInfo::saveLROnStack(MachineBasicBlock &MBB,
64350eae32dcSDimitry Andric                                      MachineBasicBlock::iterator It, bool CFI,
64360eae32dcSDimitry Andric                                      bool Auth) const {
64370eae32dcSDimitry Andric   int Align = std::max(Subtarget.getStackAlignment().value(), uint64_t(8));
6438*5f757f3fSDimitry Andric   unsigned MIFlags = CFI ? MachineInstr::FrameSetup : 0;
64390eae32dcSDimitry Andric   assert(Align >= 8 && Align <= 256);
64400eae32dcSDimitry Andric   if (Auth) {
64410eae32dcSDimitry Andric     assert(Subtarget.isThumb2());
64420eae32dcSDimitry Andric     // Compute PAC in R12. Outlining ensures R12 is dead across the outlined
64430eae32dcSDimitry Andric     // sequence.
6444*5f757f3fSDimitry Andric     BuildMI(MBB, It, DebugLoc(), get(ARM::t2PAC)).setMIFlags(MIFlags);
64450eae32dcSDimitry Andric     BuildMI(MBB, It, DebugLoc(), get(ARM::t2STRD_PRE), ARM::SP)
64460eae32dcSDimitry Andric         .addReg(ARM::R12, RegState::Kill)
64470eae32dcSDimitry Andric         .addReg(ARM::LR, RegState::Kill)
64480eae32dcSDimitry Andric         .addReg(ARM::SP)
64490eae32dcSDimitry Andric         .addImm(-Align)
64500eae32dcSDimitry Andric         .add(predOps(ARMCC::AL))
6451*5f757f3fSDimitry Andric         .setMIFlags(MIFlags);
64520eae32dcSDimitry Andric   } else {
6453e8d8bef9SDimitry Andric     unsigned Opc = Subtarget.isThumb() ? ARM::t2STR_PRE : ARM::STR_PRE_IMM;
6454e8d8bef9SDimitry Andric     BuildMI(MBB, It, DebugLoc(), get(Opc), ARM::SP)
6455e8d8bef9SDimitry Andric         .addReg(ARM::LR, RegState::Kill)
6456e8d8bef9SDimitry Andric         .addReg(ARM::SP)
64570eae32dcSDimitry Andric         .addImm(-Align)
64580eae32dcSDimitry Andric         .add(predOps(ARMCC::AL))
6459*5f757f3fSDimitry Andric         .setMIFlags(MIFlags);
6460e8d8bef9SDimitry Andric   }
6461e8d8bef9SDimitry Andric 
64620eae32dcSDimitry Andric   if (!CFI)
64630eae32dcSDimitry Andric     return;
64640eae32dcSDimitry Andric 
6465e8d8bef9SDimitry Andric   MachineFunction &MF = *MBB.getParent();
64660eae32dcSDimitry Andric 
64670eae32dcSDimitry Andric   // Add a CFI, saying CFA is offset by Align bytes from SP.
6468e8d8bef9SDimitry Andric   int64_t StackPosEntry =
6469e8d8bef9SDimitry Andric       MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, Align));
6470e8d8bef9SDimitry Andric   BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
6471e8d8bef9SDimitry Andric       .addCFIIndex(StackPosEntry)
6472e8d8bef9SDimitry Andric       .setMIFlags(MachineInstr::FrameSetup);
6473e8d8bef9SDimitry Andric 
6474e8d8bef9SDimitry Andric   // Add a CFI saying that the LR that we want to find is now higher than
6475e8d8bef9SDimitry Andric   // before.
64760eae32dcSDimitry Andric   int LROffset = Auth ? Align - 4 : Align;
64770eae32dcSDimitry Andric   const MCRegisterInfo *MRI = Subtarget.getRegisterInfo();
64780eae32dcSDimitry Andric   unsigned DwarfLR = MRI->getDwarfRegNum(ARM::LR, true);
64790eae32dcSDimitry Andric   int64_t LRPosEntry = MF.addFrameInst(
64800eae32dcSDimitry Andric       MCCFIInstruction::createOffset(nullptr, DwarfLR, -LROffset));
6481e8d8bef9SDimitry Andric   BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
6482e8d8bef9SDimitry Andric       .addCFIIndex(LRPosEntry)
6483e8d8bef9SDimitry Andric       .setMIFlags(MachineInstr::FrameSetup);
64840eae32dcSDimitry Andric   if (Auth) {
64850eae32dcSDimitry Andric     // Add a CFI for the location of the return adddress PAC.
64860eae32dcSDimitry Andric     unsigned DwarfRAC = MRI->getDwarfRegNum(ARM::RA_AUTH_CODE, true);
64870eae32dcSDimitry Andric     int64_t RACPosEntry = MF.addFrameInst(
64880eae32dcSDimitry Andric         MCCFIInstruction::createOffset(nullptr, DwarfRAC, -Align));
64890eae32dcSDimitry Andric     BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
64900eae32dcSDimitry Andric         .addCFIIndex(RACPosEntry)
64910eae32dcSDimitry Andric         .setMIFlags(MachineInstr::FrameSetup);
64920eae32dcSDimitry Andric   }
6493e8d8bef9SDimitry Andric }
6494e8d8bef9SDimitry Andric 
6495e8d8bef9SDimitry Andric void ARMBaseInstrInfo::emitCFIForLRSaveToReg(MachineBasicBlock &MBB,
6496e8d8bef9SDimitry Andric                                              MachineBasicBlock::iterator It,
6497e8d8bef9SDimitry Andric                                              Register Reg) const {
6498e8d8bef9SDimitry Andric   MachineFunction &MF = *MBB.getParent();
6499e8d8bef9SDimitry Andric   const MCRegisterInfo *MRI = Subtarget.getRegisterInfo();
6500e8d8bef9SDimitry Andric   unsigned DwarfLR = MRI->getDwarfRegNum(ARM::LR, true);
6501e8d8bef9SDimitry Andric   unsigned DwarfReg = MRI->getDwarfRegNum(Reg, true);
6502e8d8bef9SDimitry Andric 
6503e8d8bef9SDimitry Andric   int64_t LRPosEntry = MF.addFrameInst(
6504e8d8bef9SDimitry Andric       MCCFIInstruction::createRegister(nullptr, DwarfLR, DwarfReg));
6505e8d8bef9SDimitry Andric   BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
6506e8d8bef9SDimitry Andric       .addCFIIndex(LRPosEntry)
6507e8d8bef9SDimitry Andric       .setMIFlags(MachineInstr::FrameSetup);
6508e8d8bef9SDimitry Andric }
6509e8d8bef9SDimitry Andric 
65100eae32dcSDimitry Andric void ARMBaseInstrInfo::restoreLRFromStack(MachineBasicBlock &MBB,
65110eae32dcSDimitry Andric                                           MachineBasicBlock::iterator It,
65120eae32dcSDimitry Andric                                           bool CFI, bool Auth) const {
65130eae32dcSDimitry Andric   int Align = Subtarget.getStackAlignment().value();
6514*5f757f3fSDimitry Andric   unsigned MIFlags = CFI ? MachineInstr::FrameDestroy : 0;
65150eae32dcSDimitry Andric   if (Auth) {
65160eae32dcSDimitry Andric     assert(Subtarget.isThumb2());
65170eae32dcSDimitry Andric     // Restore return address PAC and LR.
65180eae32dcSDimitry Andric     BuildMI(MBB, It, DebugLoc(), get(ARM::t2LDRD_POST))
65190eae32dcSDimitry Andric         .addReg(ARM::R12, RegState::Define)
65200eae32dcSDimitry Andric         .addReg(ARM::LR, RegState::Define)
65210eae32dcSDimitry Andric         .addReg(ARM::SP, RegState::Define)
65220eae32dcSDimitry Andric         .addReg(ARM::SP)
65230eae32dcSDimitry Andric         .addImm(Align)
65240eae32dcSDimitry Andric         .add(predOps(ARMCC::AL))
6525*5f757f3fSDimitry Andric         .setMIFlags(MIFlags);
65260eae32dcSDimitry Andric     // LR authentication is after the CFI instructions, below.
65270eae32dcSDimitry Andric   } else {
6528e8d8bef9SDimitry Andric     unsigned Opc = Subtarget.isThumb() ? ARM::t2LDR_POST : ARM::LDR_POST_IMM;
6529e8d8bef9SDimitry Andric     MachineInstrBuilder MIB = BuildMI(MBB, It, DebugLoc(), get(Opc), ARM::LR)
6530e8d8bef9SDimitry Andric                                   .addReg(ARM::SP, RegState::Define)
6531e8d8bef9SDimitry Andric                                   .addReg(ARM::SP);
6532e8d8bef9SDimitry Andric     if (!Subtarget.isThumb())
6533e8d8bef9SDimitry Andric       MIB.addReg(0);
65340eae32dcSDimitry Andric     MIB.addImm(Subtarget.getStackAlignment().value())
65350eae32dcSDimitry Andric         .add(predOps(ARMCC::AL))
6536*5f757f3fSDimitry Andric         .setMIFlags(MIFlags);
6537e8d8bef9SDimitry Andric   }
6538e8d8bef9SDimitry Andric 
65390eae32dcSDimitry Andric   if (CFI) {
6540e8d8bef9SDimitry Andric     // Now stack has moved back up...
6541e8d8bef9SDimitry Andric     MachineFunction &MF = *MBB.getParent();
6542e8d8bef9SDimitry Andric     const MCRegisterInfo *MRI = Subtarget.getRegisterInfo();
6543e8d8bef9SDimitry Andric     unsigned DwarfLR = MRI->getDwarfRegNum(ARM::LR, true);
6544e8d8bef9SDimitry Andric     int64_t StackPosEntry =
6545e8d8bef9SDimitry Andric         MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, 0));
6546e8d8bef9SDimitry Andric     BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
6547e8d8bef9SDimitry Andric         .addCFIIndex(StackPosEntry)
6548e8d8bef9SDimitry Andric         .setMIFlags(MachineInstr::FrameDestroy);
6549e8d8bef9SDimitry Andric 
6550e8d8bef9SDimitry Andric     // ... and we have restored LR.
6551e8d8bef9SDimitry Andric     int64_t LRPosEntry =
6552e8d8bef9SDimitry Andric         MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, DwarfLR));
6553e8d8bef9SDimitry Andric     BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
6554e8d8bef9SDimitry Andric         .addCFIIndex(LRPosEntry)
6555e8d8bef9SDimitry Andric         .setMIFlags(MachineInstr::FrameDestroy);
65560eae32dcSDimitry Andric 
65570eae32dcSDimitry Andric     if (Auth) {
65580eae32dcSDimitry Andric       unsigned DwarfRAC = MRI->getDwarfRegNum(ARM::RA_AUTH_CODE, true);
65590eae32dcSDimitry Andric       int64_t Entry =
65600eae32dcSDimitry Andric           MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, DwarfRAC));
65610eae32dcSDimitry Andric       BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
65620eae32dcSDimitry Andric           .addCFIIndex(Entry)
65630eae32dcSDimitry Andric           .setMIFlags(MachineInstr::FrameDestroy);
65640eae32dcSDimitry Andric     }
65650eae32dcSDimitry Andric   }
65660eae32dcSDimitry Andric 
65670eae32dcSDimitry Andric   if (Auth)
65680eae32dcSDimitry Andric     BuildMI(MBB, It, DebugLoc(), get(ARM::t2AUT));
6569e8d8bef9SDimitry Andric }
6570e8d8bef9SDimitry Andric 
6571e8d8bef9SDimitry Andric void ARMBaseInstrInfo::emitCFIForLRRestoreFromReg(
6572e8d8bef9SDimitry Andric     MachineBasicBlock &MBB, MachineBasicBlock::iterator It) const {
6573e8d8bef9SDimitry Andric   MachineFunction &MF = *MBB.getParent();
6574e8d8bef9SDimitry Andric   const MCRegisterInfo *MRI = Subtarget.getRegisterInfo();
6575e8d8bef9SDimitry Andric   unsigned DwarfLR = MRI->getDwarfRegNum(ARM::LR, true);
6576e8d8bef9SDimitry Andric 
6577e8d8bef9SDimitry Andric   int64_t LRPosEntry =
6578e8d8bef9SDimitry Andric       MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, DwarfLR));
6579e8d8bef9SDimitry Andric   BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
6580e8d8bef9SDimitry Andric       .addCFIIndex(LRPosEntry)
6581e8d8bef9SDimitry Andric       .setMIFlags(MachineInstr::FrameDestroy);
6582e8d8bef9SDimitry Andric }
6583e8d8bef9SDimitry Andric 
65845ffd83dbSDimitry Andric void ARMBaseInstrInfo::buildOutlinedFrame(
65855ffd83dbSDimitry Andric     MachineBasicBlock &MBB, MachineFunction &MF,
65865ffd83dbSDimitry Andric     const outliner::OutlinedFunction &OF) const {
65875ffd83dbSDimitry Andric   // For thunk outlining, rewrite the last instruction from a call to a
65885ffd83dbSDimitry Andric   // tail-call.
65895ffd83dbSDimitry Andric   if (OF.FrameConstructionID == MachineOutlinerThunk) {
65905ffd83dbSDimitry Andric     MachineInstr *Call = &*--MBB.instr_end();
65915ffd83dbSDimitry Andric     bool isThumb = Subtarget.isThumb();
65925ffd83dbSDimitry Andric     unsigned FuncOp = isThumb ? 2 : 0;
65935ffd83dbSDimitry Andric     unsigned Opc = Call->getOperand(FuncOp).isReg()
65945ffd83dbSDimitry Andric                        ? isThumb ? ARM::tTAILJMPr : ARM::TAILJMPr
65955ffd83dbSDimitry Andric                        : isThumb ? Subtarget.isTargetMachO() ? ARM::tTAILJMPd
65965ffd83dbSDimitry Andric                                                              : ARM::tTAILJMPdND
65975ffd83dbSDimitry Andric                                  : ARM::TAILJMPd;
65985ffd83dbSDimitry Andric     MachineInstrBuilder MIB = BuildMI(MBB, MBB.end(), DebugLoc(), get(Opc))
65995ffd83dbSDimitry Andric                                   .add(Call->getOperand(FuncOp));
66005ffd83dbSDimitry Andric     if (isThumb && !Call->getOperand(FuncOp).isReg())
66015ffd83dbSDimitry Andric       MIB.add(predOps(ARMCC::AL));
66025ffd83dbSDimitry Andric     Call->eraseFromParent();
66035ffd83dbSDimitry Andric   }
66045ffd83dbSDimitry Andric 
6605e8d8bef9SDimitry Andric   // Is there a call in the outlined range?
6606e8d8bef9SDimitry Andric   auto IsNonTailCall = [](MachineInstr &MI) {
6607e8d8bef9SDimitry Andric     return MI.isCall() && !MI.isReturn();
6608e8d8bef9SDimitry Andric   };
6609e8d8bef9SDimitry Andric   if (llvm::any_of(MBB.instrs(), IsNonTailCall)) {
6610e8d8bef9SDimitry Andric     MachineBasicBlock::iterator It = MBB.begin();
6611e8d8bef9SDimitry Andric     MachineBasicBlock::iterator Et = MBB.end();
6612e8d8bef9SDimitry Andric 
6613e8d8bef9SDimitry Andric     if (OF.FrameConstructionID == MachineOutlinerTailCall ||
6614e8d8bef9SDimitry Andric         OF.FrameConstructionID == MachineOutlinerThunk)
6615e8d8bef9SDimitry Andric       Et = std::prev(MBB.end());
6616e8d8bef9SDimitry Andric 
6617e8d8bef9SDimitry Andric     // We have to save and restore LR, we need to add it to the liveins if it
6618e8d8bef9SDimitry Andric     // is not already part of the set.  This is suffient since outlined
6619e8d8bef9SDimitry Andric     // functions only have one block.
6620e8d8bef9SDimitry Andric     if (!MBB.isLiveIn(ARM::LR))
6621e8d8bef9SDimitry Andric       MBB.addLiveIn(ARM::LR);
6622e8d8bef9SDimitry Andric 
6623e8d8bef9SDimitry Andric     // Insert a save before the outlined region
66240eae32dcSDimitry Andric     bool Auth = OF.Candidates.front()
66250eae32dcSDimitry Andric                     .getMF()
66260eae32dcSDimitry Andric                     ->getInfo<ARMFunctionInfo>()
66270eae32dcSDimitry Andric                     ->shouldSignReturnAddress(true);
66280eae32dcSDimitry Andric     saveLROnStack(MBB, It, true, Auth);
6629e8d8bef9SDimitry Andric 
6630e8d8bef9SDimitry Andric     // Fix up the instructions in the range, since we're going to modify the
6631e8d8bef9SDimitry Andric     // stack.
6632e8d8bef9SDimitry Andric     assert(OF.FrameConstructionID != MachineOutlinerDefault &&
6633e8d8bef9SDimitry Andric            "Can only fix up stack references once");
6634e8d8bef9SDimitry Andric     fixupPostOutline(MBB);
6635e8d8bef9SDimitry Andric 
6636e8d8bef9SDimitry Andric     // Insert a restore before the terminator for the function.  Restore LR.
66370eae32dcSDimitry Andric     restoreLRFromStack(MBB, Et, true, Auth);
6638e8d8bef9SDimitry Andric   }
6639e8d8bef9SDimitry Andric 
6640e8d8bef9SDimitry Andric   // If this is a tail call outlined function, then there's already a return.
6641e8d8bef9SDimitry Andric   if (OF.FrameConstructionID == MachineOutlinerTailCall ||
6642e8d8bef9SDimitry Andric       OF.FrameConstructionID == MachineOutlinerThunk)
6643e8d8bef9SDimitry Andric     return;
6644e8d8bef9SDimitry Andric 
66455ffd83dbSDimitry Andric   // Here we have to insert the return ourselves.  Get the correct opcode from
66465ffd83dbSDimitry Andric   // current feature set.
66475ffd83dbSDimitry Andric   BuildMI(MBB, MBB.end(), DebugLoc(), get(Subtarget.getReturnOpcode()))
66485ffd83dbSDimitry Andric       .add(predOps(ARMCC::AL));
6649e8d8bef9SDimitry Andric 
6650e8d8bef9SDimitry Andric   // Did we have to modify the stack by saving the link register?
6651e8d8bef9SDimitry Andric   if (OF.FrameConstructionID != MachineOutlinerDefault &&
6652e8d8bef9SDimitry Andric       OF.Candidates[0].CallConstructionID != MachineOutlinerDefault)
6653e8d8bef9SDimitry Andric     return;
6654e8d8bef9SDimitry Andric 
6655e8d8bef9SDimitry Andric   // We modified the stack.
6656e8d8bef9SDimitry Andric   // Walk over the basic block and fix up all the stack accesses.
6657e8d8bef9SDimitry Andric   fixupPostOutline(MBB);
66585ffd83dbSDimitry Andric }
66595ffd83dbSDimitry Andric 
66605ffd83dbSDimitry Andric MachineBasicBlock::iterator ARMBaseInstrInfo::insertOutlinedCall(
66615ffd83dbSDimitry Andric     Module &M, MachineBasicBlock &MBB, MachineBasicBlock::iterator &It,
666281ad6265SDimitry Andric     MachineFunction &MF, outliner::Candidate &C) const {
66635ffd83dbSDimitry Andric   MachineInstrBuilder MIB;
66645ffd83dbSDimitry Andric   MachineBasicBlock::iterator CallPt;
66655ffd83dbSDimitry Andric   unsigned Opc;
66665ffd83dbSDimitry Andric   bool isThumb = Subtarget.isThumb();
66675ffd83dbSDimitry Andric 
66685ffd83dbSDimitry Andric   // Are we tail calling?
66695ffd83dbSDimitry Andric   if (C.CallConstructionID == MachineOutlinerTailCall) {
66705ffd83dbSDimitry Andric     // If yes, then we can just branch to the label.
66715ffd83dbSDimitry Andric     Opc = isThumb
66725ffd83dbSDimitry Andric               ? Subtarget.isTargetMachO() ? ARM::tTAILJMPd : ARM::tTAILJMPdND
66735ffd83dbSDimitry Andric               : ARM::TAILJMPd;
66745ffd83dbSDimitry Andric     MIB = BuildMI(MF, DebugLoc(), get(Opc))
66755ffd83dbSDimitry Andric               .addGlobalAddress(M.getNamedValue(MF.getName()));
66765ffd83dbSDimitry Andric     if (isThumb)
66775ffd83dbSDimitry Andric       MIB.add(predOps(ARMCC::AL));
66785ffd83dbSDimitry Andric     It = MBB.insert(It, MIB);
66795ffd83dbSDimitry Andric     return It;
66805ffd83dbSDimitry Andric   }
66815ffd83dbSDimitry Andric 
66825ffd83dbSDimitry Andric   // Create the call instruction.
66835ffd83dbSDimitry Andric   Opc = isThumb ? ARM::tBL : ARM::BL;
66845ffd83dbSDimitry Andric   MachineInstrBuilder CallMIB = BuildMI(MF, DebugLoc(), get(Opc));
66855ffd83dbSDimitry Andric   if (isThumb)
66865ffd83dbSDimitry Andric     CallMIB.add(predOps(ARMCC::AL));
66875ffd83dbSDimitry Andric   CallMIB.addGlobalAddress(M.getNamedValue(MF.getName()));
66885ffd83dbSDimitry Andric 
6689e8d8bef9SDimitry Andric   if (C.CallConstructionID == MachineOutlinerNoLRSave ||
6690e8d8bef9SDimitry Andric       C.CallConstructionID == MachineOutlinerThunk) {
6691e8d8bef9SDimitry Andric     // No, so just insert the call.
6692e8d8bef9SDimitry Andric     It = MBB.insert(It, CallMIB);
6693e8d8bef9SDimitry Andric     return It;
6694e8d8bef9SDimitry Andric   }
6695e8d8bef9SDimitry Andric 
6696e8d8bef9SDimitry Andric   const ARMFunctionInfo &AFI = *C.getMF()->getInfo<ARMFunctionInfo>();
66975ffd83dbSDimitry Andric   // Can we save to a register?
66985ffd83dbSDimitry Andric   if (C.CallConstructionID == MachineOutlinerRegSave) {
6699bdd1243dSDimitry Andric     Register Reg = findRegisterToSaveLRTo(C);
67005ffd83dbSDimitry Andric     assert(Reg != 0 && "No callee-saved register available?");
67015ffd83dbSDimitry Andric 
67025ffd83dbSDimitry Andric     // Save and restore LR from that register.
67035ffd83dbSDimitry Andric     copyPhysReg(MBB, It, DebugLoc(), Reg, ARM::LR, true);
6704e8d8bef9SDimitry Andric     if (!AFI.isLRSpilled())
6705e8d8bef9SDimitry Andric       emitCFIForLRSaveToReg(MBB, It, Reg);
67065ffd83dbSDimitry Andric     CallPt = MBB.insert(It, CallMIB);
67075ffd83dbSDimitry Andric     copyPhysReg(MBB, It, DebugLoc(), ARM::LR, Reg, true);
6708e8d8bef9SDimitry Andric     if (!AFI.isLRSpilled())
6709e8d8bef9SDimitry Andric       emitCFIForLRRestoreFromReg(MBB, It);
67105ffd83dbSDimitry Andric     It--;
67115ffd83dbSDimitry Andric     return CallPt;
67125ffd83dbSDimitry Andric   }
6713e8d8bef9SDimitry Andric   // We have the default case. Save and restore from SP.
6714e8d8bef9SDimitry Andric   if (!MBB.isLiveIn(ARM::LR))
6715e8d8bef9SDimitry Andric     MBB.addLiveIn(ARM::LR);
67160eae32dcSDimitry Andric   bool Auth = !AFI.isLRSpilled() && AFI.shouldSignReturnAddress(true);
67170eae32dcSDimitry Andric   saveLROnStack(MBB, It, !AFI.isLRSpilled(), Auth);
6718e8d8bef9SDimitry Andric   CallPt = MBB.insert(It, CallMIB);
67190eae32dcSDimitry Andric   restoreLRFromStack(MBB, It, !AFI.isLRSpilled(), Auth);
6720e8d8bef9SDimitry Andric   It--;
6721e8d8bef9SDimitry Andric   return CallPt;
67225ffd83dbSDimitry Andric }
6723e8d8bef9SDimitry Andric 
6724e8d8bef9SDimitry Andric bool ARMBaseInstrInfo::shouldOutlineFromFunctionByDefault(
6725e8d8bef9SDimitry Andric     MachineFunction &MF) const {
6726e8d8bef9SDimitry Andric   return Subtarget.isMClass() && MF.getFunction().hasMinSize();
6727e8d8bef9SDimitry Andric }
6728e8d8bef9SDimitry Andric 
6729fcaf7f86SDimitry Andric bool ARMBaseInstrInfo::isReallyTriviallyReMaterializable(
6730fcaf7f86SDimitry Andric     const MachineInstr &MI) const {
6731e8d8bef9SDimitry Andric   // Try hard to rematerialize any VCTPs because if we spill P0, it will block
6732e8d8bef9SDimitry Andric   // the tail predication conversion. This means that the element count
6733e8d8bef9SDimitry Andric   // register has to be live for longer, but that has to be better than
6734e8d8bef9SDimitry Andric   // spill/restore and VPT predication.
6735*5f757f3fSDimitry Andric   return (isVCTP(&MI) && !isPredicated(MI)) ||
6736*5f757f3fSDimitry Andric          TargetInstrInfo::isReallyTriviallyReMaterializable(MI);
6737e8d8bef9SDimitry Andric }
6738e8d8bef9SDimitry Andric 
6739e8d8bef9SDimitry Andric unsigned llvm::getBLXOpcode(const MachineFunction &MF) {
6740e8d8bef9SDimitry Andric   return (MF.getSubtarget<ARMSubtarget>().hardenSlsBlr()) ? ARM::BLX_noip
6741e8d8bef9SDimitry Andric                                                           : ARM::BLX;
6742e8d8bef9SDimitry Andric }
6743e8d8bef9SDimitry Andric 
6744e8d8bef9SDimitry Andric unsigned llvm::gettBLXrOpcode(const MachineFunction &MF) {
6745e8d8bef9SDimitry Andric   return (MF.getSubtarget<ARMSubtarget>().hardenSlsBlr()) ? ARM::tBLXr_noip
6746e8d8bef9SDimitry Andric                                                           : ARM::tBLXr;
6747e8d8bef9SDimitry Andric }
6748e8d8bef9SDimitry Andric 
6749e8d8bef9SDimitry Andric unsigned llvm::getBLXpredOpcode(const MachineFunction &MF) {
6750e8d8bef9SDimitry Andric   return (MF.getSubtarget<ARMSubtarget>().hardenSlsBlr()) ? ARM::BLX_pred_noip
6751e8d8bef9SDimitry Andric                                                           : ARM::BLX_pred;
6752e8d8bef9SDimitry Andric }
6753e8d8bef9SDimitry Andric 
675481ad6265SDimitry Andric namespace {
675581ad6265SDimitry Andric class ARMPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo {
675681ad6265SDimitry Andric   MachineInstr *EndLoop, *LoopCount;
675781ad6265SDimitry Andric   MachineFunction *MF;
675881ad6265SDimitry Andric   const TargetInstrInfo *TII;
675981ad6265SDimitry Andric 
6760bdd1243dSDimitry Andric   // Bitset[0 .. MAX_STAGES-1] ... iterations needed
6761bdd1243dSDimitry Andric   //       [LAST_IS_USE] : last reference to register in schedule is a use
6762bdd1243dSDimitry Andric   //       [SEEN_AS_LIVE] : Normal pressure algorithm believes register is live
6763bdd1243dSDimitry Andric   static int constexpr MAX_STAGES = 30;
6764bdd1243dSDimitry Andric   static int constexpr LAST_IS_USE = MAX_STAGES;
6765bdd1243dSDimitry Andric   static int constexpr SEEN_AS_LIVE = MAX_STAGES + 1;
6766bdd1243dSDimitry Andric   typedef std::bitset<MAX_STAGES + 2> IterNeed;
6767bdd1243dSDimitry Andric   typedef std::map<unsigned, IterNeed> IterNeeds;
6768bdd1243dSDimitry Andric 
6769bdd1243dSDimitry Andric   void bumpCrossIterationPressure(RegPressureTracker &RPT,
6770bdd1243dSDimitry Andric                                   const IterNeeds &CIN);
6771bdd1243dSDimitry Andric   bool tooMuchRegisterPressure(SwingSchedulerDAG &SSD, SMSchedule &SMS);
6772bdd1243dSDimitry Andric 
677381ad6265SDimitry Andric   // Meanings of the various stuff with loop types:
677481ad6265SDimitry Andric   // t2Bcc:
677581ad6265SDimitry Andric   //   EndLoop = branch at end of original BB that will become a kernel
677681ad6265SDimitry Andric   //   LoopCount = CC setter live into branch
677781ad6265SDimitry Andric   // t2LoopEnd:
677881ad6265SDimitry Andric   //   EndLoop = branch at end of original BB
677981ad6265SDimitry Andric   //   LoopCount = t2LoopDec
678081ad6265SDimitry Andric public:
678181ad6265SDimitry Andric   ARMPipelinerLoopInfo(MachineInstr *EndLoop, MachineInstr *LoopCount)
678281ad6265SDimitry Andric       : EndLoop(EndLoop), LoopCount(LoopCount),
678381ad6265SDimitry Andric         MF(EndLoop->getParent()->getParent()),
678481ad6265SDimitry Andric         TII(MF->getSubtarget().getInstrInfo()) {}
678581ad6265SDimitry Andric 
678681ad6265SDimitry Andric   bool shouldIgnoreForPipelining(const MachineInstr *MI) const override {
678781ad6265SDimitry Andric     // Only ignore the terminator.
678881ad6265SDimitry Andric     return MI == EndLoop || MI == LoopCount;
678981ad6265SDimitry Andric   }
679081ad6265SDimitry Andric 
6791bdd1243dSDimitry Andric   bool shouldUseSchedule(SwingSchedulerDAG &SSD, SMSchedule &SMS) override {
6792bdd1243dSDimitry Andric     if (tooMuchRegisterPressure(SSD, SMS))
6793bdd1243dSDimitry Andric       return false;
6794bdd1243dSDimitry Andric 
6795bdd1243dSDimitry Andric     return true;
6796bdd1243dSDimitry Andric   }
6797bdd1243dSDimitry Andric 
6798bdd1243dSDimitry Andric   std::optional<bool> createTripCountGreaterCondition(
679981ad6265SDimitry Andric       int TC, MachineBasicBlock &MBB,
680081ad6265SDimitry Andric       SmallVectorImpl<MachineOperand> &Cond) override {
680181ad6265SDimitry Andric 
680281ad6265SDimitry Andric     if (isCondBranchOpcode(EndLoop->getOpcode())) {
680381ad6265SDimitry Andric       Cond.push_back(EndLoop->getOperand(1));
680481ad6265SDimitry Andric       Cond.push_back(EndLoop->getOperand(2));
680581ad6265SDimitry Andric       if (EndLoop->getOperand(0).getMBB() == EndLoop->getParent()) {
680681ad6265SDimitry Andric         TII->reverseBranchCondition(Cond);
680781ad6265SDimitry Andric       }
680881ad6265SDimitry Andric       return {};
680981ad6265SDimitry Andric     } else if (EndLoop->getOpcode() == ARM::t2LoopEnd) {
681081ad6265SDimitry Andric       // General case just lets the unrolled t2LoopDec do the subtraction and
681181ad6265SDimitry Andric       // therefore just needs to check if zero has been reached.
681281ad6265SDimitry Andric       MachineInstr *LoopDec = nullptr;
681381ad6265SDimitry Andric       for (auto &I : MBB.instrs())
681481ad6265SDimitry Andric         if (I.getOpcode() == ARM::t2LoopDec)
681581ad6265SDimitry Andric           LoopDec = &I;
681681ad6265SDimitry Andric       assert(LoopDec && "Unable to find copied LoopDec");
681781ad6265SDimitry Andric       // Check if we're done with the loop.
681881ad6265SDimitry Andric       BuildMI(&MBB, LoopDec->getDebugLoc(), TII->get(ARM::t2CMPri))
681981ad6265SDimitry Andric           .addReg(LoopDec->getOperand(0).getReg())
682081ad6265SDimitry Andric           .addImm(0)
682181ad6265SDimitry Andric           .addImm(ARMCC::AL)
682281ad6265SDimitry Andric           .addReg(ARM::NoRegister);
682381ad6265SDimitry Andric       Cond.push_back(MachineOperand::CreateImm(ARMCC::EQ));
682481ad6265SDimitry Andric       Cond.push_back(MachineOperand::CreateReg(ARM::CPSR, false));
682581ad6265SDimitry Andric       return {};
682681ad6265SDimitry Andric     } else
682781ad6265SDimitry Andric       llvm_unreachable("Unknown EndLoop");
682881ad6265SDimitry Andric   }
682981ad6265SDimitry Andric 
683081ad6265SDimitry Andric   void setPreheader(MachineBasicBlock *NewPreheader) override {}
683181ad6265SDimitry Andric 
683281ad6265SDimitry Andric   void adjustTripCount(int TripCountAdjust) override {}
683381ad6265SDimitry Andric 
683481ad6265SDimitry Andric   void disposed() override {}
683581ad6265SDimitry Andric };
6836bdd1243dSDimitry Andric 
6837bdd1243dSDimitry Andric void ARMPipelinerLoopInfo::bumpCrossIterationPressure(RegPressureTracker &RPT,
6838bdd1243dSDimitry Andric                                                       const IterNeeds &CIN) {
6839bdd1243dSDimitry Andric   // Increase pressure by the amounts in CrossIterationNeeds
6840bdd1243dSDimitry Andric   for (const auto &N : CIN) {
6841bdd1243dSDimitry Andric     int Cnt = N.second.count() - N.second[SEEN_AS_LIVE] * 2;
6842bdd1243dSDimitry Andric     for (int I = 0; I < Cnt; ++I)
6843bdd1243dSDimitry Andric       RPT.increaseRegPressure(Register(N.first), LaneBitmask::getNone(),
6844bdd1243dSDimitry Andric                               LaneBitmask::getAll());
6845bdd1243dSDimitry Andric   }
6846bdd1243dSDimitry Andric   // Decrease pressure by the amounts in CrossIterationNeeds
6847bdd1243dSDimitry Andric   for (const auto &N : CIN) {
6848bdd1243dSDimitry Andric     int Cnt = N.second.count() - N.second[SEEN_AS_LIVE] * 2;
6849bdd1243dSDimitry Andric     for (int I = 0; I < Cnt; ++I)
6850bdd1243dSDimitry Andric       RPT.decreaseRegPressure(Register(N.first), LaneBitmask::getAll(),
6851bdd1243dSDimitry Andric                               LaneBitmask::getNone());
6852bdd1243dSDimitry Andric   }
6853bdd1243dSDimitry Andric }
6854bdd1243dSDimitry Andric 
6855bdd1243dSDimitry Andric bool ARMPipelinerLoopInfo::tooMuchRegisterPressure(SwingSchedulerDAG &SSD,
6856bdd1243dSDimitry Andric                                                    SMSchedule &SMS) {
6857bdd1243dSDimitry Andric   IterNeeds CrossIterationNeeds;
6858bdd1243dSDimitry Andric 
6859bdd1243dSDimitry Andric   // Determine which values will be loop-carried after the schedule is
6860bdd1243dSDimitry Andric   // applied
6861bdd1243dSDimitry Andric 
6862bdd1243dSDimitry Andric   for (auto &SU : SSD.SUnits) {
6863bdd1243dSDimitry Andric     const MachineInstr *MI = SU.getInstr();
6864bdd1243dSDimitry Andric     int Stg = SMS.stageScheduled(const_cast<SUnit *>(&SU));
6865bdd1243dSDimitry Andric     for (auto &S : SU.Succs)
6866bdd1243dSDimitry Andric       if (MI->isPHI() && S.getKind() == SDep::Anti) {
6867bdd1243dSDimitry Andric         Register Reg = S.getReg();
6868bdd1243dSDimitry Andric         if (Reg.isVirtual())
6869bdd1243dSDimitry Andric           CrossIterationNeeds.insert(std::make_pair(Reg.id(), IterNeed()))
6870bdd1243dSDimitry Andric               .first->second.set(0);
6871bdd1243dSDimitry Andric       } else if (S.isAssignedRegDep()) {
6872bdd1243dSDimitry Andric         int OStg = SMS.stageScheduled(S.getSUnit());
6873bdd1243dSDimitry Andric         if (OStg >= 0 && OStg != Stg) {
6874bdd1243dSDimitry Andric           Register Reg = S.getReg();
6875bdd1243dSDimitry Andric           if (Reg.isVirtual())
6876bdd1243dSDimitry Andric             CrossIterationNeeds.insert(std::make_pair(Reg.id(), IterNeed()))
6877bdd1243dSDimitry Andric                 .first->second |= ((1 << (OStg - Stg)) - 1);
6878bdd1243dSDimitry Andric         }
6879bdd1243dSDimitry Andric       }
6880bdd1243dSDimitry Andric   }
6881bdd1243dSDimitry Andric 
6882bdd1243dSDimitry Andric   // Determine more-or-less what the proposed schedule (reversed) is going to
6883bdd1243dSDimitry Andric   // be; it might not be quite the same because the within-cycle ordering
6884bdd1243dSDimitry Andric   // created by SMSchedule depends upon changes to help with address offsets and
6885bdd1243dSDimitry Andric   // the like.
6886bdd1243dSDimitry Andric   std::vector<SUnit *> ProposedSchedule;
6887bdd1243dSDimitry Andric   for (int Cycle = SMS.getFinalCycle(); Cycle >= SMS.getFirstCycle(); --Cycle)
6888bdd1243dSDimitry Andric     for (int Stage = 0, StageEnd = SMS.getMaxStageCount(); Stage <= StageEnd;
6889bdd1243dSDimitry Andric          ++Stage) {
6890bdd1243dSDimitry Andric       std::deque<SUnit *> Instrs =
6891bdd1243dSDimitry Andric           SMS.getInstructions(Cycle + Stage * SMS.getInitiationInterval());
6892bdd1243dSDimitry Andric       std::sort(Instrs.begin(), Instrs.end(),
6893bdd1243dSDimitry Andric                 [](SUnit *A, SUnit *B) { return A->NodeNum > B->NodeNum; });
6894bdd1243dSDimitry Andric       for (SUnit *SU : Instrs)
6895bdd1243dSDimitry Andric         ProposedSchedule.push_back(SU);
6896bdd1243dSDimitry Andric     }
6897bdd1243dSDimitry Andric 
6898bdd1243dSDimitry Andric   // Learn whether the last use/def of each cross-iteration register is a use or
6899bdd1243dSDimitry Andric   // def. If it is a def, RegisterPressure will implicitly increase max pressure
6900bdd1243dSDimitry Andric   // and we do not have to add the pressure.
6901bdd1243dSDimitry Andric   for (auto *SU : ProposedSchedule)
6902bdd1243dSDimitry Andric     for (ConstMIBundleOperands OperI(*SU->getInstr()); OperI.isValid();
6903bdd1243dSDimitry Andric          ++OperI) {
6904bdd1243dSDimitry Andric       auto MO = *OperI;
6905bdd1243dSDimitry Andric       if (!MO.isReg() || !MO.getReg())
6906bdd1243dSDimitry Andric         continue;
6907bdd1243dSDimitry Andric       Register Reg = MO.getReg();
6908bdd1243dSDimitry Andric       auto CIter = CrossIterationNeeds.find(Reg.id());
6909bdd1243dSDimitry Andric       if (CIter == CrossIterationNeeds.end() || CIter->second[LAST_IS_USE] ||
6910bdd1243dSDimitry Andric           CIter->second[SEEN_AS_LIVE])
6911bdd1243dSDimitry Andric         continue;
6912bdd1243dSDimitry Andric       if (MO.isDef() && !MO.isDead())
6913bdd1243dSDimitry Andric         CIter->second.set(SEEN_AS_LIVE);
6914bdd1243dSDimitry Andric       else if (MO.isUse())
6915bdd1243dSDimitry Andric         CIter->second.set(LAST_IS_USE);
6916bdd1243dSDimitry Andric     }
6917bdd1243dSDimitry Andric   for (auto &CI : CrossIterationNeeds)
6918bdd1243dSDimitry Andric     CI.second.reset(LAST_IS_USE);
6919bdd1243dSDimitry Andric 
6920bdd1243dSDimitry Andric   RegionPressure RecRegPressure;
6921bdd1243dSDimitry Andric   RegPressureTracker RPTracker(RecRegPressure);
6922bdd1243dSDimitry Andric   RegisterClassInfo RegClassInfo;
6923bdd1243dSDimitry Andric   RegClassInfo.runOnMachineFunction(*MF);
6924bdd1243dSDimitry Andric   RPTracker.init(MF, &RegClassInfo, nullptr, EndLoop->getParent(),
6925bdd1243dSDimitry Andric                  EndLoop->getParent()->end(), false, false);
6926bdd1243dSDimitry Andric   const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
6927bdd1243dSDimitry Andric 
6928bdd1243dSDimitry Andric   bumpCrossIterationPressure(RPTracker, CrossIterationNeeds);
6929bdd1243dSDimitry Andric 
6930bdd1243dSDimitry Andric   for (auto *SU : ProposedSchedule) {
6931bdd1243dSDimitry Andric     MachineBasicBlock::const_iterator CurInstI = SU->getInstr();
6932bdd1243dSDimitry Andric     RPTracker.setPos(std::next(CurInstI));
6933bdd1243dSDimitry Andric     RPTracker.recede();
6934bdd1243dSDimitry Andric 
6935bdd1243dSDimitry Andric     // Track what cross-iteration registers would be seen as live
6936bdd1243dSDimitry Andric     for (ConstMIBundleOperands OperI(*CurInstI); OperI.isValid(); ++OperI) {
6937bdd1243dSDimitry Andric       auto MO = *OperI;
6938bdd1243dSDimitry Andric       if (!MO.isReg() || !MO.getReg())
6939bdd1243dSDimitry Andric         continue;
6940bdd1243dSDimitry Andric       Register Reg = MO.getReg();
6941bdd1243dSDimitry Andric       if (MO.isDef() && !MO.isDead()) {
6942bdd1243dSDimitry Andric         auto CIter = CrossIterationNeeds.find(Reg.id());
6943bdd1243dSDimitry Andric         if (CIter != CrossIterationNeeds.end()) {
6944bdd1243dSDimitry Andric           CIter->second.reset(0);
6945bdd1243dSDimitry Andric           CIter->second.reset(SEEN_AS_LIVE);
6946bdd1243dSDimitry Andric         }
6947bdd1243dSDimitry Andric       }
6948bdd1243dSDimitry Andric     }
6949bdd1243dSDimitry Andric     for (auto &S : SU->Preds) {
6950bdd1243dSDimitry Andric       auto Stg = SMS.stageScheduled(SU);
6951bdd1243dSDimitry Andric       if (S.isAssignedRegDep()) {
6952bdd1243dSDimitry Andric         Register Reg = S.getReg();
6953bdd1243dSDimitry Andric         auto CIter = CrossIterationNeeds.find(Reg.id());
6954bdd1243dSDimitry Andric         if (CIter != CrossIterationNeeds.end()) {
6955bdd1243dSDimitry Andric           auto Stg2 = SMS.stageScheduled(const_cast<SUnit *>(S.getSUnit()));
6956bdd1243dSDimitry Andric           assert(Stg2 <= Stg && "Data dependence upon earlier stage");
6957bdd1243dSDimitry Andric           if (Stg - Stg2 < MAX_STAGES)
6958bdd1243dSDimitry Andric             CIter->second.set(Stg - Stg2);
6959bdd1243dSDimitry Andric           CIter->second.set(SEEN_AS_LIVE);
6960bdd1243dSDimitry Andric         }
6961bdd1243dSDimitry Andric       }
6962bdd1243dSDimitry Andric     }
6963bdd1243dSDimitry Andric 
6964bdd1243dSDimitry Andric     bumpCrossIterationPressure(RPTracker, CrossIterationNeeds);
6965bdd1243dSDimitry Andric   }
6966bdd1243dSDimitry Andric 
6967bdd1243dSDimitry Andric   auto &P = RPTracker.getPressure().MaxSetPressure;
6968bdd1243dSDimitry Andric   for (unsigned I = 0, E = P.size(); I < E; ++I)
6969bdd1243dSDimitry Andric     if (P[I] > TRI->getRegPressureSetLimit(*MF, I)) {
6970bdd1243dSDimitry Andric       return true;
6971bdd1243dSDimitry Andric     }
6972bdd1243dSDimitry Andric   return false;
6973bdd1243dSDimitry Andric }
6974bdd1243dSDimitry Andric 
697581ad6265SDimitry Andric } // namespace
697681ad6265SDimitry Andric 
697781ad6265SDimitry Andric std::unique_ptr<TargetInstrInfo::PipelinerLoopInfo>
697881ad6265SDimitry Andric ARMBaseInstrInfo::analyzeLoopForPipelining(MachineBasicBlock *LoopBB) const {
697981ad6265SDimitry Andric   MachineBasicBlock::iterator I = LoopBB->getFirstTerminator();
698081ad6265SDimitry Andric   MachineBasicBlock *Preheader = *LoopBB->pred_begin();
698181ad6265SDimitry Andric   if (Preheader == LoopBB)
698281ad6265SDimitry Andric     Preheader = *std::next(LoopBB->pred_begin());
698381ad6265SDimitry Andric 
698481ad6265SDimitry Andric   if (I != LoopBB->end() && I->getOpcode() == ARM::t2Bcc) {
698581ad6265SDimitry Andric     // If the branch is a Bcc, then the CPSR should be set somewhere within the
698681ad6265SDimitry Andric     // block.  We need to determine the reaching definition of CPSR so that
698781ad6265SDimitry Andric     // it can be marked as non-pipelineable, allowing the pipeliner to force
698881ad6265SDimitry Andric     // it into stage 0 or give up if it cannot or will not do so.
698981ad6265SDimitry Andric     MachineInstr *CCSetter = nullptr;
699081ad6265SDimitry Andric     for (auto &L : LoopBB->instrs()) {
699181ad6265SDimitry Andric       if (L.isCall())
699281ad6265SDimitry Andric         return nullptr;
699381ad6265SDimitry Andric       if (isCPSRDefined(L))
699481ad6265SDimitry Andric         CCSetter = &L;
699581ad6265SDimitry Andric     }
699681ad6265SDimitry Andric     if (CCSetter)
699781ad6265SDimitry Andric       return std::make_unique<ARMPipelinerLoopInfo>(&*I, CCSetter);
699881ad6265SDimitry Andric     else
699981ad6265SDimitry Andric       return nullptr; // Unable to find the CC setter, so unable to guarantee
700081ad6265SDimitry Andric                       // that pipeline will work
700181ad6265SDimitry Andric   }
700281ad6265SDimitry Andric 
700381ad6265SDimitry Andric   // Recognize:
700481ad6265SDimitry Andric   //   preheader:
700581ad6265SDimitry Andric   //     %1 = t2DoopLoopStart %0
700681ad6265SDimitry Andric   //   loop:
700781ad6265SDimitry Andric   //     %2 = phi %1, <not loop>, %..., %loop
700881ad6265SDimitry Andric   //     %3 = t2LoopDec %2, <imm>
700981ad6265SDimitry Andric   //     t2LoopEnd %3, %loop
701081ad6265SDimitry Andric 
701181ad6265SDimitry Andric   if (I != LoopBB->end() && I->getOpcode() == ARM::t2LoopEnd) {
701281ad6265SDimitry Andric     for (auto &L : LoopBB->instrs())
701381ad6265SDimitry Andric       if (L.isCall())
701481ad6265SDimitry Andric         return nullptr;
701581ad6265SDimitry Andric       else if (isVCTP(&L))
701681ad6265SDimitry Andric         return nullptr;
701781ad6265SDimitry Andric     Register LoopDecResult = I->getOperand(0).getReg();
701881ad6265SDimitry Andric     MachineRegisterInfo &MRI = LoopBB->getParent()->getRegInfo();
701981ad6265SDimitry Andric     MachineInstr *LoopDec = MRI.getUniqueVRegDef(LoopDecResult);
702081ad6265SDimitry Andric     if (!LoopDec || LoopDec->getOpcode() != ARM::t2LoopDec)
702181ad6265SDimitry Andric       return nullptr;
702281ad6265SDimitry Andric     MachineInstr *LoopStart = nullptr;
702381ad6265SDimitry Andric     for (auto &J : Preheader->instrs())
702481ad6265SDimitry Andric       if (J.getOpcode() == ARM::t2DoLoopStart)
702581ad6265SDimitry Andric         LoopStart = &J;
702681ad6265SDimitry Andric     if (!LoopStart)
702781ad6265SDimitry Andric       return nullptr;
702881ad6265SDimitry Andric     return std::make_unique<ARMPipelinerLoopInfo>(&*I, LoopDec);
702981ad6265SDimitry Andric   }
703081ad6265SDimitry Andric   return nullptr;
703181ad6265SDimitry Andric }
7032