ARMBaseInstrInfo.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. //===-- ARMBaseInstrInfo.h - ARM Base Instruction Information ---*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains the Base ARM implementation of the TargetInstrInfo class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_TARGET_ARM_ARMBASEINSTRINFO_H
  13. #define LLVM_LIB_TARGET_ARM_ARMBASEINSTRINFO_H
  14. #include "MCTargetDesc/ARMBaseInfo.h"
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/SmallSet.h"
  17. #include "llvm/CodeGen/MachineBasicBlock.h"
  18. #include "llvm/CodeGen/MachineInstr.h"
  19. #include "llvm/CodeGen/MachineInstrBuilder.h"
  20. #include "llvm/CodeGen/MachineOperand.h"
  21. #include "llvm/CodeGen/TargetInstrInfo.h"
  22. #include "llvm/IR/IntrinsicInst.h"
  23. #include "llvm/IR/IntrinsicsARM.h"
  24. #include <array>
  25. #include <cstdint>
  26. #define GET_INSTRINFO_HEADER
  27. #include "ARMGenInstrInfo.inc"
  28. namespace llvm {
  29. class ARMBaseRegisterInfo;
  30. class ARMSubtarget;
  31. class ARMBaseInstrInfo : public ARMGenInstrInfo {
  32. const ARMSubtarget &Subtarget;
  33. protected:
  34. // Can be only subclassed.
  35. explicit ARMBaseInstrInfo(const ARMSubtarget &STI);
  36. void expandLoadStackGuardBase(MachineBasicBlock::iterator MI,
  37. unsigned LoadImmOpc, unsigned LoadOpc) const;
  38. /// Build the equivalent inputs of a REG_SEQUENCE for the given \p MI
  39. /// and \p DefIdx.
  40. /// \p [out] InputRegs of the equivalent REG_SEQUENCE. Each element of
  41. /// the list is modeled as <Reg:SubReg, SubIdx>.
  42. /// E.g., REG_SEQUENCE %1:sub1, sub0, %2, sub1 would produce
  43. /// two elements:
  44. /// - %1:sub1, sub0
  45. /// - %2<:0>, sub1
  46. ///
  47. /// \returns true if it is possible to build such an input sequence
  48. /// with the pair \p MI, \p DefIdx. False otherwise.
  49. ///
  50. /// \pre MI.isRegSequenceLike().
  51. bool getRegSequenceLikeInputs(
  52. const MachineInstr &MI, unsigned DefIdx,
  53. SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const override;
  54. /// Build the equivalent inputs of a EXTRACT_SUBREG for the given \p MI
  55. /// and \p DefIdx.
  56. /// \p [out] InputReg of the equivalent EXTRACT_SUBREG.
  57. /// E.g., EXTRACT_SUBREG %1:sub1, sub0, sub1 would produce:
  58. /// - %1:sub1, sub0
  59. ///
  60. /// \returns true if it is possible to build such an input sequence
  61. /// with the pair \p MI, \p DefIdx. False otherwise.
  62. ///
  63. /// \pre MI.isExtractSubregLike().
  64. bool getExtractSubregLikeInputs(const MachineInstr &MI, unsigned DefIdx,
  65. RegSubRegPairAndIdx &InputReg) const override;
  66. /// Build the equivalent inputs of a INSERT_SUBREG for the given \p MI
  67. /// and \p DefIdx.
  68. /// \p [out] BaseReg and \p [out] InsertedReg contain
  69. /// the equivalent inputs of INSERT_SUBREG.
  70. /// E.g., INSERT_SUBREG %0:sub0, %1:sub1, sub3 would produce:
  71. /// - BaseReg: %0:sub0
  72. /// - InsertedReg: %1:sub1, sub3
  73. ///
  74. /// \returns true if it is possible to build such an input sequence
  75. /// with the pair \p MI, \p DefIdx. False otherwise.
  76. ///
  77. /// \pre MI.isInsertSubregLike().
  78. bool
  79. getInsertSubregLikeInputs(const MachineInstr &MI, unsigned DefIdx,
  80. RegSubRegPair &BaseReg,
  81. RegSubRegPairAndIdx &InsertedReg) const override;
  82. /// Commutes the operands in the given instruction.
  83. /// The commutable operands are specified by their indices OpIdx1 and OpIdx2.
  84. ///
  85. /// Do not call this method for a non-commutable instruction or for
  86. /// non-commutable pair of operand indices OpIdx1 and OpIdx2.
  87. /// Even though the instruction is commutable, the method may still
  88. /// fail to commute the operands, null pointer is returned in such cases.
  89. MachineInstr *commuteInstructionImpl(MachineInstr &MI, bool NewMI,
  90. unsigned OpIdx1,
  91. unsigned OpIdx2) const override;
  92. /// If the specific machine instruction is an instruction that moves/copies
  93. /// value from one register to another register return destination and source
  94. /// registers as machine operands.
  95. Optional<DestSourcePair>
  96. isCopyInstrImpl(const MachineInstr &MI) const override;
  97. /// Specialization of \ref TargetInstrInfo::describeLoadedValue, used to
  98. /// enhance debug entry value descriptions for ARM targets.
  99. Optional<ParamLoadedValue> describeLoadedValue(const MachineInstr &MI,
  100. Register Reg) const override;
  101. public:
  102. // Return whether the target has an explicit NOP encoding.
  103. bool hasNOP() const;
  104. // Return the non-pre/post incrementing version of 'Opc'. Return 0
  105. // if there is not such an opcode.
  106. virtual unsigned getUnindexedOpcode(unsigned Opc) const = 0;
  107. MachineInstr *convertToThreeAddress(MachineInstr &MI, LiveVariables *LV,
  108. LiveIntervals *LIS) const override;
  109. virtual const ARMBaseRegisterInfo &getRegisterInfo() const = 0;
  110. const ARMSubtarget &getSubtarget() const { return Subtarget; }
  111. ScheduleHazardRecognizer *
  112. CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
  113. const ScheduleDAG *DAG) const override;
  114. ScheduleHazardRecognizer *
  115. CreateTargetMIHazardRecognizer(const InstrItineraryData *II,
  116. const ScheduleDAGMI *DAG) const override;
  117. ScheduleHazardRecognizer *
  118. CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
  119. const ScheduleDAG *DAG) const override;
  120. // Branch analysis.
  121. bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
  122. MachineBasicBlock *&FBB,
  123. SmallVectorImpl<MachineOperand> &Cond,
  124. bool AllowModify = false) const override;
  125. unsigned removeBranch(MachineBasicBlock &MBB,
  126. int *BytesRemoved = nullptr) const override;
  127. unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
  128. MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
  129. const DebugLoc &DL,
  130. int *BytesAdded = nullptr) const override;
  131. bool
  132. reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
  133. // Predication support.
  134. bool isPredicated(const MachineInstr &MI) const override;
  135. // MIR printer helper function to annotate Operands with a comment.
  136. std::string
  137. createMIROperandComment(const MachineInstr &MI, const MachineOperand &Op,
  138. unsigned OpIdx,
  139. const TargetRegisterInfo *TRI) const override;
  140. ARMCC::CondCodes getPredicate(const MachineInstr &MI) const {
  141. int PIdx = MI.findFirstPredOperandIdx();
  142. return PIdx != -1 ? (ARMCC::CondCodes)MI.getOperand(PIdx).getImm()
  143. : ARMCC::AL;
  144. }
  145. bool PredicateInstruction(MachineInstr &MI,
  146. ArrayRef<MachineOperand> Pred) const override;
  147. bool SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
  148. ArrayRef<MachineOperand> Pred2) const override;
  149. bool ClobbersPredicate(MachineInstr &MI, std::vector<MachineOperand> &Pred,
  150. bool SkipDead) const override;
  151. bool isPredicable(const MachineInstr &MI) const override;
  152. // CPSR defined in instruction
  153. static bool isCPSRDefined(const MachineInstr &MI);
  154. /// GetInstSize - Returns the size of the specified MachineInstr.
  155. ///
  156. unsigned getInstSizeInBytes(const MachineInstr &MI) const override;
  157. unsigned isLoadFromStackSlot(const MachineInstr &MI,
  158. int &FrameIndex) const override;
  159. unsigned isStoreToStackSlot(const MachineInstr &MI,
  160. int &FrameIndex) const override;
  161. unsigned isLoadFromStackSlotPostFE(const MachineInstr &MI,
  162. int &FrameIndex) const override;
  163. unsigned isStoreToStackSlotPostFE(const MachineInstr &MI,
  164. int &FrameIndex) const override;
  165. void copyToCPSR(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
  166. unsigned SrcReg, bool KillSrc,
  167. const ARMSubtarget &Subtarget) const;
  168. void copyFromCPSR(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
  169. unsigned DestReg, bool KillSrc,
  170. const ARMSubtarget &Subtarget) const;
  171. void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
  172. const DebugLoc &DL, MCRegister DestReg, MCRegister SrcReg,
  173. bool KillSrc) const override;
  174. void storeRegToStackSlot(MachineBasicBlock &MBB,
  175. MachineBasicBlock::iterator MBBI,
  176. Register SrcReg, bool isKill, int FrameIndex,
  177. const TargetRegisterClass *RC,
  178. const TargetRegisterInfo *TRI) const override;
  179. void loadRegFromStackSlot(MachineBasicBlock &MBB,
  180. MachineBasicBlock::iterator MBBI,
  181. Register DestReg, int FrameIndex,
  182. const TargetRegisterClass *RC,
  183. const TargetRegisterInfo *TRI) const override;
  184. bool expandPostRAPseudo(MachineInstr &MI) const override;
  185. bool shouldSink(const MachineInstr &MI) const override;
  186. void reMaterialize(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
  187. Register DestReg, unsigned SubIdx,
  188. const MachineInstr &Orig,
  189. const TargetRegisterInfo &TRI) const override;
  190. MachineInstr &
  191. duplicate(MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore,
  192. const MachineInstr &Orig) const override;
  193. const MachineInstrBuilder &AddDReg(MachineInstrBuilder &MIB, unsigned Reg,
  194. unsigned SubIdx, unsigned State,
  195. const TargetRegisterInfo *TRI) const;
  196. bool produceSameValue(const MachineInstr &MI0, const MachineInstr &MI1,
  197. const MachineRegisterInfo *MRI) const override;
  198. /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler to
  199. /// determine if two loads are loading from the same base address. It should
  200. /// only return true if the base pointers are the same and the only
  201. /// differences between the two addresses is the offset. It also returns the
  202. /// offsets by reference.
  203. bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2, int64_t &Offset1,
  204. int64_t &Offset2) const override;
  205. /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
  206. /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads
  207. /// should be scheduled togther. On some targets if two loads are loading from
  208. /// addresses in the same cache line, it's better if they are scheduled
  209. /// together. This function takes two integers that represent the load offsets
  210. /// from the common base address. It returns true if it decides it's desirable
  211. /// to schedule the two loads together. "NumLoads" is the number of loads that
  212. /// have already been scheduled after Load1.
  213. bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
  214. int64_t Offset1, int64_t Offset2,
  215. unsigned NumLoads) const override;
  216. bool isSchedulingBoundary(const MachineInstr &MI,
  217. const MachineBasicBlock *MBB,
  218. const MachineFunction &MF) const override;
  219. bool isProfitableToIfCvt(MachineBasicBlock &MBB,
  220. unsigned NumCycles, unsigned ExtraPredCycles,
  221. BranchProbability Probability) const override;
  222. bool isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumT,
  223. unsigned ExtraT, MachineBasicBlock &FMBB,
  224. unsigned NumF, unsigned ExtraF,
  225. BranchProbability Probability) const override;
  226. bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
  227. BranchProbability Probability) const override {
  228. return NumCycles == 1;
  229. }
  230. unsigned extraSizeToPredicateInstructions(const MachineFunction &MF,
  231. unsigned NumInsts) const override;
  232. unsigned predictBranchSizeForIfCvt(MachineInstr &MI) const override;
  233. bool isProfitableToUnpredicate(MachineBasicBlock &TMBB,
  234. MachineBasicBlock &FMBB) const override;
  235. /// analyzeCompare - For a comparison instruction, return the source registers
  236. /// in SrcReg and SrcReg2 if having two register operands, and the value it
  237. /// compares against in CmpValue. Return true if the comparison instruction
  238. /// can be analyzed.
  239. bool analyzeCompare(const MachineInstr &MI, Register &SrcReg,
  240. Register &SrcReg2, int64_t &CmpMask,
  241. int64_t &CmpValue) const override;
  242. /// optimizeCompareInstr - Convert the instruction to set the zero flag so
  243. /// that we can remove a "comparison with zero"; Remove a redundant CMP
  244. /// instruction if the flags can be updated in the same way by an earlier
  245. /// instruction such as SUB.
  246. bool optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
  247. Register SrcReg2, int64_t CmpMask, int64_t CmpValue,
  248. const MachineRegisterInfo *MRI) const override;
  249. bool analyzeSelect(const MachineInstr &MI,
  250. SmallVectorImpl<MachineOperand> &Cond, unsigned &TrueOp,
  251. unsigned &FalseOp, bool &Optimizable) const override;
  252. MachineInstr *optimizeSelect(MachineInstr &MI,
  253. SmallPtrSetImpl<MachineInstr *> &SeenMIs,
  254. bool) const override;
  255. /// FoldImmediate - 'Reg' is known to be defined by a move immediate
  256. /// instruction, try to fold the immediate into the use instruction.
  257. bool FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, Register Reg,
  258. MachineRegisterInfo *MRI) const override;
  259. unsigned getNumMicroOps(const InstrItineraryData *ItinData,
  260. const MachineInstr &MI) const override;
  261. int getOperandLatency(const InstrItineraryData *ItinData,
  262. const MachineInstr &DefMI, unsigned DefIdx,
  263. const MachineInstr &UseMI,
  264. unsigned UseIdx) const override;
  265. int getOperandLatency(const InstrItineraryData *ItinData,
  266. SDNode *DefNode, unsigned DefIdx,
  267. SDNode *UseNode, unsigned UseIdx) const override;
  268. /// VFP/NEON execution domains.
  269. std::pair<uint16_t, uint16_t>
  270. getExecutionDomain(const MachineInstr &MI) const override;
  271. void setExecutionDomain(MachineInstr &MI, unsigned Domain) const override;
  272. unsigned
  273. getPartialRegUpdateClearance(const MachineInstr &, unsigned,
  274. const TargetRegisterInfo *) const override;
  275. void breakPartialRegDependency(MachineInstr &, unsigned,
  276. const TargetRegisterInfo *TRI) const override;
  277. /// Get the number of addresses by LDM or VLDM or zero for unknown.
  278. unsigned getNumLDMAddresses(const MachineInstr &MI) const;
  279. std::pair<unsigned, unsigned>
  280. decomposeMachineOperandsTargetFlags(unsigned TF) const override;
  281. ArrayRef<std::pair<unsigned, const char *>>
  282. getSerializableDirectMachineOperandTargetFlags() const override;
  283. ArrayRef<std::pair<unsigned, const char *>>
  284. getSerializableBitmaskMachineOperandTargetFlags() const override;
  285. /// ARM supports the MachineOutliner.
  286. bool isFunctionSafeToOutlineFrom(MachineFunction &MF,
  287. bool OutlineFromLinkOnceODRs) const override;
  288. outliner::OutlinedFunction getOutliningCandidateInfo(
  289. std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;
  290. void mergeOutliningCandidateAttributes(
  291. Function &F, std::vector<outliner::Candidate> &Candidates) const override;
  292. outliner::InstrType getOutliningType(MachineBasicBlock::iterator &MIT,
  293. unsigned Flags) const override;
  294. bool isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
  295. unsigned &Flags) const override;
  296. void buildOutlinedFrame(MachineBasicBlock &MBB, MachineFunction &MF,
  297. const outliner::OutlinedFunction &OF) const override;
  298. MachineBasicBlock::iterator
  299. insertOutlinedCall(Module &M, MachineBasicBlock &MBB,
  300. MachineBasicBlock::iterator &It, MachineFunction &MF,
  301. const outliner::Candidate &C) const override;
  302. /// Enable outlining by default at -Oz.
  303. bool shouldOutlineFromFunctionByDefault(MachineFunction &MF) const override;
  304. bool isUnspillableTerminatorImpl(const MachineInstr *MI) const override {
  305. return MI->getOpcode() == ARM::t2LoopEndDec ||
  306. MI->getOpcode() == ARM::t2DoLoopStartTP ||
  307. MI->getOpcode() == ARM::t2WhileLoopStartLR ||
  308. MI->getOpcode() == ARM::t2WhileLoopStartTP;
  309. }
  310. private:
  311. /// Returns an unused general-purpose register which can be used for
  312. /// constructing an outlined call if one exists. Returns 0 otherwise.
  313. unsigned findRegisterToSaveLRTo(const outliner::Candidate &C) const;
  314. /// Adds an instruction which saves the link register on top of the stack into
  315. /// the MachineBasicBlock \p MBB at position \p It. If \p Auth is true,
  316. /// compute and store an authentication code alongiside the link register.
  317. /// If \p CFI is true, emit CFI instructions.
  318. void saveLROnStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator It,
  319. bool CFI, bool Auth) const;
  320. /// Adds an instruction which restores the link register from the top the
  321. /// stack into the MachineBasicBlock \p MBB at position \p It. If \p Auth is
  322. /// true, restore an authentication code and authenticate LR.
  323. /// If \p CFI is true, emit CFI instructions.
  324. void restoreLRFromStack(MachineBasicBlock &MBB,
  325. MachineBasicBlock::iterator It, bool CFI,
  326. bool Auth) const;
  327. /// Emit CFI instructions into the MachineBasicBlock \p MBB at position \p It,
  328. /// for the case when the LR is saved in the register \p Reg.
  329. void emitCFIForLRSaveToReg(MachineBasicBlock &MBB,
  330. MachineBasicBlock::iterator It,
  331. Register Reg) const;
  332. /// Emit CFI instructions into the MachineBasicBlock \p MBB at position \p It,
  333. /// after the LR is was restored from a register.
  334. void emitCFIForLRRestoreFromReg(MachineBasicBlock &MBB,
  335. MachineBasicBlock::iterator It) const;
  336. /// \brief Sets the offsets on outlined instructions in \p MBB which use SP
  337. /// so that they will be valid post-outlining.
  338. ///
  339. /// \param MBB A \p MachineBasicBlock in an outlined function.
  340. void fixupPostOutline(MachineBasicBlock &MBB) const;
  341. /// Returns true if the machine instruction offset can handle the stack fixup
  342. /// and updates it if requested.
  343. bool checkAndUpdateStackOffset(MachineInstr *MI, int64_t Fixup,
  344. bool Updt) const;
  345. unsigned getInstBundleLength(const MachineInstr &MI) const;
  346. int getVLDMDefCycle(const InstrItineraryData *ItinData,
  347. const MCInstrDesc &DefMCID,
  348. unsigned DefClass,
  349. unsigned DefIdx, unsigned DefAlign) const;
  350. int getLDMDefCycle(const InstrItineraryData *ItinData,
  351. const MCInstrDesc &DefMCID,
  352. unsigned DefClass,
  353. unsigned DefIdx, unsigned DefAlign) const;
  354. int getVSTMUseCycle(const InstrItineraryData *ItinData,
  355. const MCInstrDesc &UseMCID,
  356. unsigned UseClass,
  357. unsigned UseIdx, unsigned UseAlign) const;
  358. int getSTMUseCycle(const InstrItineraryData *ItinData,
  359. const MCInstrDesc &UseMCID,
  360. unsigned UseClass,
  361. unsigned UseIdx, unsigned UseAlign) const;
  362. int getOperandLatency(const InstrItineraryData *ItinData,
  363. const MCInstrDesc &DefMCID,
  364. unsigned DefIdx, unsigned DefAlign,
  365. const MCInstrDesc &UseMCID,
  366. unsigned UseIdx, unsigned UseAlign) const;
  367. int getOperandLatencyImpl(const InstrItineraryData *ItinData,
  368. const MachineInstr &DefMI, unsigned DefIdx,
  369. const MCInstrDesc &DefMCID, unsigned DefAdj,
  370. const MachineOperand &DefMO, unsigned Reg,
  371. const MachineInstr &UseMI, unsigned UseIdx,
  372. const MCInstrDesc &UseMCID, unsigned UseAdj) const;
  373. unsigned getPredicationCost(const MachineInstr &MI) const override;
  374. unsigned getInstrLatency(const InstrItineraryData *ItinData,
  375. const MachineInstr &MI,
  376. unsigned *PredCost = nullptr) const override;
  377. int getInstrLatency(const InstrItineraryData *ItinData,
  378. SDNode *Node) const override;
  379. bool hasHighOperandLatency(const TargetSchedModel &SchedModel,
  380. const MachineRegisterInfo *MRI,
  381. const MachineInstr &DefMI, unsigned DefIdx,
  382. const MachineInstr &UseMI,
  383. unsigned UseIdx) const override;
  384. bool hasLowDefLatency(const TargetSchedModel &SchedModel,
  385. const MachineInstr &DefMI,
  386. unsigned DefIdx) const override;
  387. /// verifyInstruction - Perform target specific instruction verification.
  388. bool verifyInstruction(const MachineInstr &MI,
  389. StringRef &ErrInfo) const override;
  390. virtual void expandLoadStackGuard(MachineBasicBlock::iterator MI) const = 0;
  391. void expandMEMCPY(MachineBasicBlock::iterator) const;
  392. /// Identify instructions that can be folded into a MOVCC instruction, and
  393. /// return the defining instruction.
  394. MachineInstr *canFoldIntoMOVCC(Register Reg, const MachineRegisterInfo &MRI,
  395. const TargetInstrInfo *TII) const;
  396. bool isReallyTriviallyReMaterializable(const MachineInstr &MI,
  397. AAResults *AA) const override;
  398. private:
  399. /// Modeling special VFP / NEON fp MLA / MLS hazards.
  400. /// MLxEntryMap - Map fp MLA / MLS to the corresponding entry in the internal
  401. /// MLx table.
  402. DenseMap<unsigned, unsigned> MLxEntryMap;
  403. /// MLxHazardOpcodes - Set of add / sub and multiply opcodes that would cause
  404. /// stalls when scheduled together with fp MLA / MLS opcodes.
  405. SmallSet<unsigned, 16> MLxHazardOpcodes;
  406. public:
  407. /// isFpMLxInstruction - Return true if the specified opcode is a fp MLA / MLS
  408. /// instruction.
  409. bool isFpMLxInstruction(unsigned Opcode) const {
  410. return MLxEntryMap.count(Opcode);
  411. }
  412. /// isFpMLxInstruction - This version also returns the multiply opcode and the
  413. /// addition / subtraction opcode to expand to. Return true for 'HasLane' for
  414. /// the MLX instructions with an extra lane operand.
  415. bool isFpMLxInstruction(unsigned Opcode, unsigned &MulOpc,
  416. unsigned &AddSubOpc, bool &NegAcc,
  417. bool &HasLane) const;
  418. /// canCauseFpMLxStall - Return true if an instruction of the specified opcode
  419. /// will cause stalls when scheduled after (within 4-cycle window) a fp
  420. /// MLA / MLS instruction.
  421. bool canCauseFpMLxStall(unsigned Opcode) const {
  422. return MLxHazardOpcodes.count(Opcode);
  423. }
  424. /// Returns true if the instruction has a shift by immediate that can be
  425. /// executed in one cycle less.
  426. bool isSwiftFastImmShift(const MachineInstr *MI) const;
  427. /// Returns predicate register associated with the given frame instruction.
  428. unsigned getFramePred(const MachineInstr &MI) const {
  429. assert(isFrameInstr(MI));
  430. // Operands of ADJCALLSTACKDOWN/ADJCALLSTACKUP:
  431. // - argument declared in the pattern:
  432. // 0 - frame size
  433. // 1 - arg of CALLSEQ_START/CALLSEQ_END
  434. // 2 - predicate code (like ARMCC::AL)
  435. // - added by predOps:
  436. // 3 - predicate reg
  437. return MI.getOperand(3).getReg();
  438. }
  439. Optional<RegImmPair> isAddImmediate(const MachineInstr &MI,
  440. Register Reg) const override;
  441. };
  442. /// Get the operands corresponding to the given \p Pred value. By default, the
  443. /// predicate register is assumed to be 0 (no register), but you can pass in a
  444. /// \p PredReg if that is not the case.
  445. static inline std::array<MachineOperand, 2> predOps(ARMCC::CondCodes Pred,
  446. unsigned PredReg = 0) {
  447. return {{MachineOperand::CreateImm(static_cast<int64_t>(Pred)),
  448. MachineOperand::CreateReg(PredReg, false)}};
  449. }
  450. /// Get the operand corresponding to the conditional code result. By default,
  451. /// this is 0 (no register).
  452. static inline MachineOperand condCodeOp(unsigned CCReg = 0) {
  453. return MachineOperand::CreateReg(CCReg, false);
  454. }
  455. /// Get the operand corresponding to the conditional code result for Thumb1.
  456. /// This operand will always refer to CPSR and it will have the Define flag set.
  457. /// You can optionally set the Dead flag by means of \p isDead.
  458. static inline MachineOperand t1CondCodeOp(bool isDead = false) {
  459. return MachineOperand::CreateReg(ARM::CPSR,
  460. /*Define*/ true, /*Implicit*/ false,
  461. /*Kill*/ false, isDead);
  462. }
  463. static inline
  464. bool isUncondBranchOpcode(int Opc) {
  465. return Opc == ARM::B || Opc == ARM::tB || Opc == ARM::t2B;
  466. }
  467. // This table shows the VPT instruction variants, i.e. the different
  468. // mask field encodings, see also B5.6. Predication/conditional execution in
  469. // the ArmARM.
  470. static inline bool isVPTOpcode(int Opc) {
  471. return Opc == ARM::MVE_VPTv16i8 || Opc == ARM::MVE_VPTv16u8 ||
  472. Opc == ARM::MVE_VPTv16s8 || Opc == ARM::MVE_VPTv8i16 ||
  473. Opc == ARM::MVE_VPTv8u16 || Opc == ARM::MVE_VPTv8s16 ||
  474. Opc == ARM::MVE_VPTv4i32 || Opc == ARM::MVE_VPTv4u32 ||
  475. Opc == ARM::MVE_VPTv4s32 || Opc == ARM::MVE_VPTv4f32 ||
  476. Opc == ARM::MVE_VPTv8f16 || Opc == ARM::MVE_VPTv16i8r ||
  477. Opc == ARM::MVE_VPTv16u8r || Opc == ARM::MVE_VPTv16s8r ||
  478. Opc == ARM::MVE_VPTv8i16r || Opc == ARM::MVE_VPTv8u16r ||
  479. Opc == ARM::MVE_VPTv8s16r || Opc == ARM::MVE_VPTv4i32r ||
  480. Opc == ARM::MVE_VPTv4u32r || Opc == ARM::MVE_VPTv4s32r ||
  481. Opc == ARM::MVE_VPTv4f32r || Opc == ARM::MVE_VPTv8f16r ||
  482. Opc == ARM::MVE_VPST;
  483. }
  484. static inline
  485. unsigned VCMPOpcodeToVPT(unsigned Opcode) {
  486. switch (Opcode) {
  487. default:
  488. return 0;
  489. case ARM::MVE_VCMPf32:
  490. return ARM::MVE_VPTv4f32;
  491. case ARM::MVE_VCMPf16:
  492. return ARM::MVE_VPTv8f16;
  493. case ARM::MVE_VCMPi8:
  494. return ARM::MVE_VPTv16i8;
  495. case ARM::MVE_VCMPi16:
  496. return ARM::MVE_VPTv8i16;
  497. case ARM::MVE_VCMPi32:
  498. return ARM::MVE_VPTv4i32;
  499. case ARM::MVE_VCMPu8:
  500. return ARM::MVE_VPTv16u8;
  501. case ARM::MVE_VCMPu16:
  502. return ARM::MVE_VPTv8u16;
  503. case ARM::MVE_VCMPu32:
  504. return ARM::MVE_VPTv4u32;
  505. case ARM::MVE_VCMPs8:
  506. return ARM::MVE_VPTv16s8;
  507. case ARM::MVE_VCMPs16:
  508. return ARM::MVE_VPTv8s16;
  509. case ARM::MVE_VCMPs32:
  510. return ARM::MVE_VPTv4s32;
  511. case ARM::MVE_VCMPf32r:
  512. return ARM::MVE_VPTv4f32r;
  513. case ARM::MVE_VCMPf16r:
  514. return ARM::MVE_VPTv8f16r;
  515. case ARM::MVE_VCMPi8r:
  516. return ARM::MVE_VPTv16i8r;
  517. case ARM::MVE_VCMPi16r:
  518. return ARM::MVE_VPTv8i16r;
  519. case ARM::MVE_VCMPi32r:
  520. return ARM::MVE_VPTv4i32r;
  521. case ARM::MVE_VCMPu8r:
  522. return ARM::MVE_VPTv16u8r;
  523. case ARM::MVE_VCMPu16r:
  524. return ARM::MVE_VPTv8u16r;
  525. case ARM::MVE_VCMPu32r:
  526. return ARM::MVE_VPTv4u32r;
  527. case ARM::MVE_VCMPs8r:
  528. return ARM::MVE_VPTv16s8r;
  529. case ARM::MVE_VCMPs16r:
  530. return ARM::MVE_VPTv8s16r;
  531. case ARM::MVE_VCMPs32r:
  532. return ARM::MVE_VPTv4s32r;
  533. }
  534. }
  535. static inline
  536. bool isCondBranchOpcode(int Opc) {
  537. return Opc == ARM::Bcc || Opc == ARM::tBcc || Opc == ARM::t2Bcc;
  538. }
  539. static inline bool isJumpTableBranchOpcode(int Opc) {
  540. return Opc == ARM::BR_JTr || Opc == ARM::BR_JTm_i12 ||
  541. Opc == ARM::BR_JTm_rs || Opc == ARM::BR_JTadd || Opc == ARM::tBR_JTr ||
  542. Opc == ARM::t2BR_JT;
  543. }
  544. static inline
  545. bool isIndirectBranchOpcode(int Opc) {
  546. return Opc == ARM::BX || Opc == ARM::MOVPCRX || Opc == ARM::tBRIND;
  547. }
  548. static inline bool isIndirectCall(const MachineInstr &MI) {
  549. int Opc = MI.getOpcode();
  550. switch (Opc) {
  551. // indirect calls:
  552. case ARM::BLX:
  553. case ARM::BLX_noip:
  554. case ARM::BLX_pred:
  555. case ARM::BLX_pred_noip:
  556. case ARM::BX_CALL:
  557. case ARM::BMOVPCRX_CALL:
  558. case ARM::TCRETURNri:
  559. case ARM::TAILJMPr:
  560. case ARM::TAILJMPr4:
  561. case ARM::tBLXr:
  562. case ARM::tBLXr_noip:
  563. case ARM::tBLXNSr:
  564. case ARM::tBLXNS_CALL:
  565. case ARM::tBX_CALL:
  566. case ARM::tTAILJMPr:
  567. assert(MI.isCall(MachineInstr::IgnoreBundle));
  568. return true;
  569. // direct calls:
  570. case ARM::BL:
  571. case ARM::BL_pred:
  572. case ARM::BMOVPCB_CALL:
  573. case ARM::BL_PUSHLR:
  574. case ARM::BLXi:
  575. case ARM::TCRETURNdi:
  576. case ARM::TAILJMPd:
  577. case ARM::SVC:
  578. case ARM::HVC:
  579. case ARM::TPsoft:
  580. case ARM::tTAILJMPd:
  581. case ARM::t2SMC:
  582. case ARM::t2HVC:
  583. case ARM::tBL:
  584. case ARM::tBLXi:
  585. case ARM::tBL_PUSHLR:
  586. case ARM::tTAILJMPdND:
  587. case ARM::tSVC:
  588. case ARM::tTPsoft:
  589. assert(MI.isCall(MachineInstr::IgnoreBundle));
  590. return false;
  591. }
  592. assert(!MI.isCall(MachineInstr::IgnoreBundle));
  593. return false;
  594. }
  595. static inline bool isIndirectControlFlowNotComingBack(const MachineInstr &MI) {
  596. int opc = MI.getOpcode();
  597. return MI.isReturn() || isIndirectBranchOpcode(MI.getOpcode()) ||
  598. isJumpTableBranchOpcode(opc);
  599. }
  600. static inline bool isSpeculationBarrierEndBBOpcode(int Opc) {
  601. return Opc == ARM::SpeculationBarrierISBDSBEndBB ||
  602. Opc == ARM::SpeculationBarrierSBEndBB ||
  603. Opc == ARM::t2SpeculationBarrierISBDSBEndBB ||
  604. Opc == ARM::t2SpeculationBarrierSBEndBB;
  605. }
  606. static inline bool isPopOpcode(int Opc) {
  607. return Opc == ARM::tPOP_RET || Opc == ARM::LDMIA_RET ||
  608. Opc == ARM::t2LDMIA_RET || Opc == ARM::tPOP || Opc == ARM::LDMIA_UPD ||
  609. Opc == ARM::t2LDMIA_UPD || Opc == ARM::VLDMDIA_UPD;
  610. }
  611. static inline bool isPushOpcode(int Opc) {
  612. return Opc == ARM::tPUSH || Opc == ARM::t2STMDB_UPD ||
  613. Opc == ARM::STMDB_UPD || Opc == ARM::VSTMDDB_UPD;
  614. }
  615. static inline bool isSubImmOpcode(int Opc) {
  616. return Opc == ARM::SUBri ||
  617. Opc == ARM::tSUBi3 || Opc == ARM::tSUBi8 ||
  618. Opc == ARM::tSUBSi3 || Opc == ARM::tSUBSi8 ||
  619. Opc == ARM::t2SUBri || Opc == ARM::t2SUBri12 || Opc == ARM::t2SUBSri;
  620. }
  621. static inline bool isMovRegOpcode(int Opc) {
  622. return Opc == ARM::MOVr || Opc == ARM::tMOVr || Opc == ARM::t2MOVr;
  623. }
  624. /// isValidCoprocessorNumber - decide whether an explicit coprocessor
  625. /// number is legal in generic instructions like CDP. The answer can
  626. /// vary with the subtarget.
  627. static inline bool isValidCoprocessorNumber(unsigned Num,
  628. const FeatureBitset& featureBits) {
  629. // In Armv7 and Armv8-M CP10 and CP11 clash with VFP/NEON, however, the
  630. // coprocessor is still valid for CDP/MCR/MRC and friends. Allowing it is
  631. // useful for code which is shared with older architectures which do not know
  632. // the new VFP/NEON mnemonics.
  633. // Armv8-A disallows everything *other* than 111x (CP14 and CP15).
  634. if (featureBits[ARM::HasV8Ops] && (Num & 0xE) != 0xE)
  635. return false;
  636. // Armv8.1-M disallows 100x (CP8,CP9) and 111x (CP14,CP15)
  637. // which clash with MVE.
  638. if (featureBits[ARM::HasV8_1MMainlineOps] &&
  639. ((Num & 0xE) == 0x8 || (Num & 0xE) == 0xE))
  640. return false;
  641. return true;
  642. }
  643. /// getInstrPredicate - If instruction is predicated, returns its predicate
  644. /// condition, otherwise returns AL. It also returns the condition code
  645. /// register by reference.
  646. ARMCC::CondCodes getInstrPredicate(const MachineInstr &MI, Register &PredReg);
  647. unsigned getMatchingCondBranchOpcode(unsigned Opc);
  648. /// Map pseudo instructions that imply an 'S' bit onto real opcodes. Whether
  649. /// the instruction is encoded with an 'S' bit is determined by the optional
  650. /// CPSR def operand.
  651. unsigned convertAddSubFlagsOpcode(unsigned OldOpc);
  652. /// emitARMRegPlusImmediate / emitT2RegPlusImmediate - Emits a series of
  653. /// instructions to materializea destreg = basereg + immediate in ARM / Thumb2
  654. /// code.
  655. void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
  656. MachineBasicBlock::iterator &MBBI,
  657. const DebugLoc &dl, Register DestReg,
  658. Register BaseReg, int NumBytes,
  659. ARMCC::CondCodes Pred, Register PredReg,
  660. const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);
  661. void emitT2RegPlusImmediate(MachineBasicBlock &MBB,
  662. MachineBasicBlock::iterator &MBBI,
  663. const DebugLoc &dl, Register DestReg,
  664. Register BaseReg, int NumBytes,
  665. ARMCC::CondCodes Pred, Register PredReg,
  666. const ARMBaseInstrInfo &TII, unsigned MIFlags = 0);
  667. void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
  668. MachineBasicBlock::iterator &MBBI,
  669. const DebugLoc &dl, Register DestReg,
  670. Register BaseReg, int NumBytes,
  671. const TargetInstrInfo &TII,
  672. const ARMBaseRegisterInfo &MRI,
  673. unsigned MIFlags = 0);
  674. /// Tries to add registers to the reglist of a given base-updating
  675. /// push/pop instruction to adjust the stack by an additional
  676. /// NumBytes. This can save a few bytes per function in code-size, but
  677. /// obviously generates more memory traffic. As such, it only takes
  678. /// effect in functions being optimised for size.
  679. bool tryFoldSPUpdateIntoPushPop(const ARMSubtarget &Subtarget,
  680. MachineFunction &MF, MachineInstr *MI,
  681. unsigned NumBytes);
  682. /// rewriteARMFrameIndex / rewriteT2FrameIndex -
  683. /// Rewrite MI to access 'Offset' bytes from the FP. Return false if the
  684. /// offset could not be handled directly in MI, and return the left-over
  685. /// portion by reference.
  686. bool rewriteARMFrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
  687. Register FrameReg, int &Offset,
  688. const ARMBaseInstrInfo &TII);
  689. bool rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
  690. Register FrameReg, int &Offset,
  691. const ARMBaseInstrInfo &TII,
  692. const TargetRegisterInfo *TRI);
  693. /// Return true if Reg is defd between From and To
  694. bool registerDefinedBetween(unsigned Reg, MachineBasicBlock::iterator From,
  695. MachineBasicBlock::iterator To,
  696. const TargetRegisterInfo *TRI);
  697. /// Search backwards from a tBcc to find a tCMPi8 against 0, meaning
  698. /// we can convert them to a tCBZ or tCBNZ. Return nullptr if not found.
  699. MachineInstr *findCMPToFoldIntoCBZ(MachineInstr *Br,
  700. const TargetRegisterInfo *TRI);
  701. void addUnpredicatedMveVpredNOp(MachineInstrBuilder &MIB);
  702. void addUnpredicatedMveVpredROp(MachineInstrBuilder &MIB, Register DestReg);
  703. void addPredicatedMveVpredNOp(MachineInstrBuilder &MIB, unsigned Cond);
  704. void addPredicatedMveVpredROp(MachineInstrBuilder &MIB, unsigned Cond,
  705. unsigned Inactive);
  706. /// Returns the number of instructions required to materialize the given
  707. /// constant in a register, or 3 if a literal pool load is needed.
  708. /// If ForCodesize is specified, an approximate cost in bytes is returned.
  709. unsigned ConstantMaterializationCost(unsigned Val,
  710. const ARMSubtarget *Subtarget,
  711. bool ForCodesize = false);
  712. /// Returns true if Val1 has a lower Constant Materialization Cost than Val2.
  713. /// Uses the cost from ConstantMaterializationCost, first with ForCodesize as
  714. /// specified. If the scores are equal, return the comparison for !ForCodesize.
  715. bool HasLowerConstantMaterializationCost(unsigned Val1, unsigned Val2,
  716. const ARMSubtarget *Subtarget,
  717. bool ForCodesize = false);
  718. // Return the immediate if this is ADDri or SUBri, scaled as appropriate.
  719. // Returns 0 for unknown instructions.
  720. inline int getAddSubImmediate(MachineInstr &MI) {
  721. int Scale = 1;
  722. unsigned ImmOp;
  723. switch (MI.getOpcode()) {
  724. case ARM::t2ADDri:
  725. ImmOp = 2;
  726. break;
  727. case ARM::t2SUBri:
  728. case ARM::t2SUBri12:
  729. ImmOp = 2;
  730. Scale = -1;
  731. break;
  732. case ARM::tSUBi3:
  733. case ARM::tSUBi8:
  734. ImmOp = 3;
  735. Scale = -1;
  736. break;
  737. default:
  738. return 0;
  739. }
  740. return Scale * MI.getOperand(ImmOp).getImm();
  741. }
  742. // Given a memory access Opcode, check that the give Imm would be a valid Offset
  743. // for this instruction using its addressing mode.
  744. inline bool isLegalAddressImm(unsigned Opcode, int Imm,
  745. const TargetInstrInfo *TII) {
  746. const MCInstrDesc &Desc = TII->get(Opcode);
  747. unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
  748. switch (AddrMode) {
  749. case ARMII::AddrModeT2_i7:
  750. return std::abs(Imm) < ((1 << 7) * 1);
  751. case ARMII::AddrModeT2_i7s2:
  752. return std::abs(Imm) < ((1 << 7) * 2) && Imm % 2 == 0;
  753. case ARMII::AddrModeT2_i7s4:
  754. return std::abs(Imm) < ((1 << 7) * 4) && Imm % 4 == 0;
  755. case ARMII::AddrModeT2_i8:
  756. return std::abs(Imm) < ((1 << 8) * 1);
  757. case ARMII::AddrModeT2_i8pos:
  758. return Imm >= 0 && Imm < ((1 << 8) * 1);
  759. case ARMII::AddrModeT2_i8neg:
  760. return Imm < 0 && -Imm < ((1 << 8) * 1);
  761. case ARMII::AddrModeT2_i8s4:
  762. return std::abs(Imm) < ((1 << 8) * 4) && Imm % 4 == 0;
  763. case ARMII::AddrModeT2_i12:
  764. return Imm >= 0 && Imm < ((1 << 12) * 1);
  765. case ARMII::AddrMode2:
  766. return std::abs(Imm) < ((1 << 12) * 1);
  767. default:
  768. llvm_unreachable("Unhandled Addressing mode");
  769. }
  770. }
  771. // Return true if the given intrinsic is a gather
  772. inline bool isGather(IntrinsicInst *IntInst) {
  773. if (IntInst == nullptr)
  774. return false;
  775. unsigned IntrinsicID = IntInst->getIntrinsicID();
  776. return (IntrinsicID == Intrinsic::masked_gather ||
  777. IntrinsicID == Intrinsic::arm_mve_vldr_gather_base ||
  778. IntrinsicID == Intrinsic::arm_mve_vldr_gather_base_predicated ||
  779. IntrinsicID == Intrinsic::arm_mve_vldr_gather_base_wb ||
  780. IntrinsicID == Intrinsic::arm_mve_vldr_gather_base_wb_predicated ||
  781. IntrinsicID == Intrinsic::arm_mve_vldr_gather_offset ||
  782. IntrinsicID == Intrinsic::arm_mve_vldr_gather_offset_predicated);
  783. }
  784. // Return true if the given intrinsic is a scatter
  785. inline bool isScatter(IntrinsicInst *IntInst) {
  786. if (IntInst == nullptr)
  787. return false;
  788. unsigned IntrinsicID = IntInst->getIntrinsicID();
  789. return (IntrinsicID == Intrinsic::masked_scatter ||
  790. IntrinsicID == Intrinsic::arm_mve_vstr_scatter_base ||
  791. IntrinsicID == Intrinsic::arm_mve_vstr_scatter_base_predicated ||
  792. IntrinsicID == Intrinsic::arm_mve_vstr_scatter_base_wb ||
  793. IntrinsicID == Intrinsic::arm_mve_vstr_scatter_base_wb_predicated ||
  794. IntrinsicID == Intrinsic::arm_mve_vstr_scatter_offset ||
  795. IntrinsicID == Intrinsic::arm_mve_vstr_scatter_offset_predicated);
  796. }
  797. // Return true if the given intrinsic is a gather or scatter
  798. inline bool isGatherScatter(IntrinsicInst *IntInst) {
  799. if (IntInst == nullptr)
  800. return false;
  801. return isGather(IntInst) || isScatter(IntInst);
  802. }
  803. unsigned getBLXOpcode(const MachineFunction &MF);
  804. unsigned gettBLXrOpcode(const MachineFunction &MF);
  805. unsigned getBLXpredOpcode(const MachineFunction &MF);
  806. } // end namespace llvm
  807. #endif // LLVM_LIB_TARGET_ARM_ARMBASEINSTRINFO_H