Path: blob/main/contrib/llvm-project/llvm/lib/Target/ARM/ARMBaseInstrInfo.h
35269 views
//===-- ARMBaseInstrInfo.h - ARM Base Instruction Information ---*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains the Base ARM implementation of the TargetInstrInfo class.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_LIB_TARGET_ARM_ARMBASEINSTRINFO_H13#define LLVM_LIB_TARGET_ARM_ARMBASEINSTRINFO_H1415#include "ARMBaseRegisterInfo.h"16#include "MCTargetDesc/ARMBaseInfo.h"17#include "MCTargetDesc/ARMMCTargetDesc.h"18#include "llvm/ADT/DenseMap.h"19#include "llvm/ADT/SmallSet.h"20#include "llvm/CodeGen/MachineBasicBlock.h"21#include "llvm/CodeGen/MachineInstr.h"22#include "llvm/CodeGen/MachineInstrBuilder.h"23#include "llvm/CodeGen/MachineOperand.h"24#include "llvm/CodeGen/MachineRegisterInfo.h"25#include "llvm/CodeGen/Register.h"26#include "llvm/CodeGen/TargetInstrInfo.h"27#include "llvm/IR/IntrinsicInst.h"28#include "llvm/IR/IntrinsicsARM.h"29#include "llvm/Support/ErrorHandling.h"30#include <array>31#include <cstdint>3233#define GET_INSTRINFO_HEADER34#include "ARMGenInstrInfo.inc"3536namespace llvm {3738class ARMBaseRegisterInfo;39class ARMSubtarget;4041class ARMBaseInstrInfo : public ARMGenInstrInfo {42const ARMSubtarget &Subtarget;4344protected:45// Can be only subclassed.46explicit ARMBaseInstrInfo(const ARMSubtarget &STI);4748void expandLoadStackGuardBase(MachineBasicBlock::iterator MI,49unsigned LoadImmOpc, unsigned LoadOpc) const;5051/// Build the equivalent inputs of a REG_SEQUENCE for the given \p MI52/// and \p DefIdx.53/// \p [out] InputRegs of the equivalent REG_SEQUENCE. Each element of54/// the list is modeled as <Reg:SubReg, SubIdx>.55/// E.g., REG_SEQUENCE %1:sub1, sub0, %2, sub1 would produce56/// two elements:57/// - %1:sub1, sub058/// - %2<:0>, sub159///60/// \returns true if it is possible to build such an input sequence61/// with the pair \p MI, \p DefIdx. False otherwise.62///63/// \pre MI.isRegSequenceLike().64bool getRegSequenceLikeInputs(65const MachineInstr &MI, unsigned DefIdx,66SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const override;6768/// Build the equivalent inputs of a EXTRACT_SUBREG for the given \p MI69/// and \p DefIdx.70/// \p [out] InputReg of the equivalent EXTRACT_SUBREG.71/// E.g., EXTRACT_SUBREG %1:sub1, sub0, sub1 would produce:72/// - %1:sub1, sub073///74/// \returns true if it is possible to build such an input sequence75/// with the pair \p MI, \p DefIdx. False otherwise.76///77/// \pre MI.isExtractSubregLike().78bool getExtractSubregLikeInputs(const MachineInstr &MI, unsigned DefIdx,79RegSubRegPairAndIdx &InputReg) const override;8081/// Build the equivalent inputs of a INSERT_SUBREG for the given \p MI82/// and \p DefIdx.83/// \p [out] BaseReg and \p [out] InsertedReg contain84/// the equivalent inputs of INSERT_SUBREG.85/// E.g., INSERT_SUBREG %0:sub0, %1:sub1, sub3 would produce:86/// - BaseReg: %0:sub087/// - InsertedReg: %1:sub1, sub388///89/// \returns true if it is possible to build such an input sequence90/// with the pair \p MI, \p DefIdx. False otherwise.91///92/// \pre MI.isInsertSubregLike().93bool94getInsertSubregLikeInputs(const MachineInstr &MI, unsigned DefIdx,95RegSubRegPair &BaseReg,96RegSubRegPairAndIdx &InsertedReg) const override;9798/// Commutes the operands in the given instruction.99/// The commutable operands are specified by their indices OpIdx1 and OpIdx2.100///101/// Do not call this method for a non-commutable instruction or for102/// non-commutable pair of operand indices OpIdx1 and OpIdx2.103/// Even though the instruction is commutable, the method may still104/// fail to commute the operands, null pointer is returned in such cases.105MachineInstr *commuteInstructionImpl(MachineInstr &MI, bool NewMI,106unsigned OpIdx1,107unsigned OpIdx2) const override;108/// If the specific machine instruction is an instruction that moves/copies109/// value from one register to another register return destination and source110/// registers as machine operands.111std::optional<DestSourcePair>112isCopyInstrImpl(const MachineInstr &MI) const override;113114/// Specialization of \ref TargetInstrInfo::describeLoadedValue, used to115/// enhance debug entry value descriptions for ARM targets.116std::optional<ParamLoadedValue>117describeLoadedValue(const MachineInstr &MI, Register Reg) const override;118119public:120// Return whether the target has an explicit NOP encoding.121bool hasNOP() const;122123// Return the non-pre/post incrementing version of 'Opc'. Return 0124// if there is not such an opcode.125virtual unsigned getUnindexedOpcode(unsigned Opc) const = 0;126127MachineInstr *convertToThreeAddress(MachineInstr &MI, LiveVariables *LV,128LiveIntervals *LIS) const override;129130virtual const ARMBaseRegisterInfo &getRegisterInfo() const = 0;131const ARMSubtarget &getSubtarget() const { return Subtarget; }132133ScheduleHazardRecognizer *134CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,135const ScheduleDAG *DAG) const override;136137ScheduleHazardRecognizer *138CreateTargetMIHazardRecognizer(const InstrItineraryData *II,139const ScheduleDAGMI *DAG) const override;140141ScheduleHazardRecognizer *142CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,143const ScheduleDAG *DAG) const override;144145// Branch analysis.146bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,147MachineBasicBlock *&FBB,148SmallVectorImpl<MachineOperand> &Cond,149bool AllowModify = false) const override;150unsigned removeBranch(MachineBasicBlock &MBB,151int *BytesRemoved = nullptr) const override;152unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,153MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,154const DebugLoc &DL,155int *BytesAdded = nullptr) const override;156157bool158reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;159160// Predication support.161bool isPredicated(const MachineInstr &MI) const override;162163// MIR printer helper function to annotate Operands with a comment.164std::string165createMIROperandComment(const MachineInstr &MI, const MachineOperand &Op,166unsigned OpIdx,167const TargetRegisterInfo *TRI) const override;168169ARMCC::CondCodes getPredicate(const MachineInstr &MI) const {170int PIdx = MI.findFirstPredOperandIdx();171return PIdx != -1 ? (ARMCC::CondCodes)MI.getOperand(PIdx).getImm()172: ARMCC::AL;173}174175bool PredicateInstruction(MachineInstr &MI,176ArrayRef<MachineOperand> Pred) const override;177178bool SubsumesPredicate(ArrayRef<MachineOperand> Pred1,179ArrayRef<MachineOperand> Pred2) const override;180181bool ClobbersPredicate(MachineInstr &MI, std::vector<MachineOperand> &Pred,182bool SkipDead) const override;183184bool isPredicable(const MachineInstr &MI) const override;185186// CPSR defined in instruction187static bool isCPSRDefined(const MachineInstr &MI);188189/// GetInstSize - Returns the size of the specified MachineInstr.190///191unsigned getInstSizeInBytes(const MachineInstr &MI) const override;192193Register isLoadFromStackSlot(const MachineInstr &MI,194int &FrameIndex) const override;195Register isStoreToStackSlot(const MachineInstr &MI,196int &FrameIndex) const override;197Register isLoadFromStackSlotPostFE(const MachineInstr &MI,198int &FrameIndex) const override;199Register isStoreToStackSlotPostFE(const MachineInstr &MI,200int &FrameIndex) const override;201202void copyToCPSR(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,203unsigned SrcReg, bool KillSrc,204const ARMSubtarget &Subtarget) const;205void copyFromCPSR(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,206unsigned DestReg, bool KillSrc,207const ARMSubtarget &Subtarget) const;208209void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,210const DebugLoc &DL, MCRegister DestReg, MCRegister SrcReg,211bool KillSrc) const override;212213void storeRegToStackSlot(MachineBasicBlock &MBB,214MachineBasicBlock::iterator MBBI, Register SrcReg,215bool isKill, int FrameIndex,216const TargetRegisterClass *RC,217const TargetRegisterInfo *TRI,218Register VReg) const override;219220void loadRegFromStackSlot(MachineBasicBlock &MBB,221MachineBasicBlock::iterator MBBI, Register DestReg,222int FrameIndex, const TargetRegisterClass *RC,223const TargetRegisterInfo *TRI,224Register VReg) const override;225226bool expandPostRAPseudo(MachineInstr &MI) const override;227228bool shouldSink(const MachineInstr &MI) const override;229230void reMaterialize(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,231Register DestReg, unsigned SubIdx,232const MachineInstr &Orig,233const TargetRegisterInfo &TRI) const override;234235MachineInstr &236duplicate(MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,237const MachineInstr &Orig) const override;238239const MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB, unsigned Reg,240unsigned SubIdx, unsigned State,241const TargetRegisterInfo *TRI) const;242243bool produceSameValue(const MachineInstr &MI0, const MachineInstr &MI1,244const MachineRegisterInfo *MRI) const override;245246/// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to247/// determine if two loads are loading from the same base address. It should248/// only return true if the base pointers are the same and the only249/// differences between the two addresses is the offset. It also returns the250/// offsets by reference.251bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2, int64_t &Offset1,252int64_t &Offset2) const override;253254/// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to255/// determine (in conjunction with areLoadsFromSameBasePtr) if two loads256/// should be scheduled togther. On some targets if two loads are loading from257/// addresses in the same cache line, it's better if they are scheduled258/// together. This function takes two integers that represent the load offsets259/// from the common base address. It returns true if it decides it's desirable260/// to schedule the two loads together. "NumLoads" is the number of loads that261/// have already been scheduled after Load1.262bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,263int64_t Offset1, int64_t Offset2,264unsigned NumLoads) const override;265266bool isSchedulingBoundary(const MachineInstr &MI,267const MachineBasicBlock *MBB,268const MachineFunction &MF) const override;269270bool isProfitableToIfCvt(MachineBasicBlock &MBB,271unsigned NumCycles, unsigned ExtraPredCycles,272BranchProbability Probability) const override;273274bool isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumT,275unsigned ExtraT, MachineBasicBlock &FMBB,276unsigned NumF, unsigned ExtraF,277BranchProbability Probability) const override;278279bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,280BranchProbability Probability) const override {281return NumCycles == 1;282}283284unsigned extraSizeToPredicateInstructions(const MachineFunction &MF,285unsigned NumInsts) const override;286unsigned predictBranchSizeForIfCvt(MachineInstr &MI) const override;287288bool isProfitableToUnpredicate(MachineBasicBlock &TMBB,289MachineBasicBlock &FMBB) const override;290291/// analyzeCompare - For a comparison instruction, return the source registers292/// in SrcReg and SrcReg2 if having two register operands, and the value it293/// compares against in CmpValue. Return true if the comparison instruction294/// can be analyzed.295bool analyzeCompare(const MachineInstr &MI, Register &SrcReg,296Register &SrcReg2, int64_t &CmpMask,297int64_t &CmpValue) const override;298299/// optimizeCompareInstr - Convert the instruction to set the zero flag so300/// that we can remove a "comparison with zero"; Remove a redundant CMP301/// instruction if the flags can be updated in the same way by an earlier302/// instruction such as SUB.303bool optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,304Register SrcReg2, int64_t CmpMask, int64_t CmpValue,305const MachineRegisterInfo *MRI) const override;306307bool analyzeSelect(const MachineInstr &MI,308SmallVectorImpl<MachineOperand> &Cond, unsigned &TrueOp,309unsigned &FalseOp, bool &Optimizable) const override;310311MachineInstr *optimizeSelect(MachineInstr &MI,312SmallPtrSetImpl<MachineInstr *> &SeenMIs,313bool) const override;314315/// foldImmediate - 'Reg' is known to be defined by a move immediate316/// instruction, try to fold the immediate into the use instruction.317bool foldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, Register Reg,318MachineRegisterInfo *MRI) const override;319320unsigned getNumMicroOps(const InstrItineraryData *ItinData,321const MachineInstr &MI) const override;322323std::optional<unsigned> getOperandLatency(const InstrItineraryData *ItinData,324const MachineInstr &DefMI,325unsigned DefIdx,326const MachineInstr &UseMI,327unsigned UseIdx) const override;328std::optional<unsigned> getOperandLatency(const InstrItineraryData *ItinData,329SDNode *DefNode, unsigned DefIdx,330SDNode *UseNode,331unsigned UseIdx) const override;332333/// VFP/NEON execution domains.334std::pair<uint16_t, uint16_t>335getExecutionDomain(const MachineInstr &MI) const override;336void setExecutionDomain(MachineInstr &MI, unsigned Domain) const override;337338unsigned339getPartialRegUpdateClearance(const MachineInstr &, unsigned,340const TargetRegisterInfo *) const override;341void breakPartialRegDependency(MachineInstr &, unsigned,342const TargetRegisterInfo *TRI) const override;343344/// Get the number of addresses by LDM or VLDM or zero for unknown.345unsigned getNumLDMAddresses(const MachineInstr &MI) const;346347std::pair<unsigned, unsigned>348decomposeMachineOperandsTargetFlags(unsigned TF) const override;349ArrayRef<std::pair<unsigned, const char *>>350getSerializableDirectMachineOperandTargetFlags() const override;351ArrayRef<std::pair<unsigned, const char *>>352getSerializableBitmaskMachineOperandTargetFlags() const override;353354/// ARM supports the MachineOutliner.355bool isFunctionSafeToOutlineFrom(MachineFunction &MF,356bool OutlineFromLinkOnceODRs) const override;357std::optional<outliner::OutlinedFunction> getOutliningCandidateInfo(358std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;359void mergeOutliningCandidateAttributes(360Function &F, std::vector<outliner::Candidate> &Candidates) const override;361outliner::InstrType getOutliningTypeImpl(MachineBasicBlock::iterator &MIT,362unsigned Flags) const override;363bool isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,364unsigned &Flags) const override;365void buildOutlinedFrame(MachineBasicBlock &MBB, MachineFunction &MF,366const outliner::OutlinedFunction &OF) const override;367MachineBasicBlock::iterator368insertOutlinedCall(Module &M, MachineBasicBlock &MBB,369MachineBasicBlock::iterator &It, MachineFunction &MF,370outliner::Candidate &C) const override;371372/// Enable outlining by default at -Oz.373bool shouldOutlineFromFunctionByDefault(MachineFunction &MF) const override;374375bool isUnspillableTerminatorImpl(const MachineInstr *MI) const override {376return MI->getOpcode() == ARM::t2LoopEndDec ||377MI->getOpcode() == ARM::t2DoLoopStartTP ||378MI->getOpcode() == ARM::t2WhileLoopStartLR ||379MI->getOpcode() == ARM::t2WhileLoopStartTP;380}381382/// Analyze loop L, which must be a single-basic-block loop, and if the383/// conditions can be understood enough produce a PipelinerLoopInfo object.384std::unique_ptr<TargetInstrInfo::PipelinerLoopInfo>385analyzeLoopForPipelining(MachineBasicBlock *LoopBB) const override;386387private:388/// Returns an unused general-purpose register which can be used for389/// constructing an outlined call if one exists. Returns 0 otherwise.390Register findRegisterToSaveLRTo(outliner::Candidate &C) const;391392/// Adds an instruction which saves the link register on top of the stack into393/// the MachineBasicBlock \p MBB at position \p It. If \p Auth is true,394/// compute and store an authentication code alongiside the link register.395/// If \p CFI is true, emit CFI instructions.396void saveLROnStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator It,397bool CFI, bool Auth) const;398399/// Adds an instruction which restores the link register from the top the400/// stack into the MachineBasicBlock \p MBB at position \p It. If \p Auth is401/// true, restore an authentication code and authenticate LR.402/// If \p CFI is true, emit CFI instructions.403void restoreLRFromStack(MachineBasicBlock &MBB,404MachineBasicBlock::iterator It, bool CFI,405bool Auth) const;406407/// Emit CFI instructions into the MachineBasicBlock \p MBB at position \p It,408/// for the case when the LR is saved in the register \p Reg.409void emitCFIForLRSaveToReg(MachineBasicBlock &MBB,410MachineBasicBlock::iterator It,411Register Reg) const;412413/// Emit CFI instructions into the MachineBasicBlock \p MBB at position \p It,414/// after the LR is was restored from a register.415void emitCFIForLRRestoreFromReg(MachineBasicBlock &MBB,416MachineBasicBlock::iterator It) const;417/// \brief Sets the offsets on outlined instructions in \p MBB which use SP418/// so that they will be valid post-outlining.419///420/// \param MBB A \p MachineBasicBlock in an outlined function.421void fixupPostOutline(MachineBasicBlock &MBB) const;422423/// Returns true if the machine instruction offset can handle the stack fixup424/// and updates it if requested.425bool checkAndUpdateStackOffset(MachineInstr *MI, int64_t Fixup,426bool Updt) const;427428unsigned getInstBundleLength(const MachineInstr &MI) const;429430std::optional<unsigned> getVLDMDefCycle(const InstrItineraryData *ItinData,431const MCInstrDesc &DefMCID,432unsigned DefClass, unsigned DefIdx,433unsigned DefAlign) const;434std::optional<unsigned> getLDMDefCycle(const InstrItineraryData *ItinData,435const MCInstrDesc &DefMCID,436unsigned DefClass, unsigned DefIdx,437unsigned DefAlign) const;438std::optional<unsigned> getVSTMUseCycle(const InstrItineraryData *ItinData,439const MCInstrDesc &UseMCID,440unsigned UseClass, unsigned UseIdx,441unsigned UseAlign) const;442std::optional<unsigned> getSTMUseCycle(const InstrItineraryData *ItinData,443const MCInstrDesc &UseMCID,444unsigned UseClass, unsigned UseIdx,445unsigned UseAlign) const;446std::optional<unsigned> getOperandLatency(const InstrItineraryData *ItinData,447const MCInstrDesc &DefMCID,448unsigned DefIdx, unsigned DefAlign,449const MCInstrDesc &UseMCID,450unsigned UseIdx,451unsigned UseAlign) const;452453std::optional<unsigned> getOperandLatencyImpl(454const InstrItineraryData *ItinData, const MachineInstr &DefMI,455unsigned DefIdx, const MCInstrDesc &DefMCID, unsigned DefAdj,456const MachineOperand &DefMO, unsigned Reg, const MachineInstr &UseMI,457unsigned UseIdx, const MCInstrDesc &UseMCID, unsigned UseAdj) const;458459unsigned getPredicationCost(const MachineInstr &MI) const override;460461unsigned getInstrLatency(const InstrItineraryData *ItinData,462const MachineInstr &MI,463unsigned *PredCost = nullptr) const override;464465unsigned getInstrLatency(const InstrItineraryData *ItinData,466SDNode *Node) const override;467468bool hasHighOperandLatency(const TargetSchedModel &SchedModel,469const MachineRegisterInfo *MRI,470const MachineInstr &DefMI, unsigned DefIdx,471const MachineInstr &UseMI,472unsigned UseIdx) const override;473bool hasLowDefLatency(const TargetSchedModel &SchedModel,474const MachineInstr &DefMI,475unsigned DefIdx) const override;476477/// verifyInstruction - Perform target specific instruction verification.478bool verifyInstruction(const MachineInstr &MI,479StringRef &ErrInfo) const override;480481virtual void expandLoadStackGuard(MachineBasicBlock::iterator MI) const = 0;482483void expandMEMCPY(MachineBasicBlock::iterator) const;484485/// Identify instructions that can be folded into a MOVCC instruction, and486/// return the defining instruction.487MachineInstr *canFoldIntoMOVCC(Register Reg, const MachineRegisterInfo &MRI,488const TargetInstrInfo *TII) const;489490bool isReallyTriviallyReMaterializable(const MachineInstr &MI) const override;491492private:493/// Modeling special VFP / NEON fp MLA / MLS hazards.494495/// MLxEntryMap - Map fp MLA / MLS to the corresponding entry in the internal496/// MLx table.497DenseMap<unsigned, unsigned> MLxEntryMap;498499/// MLxHazardOpcodes - Set of add / sub and multiply opcodes that would cause500/// stalls when scheduled together with fp MLA / MLS opcodes.501SmallSet<unsigned, 16> MLxHazardOpcodes;502503public:504/// isFpMLxInstruction - Return true if the specified opcode is a fp MLA / MLS505/// instruction.506bool isFpMLxInstruction(unsigned Opcode) const {507return MLxEntryMap.count(Opcode);508}509510/// isFpMLxInstruction - This version also returns the multiply opcode and the511/// addition / subtraction opcode to expand to. Return true for 'HasLane' for512/// the MLX instructions with an extra lane operand.513bool isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,514unsigned &AddSubOpc, bool &NegAcc,515bool &HasLane) const;516517/// canCauseFpMLxStall - Return true if an instruction of the specified opcode518/// will cause stalls when scheduled after (within 4-cycle window) a fp519/// MLA / MLS instruction.520bool canCauseFpMLxStall(unsigned Opcode) const {521return MLxHazardOpcodes.count(Opcode);522}523524/// Returns true if the instruction has a shift by immediate that can be525/// executed in one cycle less.526bool isSwiftFastImmShift(const MachineInstr *MI) const;527528/// Returns predicate register associated with the given frame instruction.529unsigned getFramePred(const MachineInstr &MI) const {530assert(isFrameInstr(MI));531// Operands of ADJCALLSTACKDOWN/ADJCALLSTACKUP:532// - argument declared in the pattern:533// 0 - frame size534// 1 - arg of CALLSEQ_START/CALLSEQ_END535// 2 - predicate code (like ARMCC::AL)536// - added by predOps:537// 3 - predicate reg538return MI.getOperand(3).getReg();539}540541std::optional<RegImmPair> isAddImmediate(const MachineInstr &MI,542Register Reg) const override;543544unsigned getUndefInitOpcode(unsigned RegClassID) const override {545if (RegClassID == ARM::MQPRRegClass.getID())546return ARM::PseudoARMInitUndefMQPR;547if (RegClassID == ARM::SPRRegClass.getID())548return ARM::PseudoARMInitUndefSPR;549if (RegClassID == ARM::DPR_VFP2RegClass.getID())550return ARM::PseudoARMInitUndefDPR_VFP2;551if (RegClassID == ARM::GPRRegClass.getID())552return ARM::PseudoARMInitUndefGPR;553554llvm_unreachable("Unexpected register class.");555}556};557558/// Get the operands corresponding to the given \p Pred value. By default, the559/// predicate register is assumed to be 0 (no register), but you can pass in a560/// \p PredReg if that is not the case.561static inline std::array<MachineOperand, 2> predOps(ARMCC::CondCodes Pred,562unsigned PredReg = 0) {563return {{MachineOperand::CreateImm(static_cast<int64_t>(Pred)),564MachineOperand::CreateReg(PredReg, false)}};565}566567/// Get the operand corresponding to the conditional code result. By default,568/// this is 0 (no register).569static inline MachineOperand condCodeOp(unsigned CCReg = 0) {570return MachineOperand::CreateReg(CCReg, false);571}572573/// Get the operand corresponding to the conditional code result for Thumb1.574/// This operand will always refer to CPSR and it will have the Define flag set.575/// You can optionally set the Dead flag by means of \p isDead.576static inline MachineOperand t1CondCodeOp(bool isDead = false) {577return MachineOperand::CreateReg(ARM::CPSR,578/*Define*/ true, /*Implicit*/ false,579/*Kill*/ false, isDead);580}581582static inline583bool isUncondBranchOpcode(int Opc) {584return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;585}586587// This table shows the VPT instruction variants, i.e. the different588// mask field encodings, see also B5.6. Predication/conditional execution in589// the ArmARM.590static inline bool isVPTOpcode(int Opc) {591return Opc == ARM::MVE_VPTv16i8 || Opc == ARM::MVE_VPTv16u8 ||592Opc == ARM::MVE_VPTv16s8 || Opc == ARM::MVE_VPTv8i16 ||593Opc == ARM::MVE_VPTv8u16 || Opc == ARM::MVE_VPTv8s16 ||594Opc == ARM::MVE_VPTv4i32 || Opc == ARM::MVE_VPTv4u32 ||595Opc == ARM::MVE_VPTv4s32 || Opc == ARM::MVE_VPTv4f32 ||596Opc == ARM::MVE_VPTv8f16 || Opc == ARM::MVE_VPTv16i8r ||597Opc == ARM::MVE_VPTv16u8r || Opc == ARM::MVE_VPTv16s8r ||598Opc == ARM::MVE_VPTv8i16r || Opc == ARM::MVE_VPTv8u16r ||599Opc == ARM::MVE_VPTv8s16r || Opc == ARM::MVE_VPTv4i32r ||600Opc == ARM::MVE_VPTv4u32r || Opc == ARM::MVE_VPTv4s32r ||601Opc == ARM::MVE_VPTv4f32r || Opc == ARM::MVE_VPTv8f16r ||602Opc == ARM::MVE_VPST;603}604605static inline606unsigned VCMPOpcodeToVPT(unsigned Opcode) {607switch (Opcode) {608default:609return 0;610case ARM::MVE_VCMPf32:611return ARM::MVE_VPTv4f32;612case ARM::MVE_VCMPf16:613return ARM::MVE_VPTv8f16;614case ARM::MVE_VCMPi8:615return ARM::MVE_VPTv16i8;616case ARM::MVE_VCMPi16:617return ARM::MVE_VPTv8i16;618case ARM::MVE_VCMPi32:619return ARM::MVE_VPTv4i32;620case ARM::MVE_VCMPu8:621return ARM::MVE_VPTv16u8;622case ARM::MVE_VCMPu16:623return ARM::MVE_VPTv8u16;624case ARM::MVE_VCMPu32:625return ARM::MVE_VPTv4u32;626case ARM::MVE_VCMPs8:627return ARM::MVE_VPTv16s8;628case ARM::MVE_VCMPs16:629return ARM::MVE_VPTv8s16;630case ARM::MVE_VCMPs32:631return ARM::MVE_VPTv4s32;632633case ARM::MVE_VCMPf32r:634return ARM::MVE_VPTv4f32r;635case ARM::MVE_VCMPf16r:636return ARM::MVE_VPTv8f16r;637case ARM::MVE_VCMPi8r:638return ARM::MVE_VPTv16i8r;639case ARM::MVE_VCMPi16r:640return ARM::MVE_VPTv8i16r;641case ARM::MVE_VCMPi32r:642return ARM::MVE_VPTv4i32r;643case ARM::MVE_VCMPu8r:644return ARM::MVE_VPTv16u8r;645case ARM::MVE_VCMPu16r:646return ARM::MVE_VPTv8u16r;647case ARM::MVE_VCMPu32r:648return ARM::MVE_VPTv4u32r;649case ARM::MVE_VCMPs8r:650return ARM::MVE_VPTv16s8r;651case ARM::MVE_VCMPs16r:652return ARM::MVE_VPTv8s16r;653case ARM::MVE_VCMPs32r:654return ARM::MVE_VPTv4s32r;655}656}657658static inline659bool isCondBranchOpcode(int Opc) {660return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;661}662663static inline bool isJumpTableBranchOpcode(int Opc) {664return Opc == ARM::BR_JTr || Opc == ARM::BR_JTm_i12 ||665Opc == ARM::BR_JTm_rs || Opc == ARM::BR_JTadd || Opc == ARM::tBR_JTr ||666Opc == ARM::t2BR_JT;667}668669static inline670bool isIndirectBranchOpcode(int Opc) {671return Opc == ARM::BX || Opc == ARM::MOVPCRX || Opc == ARM::tBRIND;672}673674static inline bool isIndirectCall(const MachineInstr &MI) {675int Opc = MI.getOpcode();676switch (Opc) {677// indirect calls:678case ARM::BLX:679case ARM::BLX_noip:680case ARM::BLX_pred:681case ARM::BLX_pred_noip:682case ARM::BX_CALL:683case ARM::BMOVPCRX_CALL:684case ARM::TCRETURNri:685case ARM::TCRETURNrinotr12:686case ARM::TAILJMPr:687case ARM::TAILJMPr4:688case ARM::tBLXr:689case ARM::tBLXr_noip:690case ARM::tBLXNSr:691case ARM::tBLXNS_CALL:692case ARM::tBX_CALL:693case ARM::tTAILJMPr:694assert(MI.isCall(MachineInstr::IgnoreBundle));695return true;696// direct calls:697case ARM::BL:698case ARM::BL_pred:699case ARM::BMOVPCB_CALL:700case ARM::BL_PUSHLR:701case ARM::BLXi:702case ARM::TCRETURNdi:703case ARM::TAILJMPd:704case ARM::SVC:705case ARM::HVC:706case ARM::TPsoft:707case ARM::tTAILJMPd:708case ARM::t2SMC:709case ARM::t2HVC:710case ARM::tBL:711case ARM::tBLXi:712case ARM::tBL_PUSHLR:713case ARM::tTAILJMPdND:714case ARM::tSVC:715case ARM::tTPsoft:716assert(MI.isCall(MachineInstr::IgnoreBundle));717return false;718}719assert(!MI.isCall(MachineInstr::IgnoreBundle));720return false;721}722723static inline bool isIndirectControlFlowNotComingBack(const MachineInstr &MI) {724int opc = MI.getOpcode();725return MI.isReturn() || isIndirectBranchOpcode(MI.getOpcode()) ||726isJumpTableBranchOpcode(opc);727}728729static inline bool isSpeculationBarrierEndBBOpcode(int Opc) {730return Opc == ARM::SpeculationBarrierISBDSBEndBB ||731Opc == ARM::SpeculationBarrierSBEndBB ||732Opc == ARM::t2SpeculationBarrierISBDSBEndBB ||733Opc == ARM::t2SpeculationBarrierSBEndBB;734}735736static inline bool isPopOpcode(int Opc) {737return Opc == ARM::tPOP_RET || Opc == ARM::LDMIA_RET ||738Opc == ARM::t2LDMIA_RET || Opc == ARM::tPOP || Opc == ARM::LDMIA_UPD ||739Opc == ARM::t2LDMIA_UPD || Opc == ARM::VLDMDIA_UPD;740}741742static inline bool isPushOpcode(int Opc) {743return Opc == ARM::tPUSH || Opc == ARM::t2STMDB_UPD ||744Opc == ARM::STMDB_UPD || Opc == ARM::VSTMDDB_UPD;745}746747static inline bool isSubImmOpcode(int Opc) {748return Opc == ARM::SUBri ||749Opc == ARM::tSUBi3 || Opc == ARM::tSUBi8 ||750Opc == ARM::tSUBSi3 || Opc == ARM::tSUBSi8 ||751Opc == ARM::t2SUBri || Opc == ARM::t2SUBri12 || Opc == ARM::t2SUBSri;752}753754static inline bool isMovRegOpcode(int Opc) {755return Opc == ARM::MOVr || Opc == ARM::tMOVr || Opc == ARM::t2MOVr;756}757/// isValidCoprocessorNumber - decide whether an explicit coprocessor758/// number is legal in generic instructions like CDP. The answer can759/// vary with the subtarget.760static inline bool isValidCoprocessorNumber(unsigned Num,761const FeatureBitset& featureBits) {762// In Armv7 and Armv8-M CP10 and CP11 clash with VFP/NEON, however, the763// coprocessor is still valid for CDP/MCR/MRC and friends. Allowing it is764// useful for code which is shared with older architectures which do not know765// the new VFP/NEON mnemonics.766767// Armv8-A disallows everything *other* than 111x (CP14 and CP15).768if (featureBits[ARM::HasV8Ops] && (Num & 0xE) != 0xE)769return false;770771// Armv8.1-M disallows 100x (CP8,CP9) and 111x (CP14,CP15)772// which clash with MVE.773if (featureBits[ARM::HasV8_1MMainlineOps] &&774((Num & 0xE) == 0x8 || (Num & 0xE) == 0xE))775return false;776777return true;778}779780static inline bool isSEHInstruction(const MachineInstr &MI) {781unsigned Opc = MI.getOpcode();782switch (Opc) {783case ARM::SEH_StackAlloc:784case ARM::SEH_SaveRegs:785case ARM::SEH_SaveRegs_Ret:786case ARM::SEH_SaveSP:787case ARM::SEH_SaveFRegs:788case ARM::SEH_SaveLR:789case ARM::SEH_Nop:790case ARM::SEH_Nop_Ret:791case ARM::SEH_PrologEnd:792case ARM::SEH_EpilogStart:793case ARM::SEH_EpilogEnd:794return true;795default:796return false;797}798}799800/// getInstrPredicate - If instruction is predicated, returns its predicate801/// condition, otherwise returns AL. It also returns the condition code802/// register by reference.803ARMCC::CondCodes getInstrPredicate(const MachineInstr &MI, Register &PredReg);804805unsigned getMatchingCondBranchOpcode(unsigned Opc);806807/// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether808/// the instruction is encoded with an 'S' bit is determined by the optional809/// CPSR def operand.810unsigned convertAddSubFlagsOpcode(unsigned OldOpc);811812/// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of813/// instructions to materializea destreg = basereg + immediate in ARM / Thumb2814/// code.815void emitARMRegPlusImmediate(MachineBasicBlock &MBB,816MachineBasicBlock::iterator &MBBI,817const DebugLoc &dl, Register DestReg,818Register BaseReg, int NumBytes,819ARMCC::CondCodes Pred, Register PredReg,820const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);821822void emitT2RegPlusImmediate(MachineBasicBlock &MBB,823MachineBasicBlock::iterator &MBBI,824const DebugLoc &dl, Register DestReg,825Register BaseReg, int NumBytes,826ARMCC::CondCodes Pred, Register PredReg,827const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);828void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,829MachineBasicBlock::iterator &MBBI,830const DebugLoc &dl, Register DestReg,831Register BaseReg, int NumBytes,832const TargetInstrInfo &TII,833const ARMBaseRegisterInfo &MRI,834unsigned MIFlags = 0);835836/// Tries to add registers to the reglist of a given base-updating837/// push/pop instruction to adjust the stack by an additional838/// NumBytes. This can save a few bytes per function in code-size, but839/// obviously generates more memory traffic. As such, it only takes840/// effect in functions being optimised for size.841bool tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,842MachineFunction &MF, MachineInstr *MI,843unsigned NumBytes);844845/// rewriteARMFrameIndex / rewriteT2FrameIndex -846/// Rewrite MI to access 'Offset' bytes from the FP. Return false if the847/// offset could not be handled directly in MI, and return the left-over848/// portion by reference.849bool rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,850Register FrameReg, int &Offset,851const ARMBaseInstrInfo &TII);852853bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,854Register FrameReg, int &Offset,855const ARMBaseInstrInfo &TII,856const TargetRegisterInfo *TRI);857858/// Return true if Reg is defd between From and To859bool registerDefinedBetween(unsigned Reg, MachineBasicBlock::iterator From,860MachineBasicBlock::iterator To,861const TargetRegisterInfo *TRI);862863/// Search backwards from a tBcc to find a tCMPi8 against 0, meaning864/// we can convert them to a tCBZ or tCBNZ. Return nullptr if not found.865MachineInstr *findCMPToFoldIntoCBZ(MachineInstr *Br,866const TargetRegisterInfo *TRI);867868void addUnpredicatedMveVpredNOp(MachineInstrBuilder &MIB);869void addUnpredicatedMveVpredROp(MachineInstrBuilder &MIB, Register DestReg);870871void addPredicatedMveVpredNOp(MachineInstrBuilder &MIB, unsigned Cond);872void addPredicatedMveVpredROp(MachineInstrBuilder &MIB, unsigned Cond,873unsigned Inactive);874875/// Returns the number of instructions required to materialize the given876/// constant in a register, or 3 if a literal pool load is needed.877/// If ForCodesize is specified, an approximate cost in bytes is returned.878unsigned ConstantMaterializationCost(unsigned Val,879const ARMSubtarget *Subtarget,880bool ForCodesize = false);881882/// Returns true if Val1 has a lower Constant Materialization Cost than Val2.883/// Uses the cost from ConstantMaterializationCost, first with ForCodesize as884/// specified. If the scores are equal, return the comparison for !ForCodesize.885bool HasLowerConstantMaterializationCost(unsigned Val1, unsigned Val2,886const ARMSubtarget *Subtarget,887bool ForCodesize = false);888889// Return the immediate if this is ADDri or SUBri, scaled as appropriate.890// Returns 0 for unknown instructions.891inline int getAddSubImmediate(MachineInstr &MI) {892int Scale = 1;893unsigned ImmOp;894switch (MI.getOpcode()) {895case ARM::t2ADDri:896ImmOp = 2;897break;898case ARM::t2SUBri:899case ARM::t2SUBri12:900ImmOp = 2;901Scale = -1;902break;903case ARM::tSUBi3:904case ARM::tSUBi8:905ImmOp = 3;906Scale = -1;907break;908default:909return 0;910}911return Scale * MI.getOperand(ImmOp).getImm();912}913914// Given a memory access Opcode, check that the give Imm would be a valid Offset915// for this instruction using its addressing mode.916inline bool isLegalAddressImm(unsigned Opcode, int Imm,917const TargetInstrInfo *TII) {918const MCInstrDesc &Desc = TII->get(Opcode);919unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);920switch (AddrMode) {921case ARMII::AddrModeT2_i7:922return std::abs(Imm) < ((1 << 7) * 1);923case ARMII::AddrModeT2_i7s2:924return std::abs(Imm) < ((1 << 7) * 2) && Imm % 2 == 0;925case ARMII::AddrModeT2_i7s4:926return std::abs(Imm) < ((1 << 7) * 4) && Imm % 4 == 0;927case ARMII::AddrModeT2_i8:928return std::abs(Imm) < ((1 << 8) * 1);929case ARMII::AddrModeT2_i8pos:930return Imm >= 0 && Imm < ((1 << 8) * 1);931case ARMII::AddrModeT2_i8neg:932return Imm < 0 && -Imm < ((1 << 8) * 1);933case ARMII::AddrModeT2_i8s4:934return std::abs(Imm) < ((1 << 8) * 4) && Imm % 4 == 0;935case ARMII::AddrModeT2_i12:936return Imm >= 0 && Imm < ((1 << 12) * 1);937case ARMII::AddrMode2:938return std::abs(Imm) < ((1 << 12) * 1);939default:940llvm_unreachable("Unhandled Addressing mode");941}942}943944// Return true if the given intrinsic is a gather945inline bool isGather(IntrinsicInst *IntInst) {946if (IntInst == nullptr)947return false;948unsigned IntrinsicID = IntInst->getIntrinsicID();949return (IntrinsicID == Intrinsic::masked_gather ||950IntrinsicID == Intrinsic::arm_mve_vldr_gather_base ||951IntrinsicID == Intrinsic::arm_mve_vldr_gather_base_predicated ||952IntrinsicID == Intrinsic::arm_mve_vldr_gather_base_wb ||953IntrinsicID == Intrinsic::arm_mve_vldr_gather_base_wb_predicated ||954IntrinsicID == Intrinsic::arm_mve_vldr_gather_offset ||955IntrinsicID == Intrinsic::arm_mve_vldr_gather_offset_predicated);956}957958// Return true if the given intrinsic is a scatter959inline bool isScatter(IntrinsicInst *IntInst) {960if (IntInst == nullptr)961return false;962unsigned IntrinsicID = IntInst->getIntrinsicID();963return (IntrinsicID == Intrinsic::masked_scatter ||964IntrinsicID == Intrinsic::arm_mve_vstr_scatter_base ||965IntrinsicID == Intrinsic::arm_mve_vstr_scatter_base_predicated ||966IntrinsicID == Intrinsic::arm_mve_vstr_scatter_base_wb ||967IntrinsicID == Intrinsic::arm_mve_vstr_scatter_base_wb_predicated ||968IntrinsicID == Intrinsic::arm_mve_vstr_scatter_offset ||969IntrinsicID == Intrinsic::arm_mve_vstr_scatter_offset_predicated);970}971972// Return true if the given intrinsic is a gather or scatter973inline bool isGatherScatter(IntrinsicInst *IntInst) {974if (IntInst == nullptr)975return false;976return isGather(IntInst) || isScatter(IntInst);977}978979unsigned getBLXOpcode(const MachineFunction &MF);980unsigned gettBLXrOpcode(const MachineFunction &MF);981unsigned getBLXpredOpcode(const MachineFunction &MF);982983} // end namespace llvm984985#endif // LLVM_LIB_TARGET_ARM_ARMBASEINSTRINFO_H986987988