TargetInstrInfo.h 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020
  1. //===- llvm/CodeGen/TargetInstrInfo.h - Instruction Info --------*- 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 describes the target machine instruction set to the code generator.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CODEGEN_TARGETINSTRINFO_H
  13. #define LLVM_CODEGEN_TARGETINSTRINFO_H
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/DenseMapInfo.h"
  17. #include "llvm/ADT/None.h"
  18. #include "llvm/CodeGen/MIRFormatter.h"
  19. #include "llvm/CodeGen/MachineBasicBlock.h"
  20. #include "llvm/CodeGen/MachineCombinerPattern.h"
  21. #include "llvm/CodeGen/MachineFunction.h"
  22. #include "llvm/CodeGen/MachineInstr.h"
  23. #include "llvm/CodeGen/MachineInstrBuilder.h"
  24. #include "llvm/CodeGen/MachineOperand.h"
  25. #include "llvm/CodeGen/MachineOutliner.h"
  26. #include "llvm/CodeGen/RegisterClassInfo.h"
  27. #include "llvm/CodeGen/VirtRegMap.h"
  28. #include "llvm/MC/MCInstrInfo.h"
  29. #include "llvm/Support/BranchProbability.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include <cassert>
  32. #include <cstddef>
  33. #include <cstdint>
  34. #include <utility>
  35. #include <vector>
  36. namespace llvm {
  37. class AAResults;
  38. class DFAPacketizer;
  39. class InstrItineraryData;
  40. class LiveIntervals;
  41. class LiveVariables;
  42. class MachineLoop;
  43. class MachineMemOperand;
  44. class MachineRegisterInfo;
  45. class MCAsmInfo;
  46. class MCInst;
  47. struct MCSchedModel;
  48. class Module;
  49. class ScheduleDAG;
  50. class ScheduleDAGMI;
  51. class ScheduleHazardRecognizer;
  52. class SDNode;
  53. class SelectionDAG;
  54. class RegScavenger;
  55. class TargetRegisterClass;
  56. class TargetRegisterInfo;
  57. class TargetSchedModel;
  58. class TargetSubtargetInfo;
  59. template <class T> class SmallVectorImpl;
  60. using ParamLoadedValue = std::pair<MachineOperand, DIExpression*>;
  61. struct DestSourcePair {
  62. const MachineOperand *Destination;
  63. const MachineOperand *Source;
  64. DestSourcePair(const MachineOperand &Dest, const MachineOperand &Src)
  65. : Destination(&Dest), Source(&Src) {}
  66. };
  67. /// Used to describe a register and immediate addition.
  68. struct RegImmPair {
  69. Register Reg;
  70. int64_t Imm;
  71. RegImmPair(Register Reg, int64_t Imm) : Reg(Reg), Imm(Imm) {}
  72. };
  73. /// Used to describe addressing mode similar to ExtAddrMode in CodeGenPrepare.
  74. /// It holds the register values, the scale value and the displacement.
  75. struct ExtAddrMode {
  76. Register BaseReg;
  77. Register ScaledReg;
  78. int64_t Scale;
  79. int64_t Displacement;
  80. };
  81. //---------------------------------------------------------------------------
  82. ///
  83. /// TargetInstrInfo - Interface to description of machine instruction set
  84. ///
  85. class TargetInstrInfo : public MCInstrInfo {
  86. public:
  87. TargetInstrInfo(unsigned CFSetupOpcode = ~0u, unsigned CFDestroyOpcode = ~0u,
  88. unsigned CatchRetOpcode = ~0u, unsigned ReturnOpcode = ~0u)
  89. : CallFrameSetupOpcode(CFSetupOpcode),
  90. CallFrameDestroyOpcode(CFDestroyOpcode), CatchRetOpcode(CatchRetOpcode),
  91. ReturnOpcode(ReturnOpcode) {}
  92. TargetInstrInfo(const TargetInstrInfo &) = delete;
  93. TargetInstrInfo &operator=(const TargetInstrInfo &) = delete;
  94. virtual ~TargetInstrInfo();
  95. static bool isGenericOpcode(unsigned Opc) {
  96. return Opc <= TargetOpcode::GENERIC_OP_END;
  97. }
  98. /// Given a machine instruction descriptor, returns the register
  99. /// class constraint for OpNum, or NULL.
  100. virtual
  101. const TargetRegisterClass *getRegClass(const MCInstrDesc &MCID, unsigned OpNum,
  102. const TargetRegisterInfo *TRI,
  103. const MachineFunction &MF) const;
  104. /// Return true if the instruction is trivially rematerializable, meaning it
  105. /// has no side effects and requires no operands that aren't always available.
  106. /// This means the only allowed uses are constants and unallocatable physical
  107. /// registers so that the instructions result is independent of the place
  108. /// in the function.
  109. bool isTriviallyReMaterializable(const MachineInstr &MI,
  110. AAResults *AA = nullptr) const {
  111. return MI.getOpcode() == TargetOpcode::IMPLICIT_DEF ||
  112. (MI.getDesc().isRematerializable() &&
  113. (isReallyTriviallyReMaterializable(MI, AA) ||
  114. isReallyTriviallyReMaterializableGeneric(MI, AA)));
  115. }
  116. protected:
  117. /// For instructions with opcodes for which the M_REMATERIALIZABLE flag is
  118. /// set, this hook lets the target specify whether the instruction is actually
  119. /// trivially rematerializable, taking into consideration its operands. This
  120. /// predicate must return false if the instruction has any side effects other
  121. /// than producing a value, or if it requres any address registers that are
  122. /// not always available.
  123. /// Requirements must be check as stated in isTriviallyReMaterializable() .
  124. virtual bool isReallyTriviallyReMaterializable(const MachineInstr &MI,
  125. AAResults *AA) const {
  126. return false;
  127. }
  128. /// This method commutes the operands of the given machine instruction MI.
  129. /// The operands to be commuted are specified by their indices OpIdx1 and
  130. /// OpIdx2.
  131. ///
  132. /// If a target has any instructions that are commutable but require
  133. /// converting to different instructions or making non-trivial changes
  134. /// to commute them, this method can be overloaded to do that.
  135. /// The default implementation simply swaps the commutable operands.
  136. ///
  137. /// If NewMI is false, MI is modified in place and returned; otherwise, a
  138. /// new machine instruction is created and returned.
  139. ///
  140. /// Do not call this method for a non-commutable instruction.
  141. /// Even though the instruction is commutable, the method may still
  142. /// fail to commute the operands, null pointer is returned in such cases.
  143. virtual MachineInstr *commuteInstructionImpl(MachineInstr &MI, bool NewMI,
  144. unsigned OpIdx1,
  145. unsigned OpIdx2) const;
  146. /// Assigns the (CommutableOpIdx1, CommutableOpIdx2) pair of commutable
  147. /// operand indices to (ResultIdx1, ResultIdx2).
  148. /// One or both input values of the pair: (ResultIdx1, ResultIdx2) may be
  149. /// predefined to some indices or be undefined (designated by the special
  150. /// value 'CommuteAnyOperandIndex').
  151. /// The predefined result indices cannot be re-defined.
  152. /// The function returns true iff after the result pair redefinition
  153. /// the fixed result pair is equal to or equivalent to the source pair of
  154. /// indices: (CommutableOpIdx1, CommutableOpIdx2). It is assumed here that
  155. /// the pairs (x,y) and (y,x) are equivalent.
  156. static bool fixCommutedOpIndices(unsigned &ResultIdx1, unsigned &ResultIdx2,
  157. unsigned CommutableOpIdx1,
  158. unsigned CommutableOpIdx2);
  159. private:
  160. /// For instructions with opcodes for which the M_REMATERIALIZABLE flag is
  161. /// set and the target hook isReallyTriviallyReMaterializable returns false,
  162. /// this function does target-independent tests to determine if the
  163. /// instruction is really trivially rematerializable.
  164. bool isReallyTriviallyReMaterializableGeneric(const MachineInstr &MI,
  165. AAResults *AA) const;
  166. public:
  167. /// These methods return the opcode of the frame setup/destroy instructions
  168. /// if they exist (-1 otherwise). Some targets use pseudo instructions in
  169. /// order to abstract away the difference between operating with a frame
  170. /// pointer and operating without, through the use of these two instructions.
  171. ///
  172. unsigned getCallFrameSetupOpcode() const { return CallFrameSetupOpcode; }
  173. unsigned getCallFrameDestroyOpcode() const { return CallFrameDestroyOpcode; }
  174. /// Returns true if the argument is a frame pseudo instruction.
  175. bool isFrameInstr(const MachineInstr &I) const {
  176. return I.getOpcode() == getCallFrameSetupOpcode() ||
  177. I.getOpcode() == getCallFrameDestroyOpcode();
  178. }
  179. /// Returns true if the argument is a frame setup pseudo instruction.
  180. bool isFrameSetup(const MachineInstr &I) const {
  181. return I.getOpcode() == getCallFrameSetupOpcode();
  182. }
  183. /// Returns size of the frame associated with the given frame instruction.
  184. /// For frame setup instruction this is frame that is set up space set up
  185. /// after the instruction. For frame destroy instruction this is the frame
  186. /// freed by the caller.
  187. /// Note, in some cases a call frame (or a part of it) may be prepared prior
  188. /// to the frame setup instruction. It occurs in the calls that involve
  189. /// inalloca arguments. This function reports only the size of the frame part
  190. /// that is set up between the frame setup and destroy pseudo instructions.
  191. int64_t getFrameSize(const MachineInstr &I) const {
  192. assert(isFrameInstr(I) && "Not a frame instruction");
  193. assert(I.getOperand(0).getImm() >= 0);
  194. return I.getOperand(0).getImm();
  195. }
  196. /// Returns the total frame size, which is made up of the space set up inside
  197. /// the pair of frame start-stop instructions and the space that is set up
  198. /// prior to the pair.
  199. int64_t getFrameTotalSize(const MachineInstr &I) const {
  200. if (isFrameSetup(I)) {
  201. assert(I.getOperand(1).getImm() >= 0 &&
  202. "Frame size must not be negative");
  203. return getFrameSize(I) + I.getOperand(1).getImm();
  204. }
  205. return getFrameSize(I);
  206. }
  207. unsigned getCatchReturnOpcode() const { return CatchRetOpcode; }
  208. unsigned getReturnOpcode() const { return ReturnOpcode; }
  209. /// Returns the actual stack pointer adjustment made by an instruction
  210. /// as part of a call sequence. By default, only call frame setup/destroy
  211. /// instructions adjust the stack, but targets may want to override this
  212. /// to enable more fine-grained adjustment, or adjust by a different value.
  213. virtual int getSPAdjust(const MachineInstr &MI) const;
  214. /// Return true if the instruction is a "coalescable" extension instruction.
  215. /// That is, it's like a copy where it's legal for the source to overlap the
  216. /// destination. e.g. X86::MOVSX64rr32. If this returns true, then it's
  217. /// expected the pre-extension value is available as a subreg of the result
  218. /// register. This also returns the sub-register index in SubIdx.
  219. virtual bool isCoalescableExtInstr(const MachineInstr &MI, Register &SrcReg,
  220. Register &DstReg, unsigned &SubIdx) const {
  221. return false;
  222. }
  223. /// If the specified machine instruction is a direct
  224. /// load from a stack slot, return the virtual or physical register number of
  225. /// the destination along with the FrameIndex of the loaded stack slot. If
  226. /// not, return 0. This predicate must return 0 if the instruction has
  227. /// any side effects other than loading from the stack slot.
  228. virtual unsigned isLoadFromStackSlot(const MachineInstr &MI,
  229. int &FrameIndex) const {
  230. return 0;
  231. }
  232. /// Optional extension of isLoadFromStackSlot that returns the number of
  233. /// bytes loaded from the stack. This must be implemented if a backend
  234. /// supports partial stack slot spills/loads to further disambiguate
  235. /// what the load does.
  236. virtual unsigned isLoadFromStackSlot(const MachineInstr &MI,
  237. int &FrameIndex,
  238. unsigned &MemBytes) const {
  239. MemBytes = 0;
  240. return isLoadFromStackSlot(MI, FrameIndex);
  241. }
  242. /// Check for post-frame ptr elimination stack locations as well.
  243. /// This uses a heuristic so it isn't reliable for correctness.
  244. virtual unsigned isLoadFromStackSlotPostFE(const MachineInstr &MI,
  245. int &FrameIndex) const {
  246. return 0;
  247. }
  248. /// If the specified machine instruction has a load from a stack slot,
  249. /// return true along with the FrameIndices of the loaded stack slot and the
  250. /// machine mem operands containing the reference.
  251. /// If not, return false. Unlike isLoadFromStackSlot, this returns true for
  252. /// any instructions that loads from the stack. This is just a hint, as some
  253. /// cases may be missed.
  254. virtual bool hasLoadFromStackSlot(
  255. const MachineInstr &MI,
  256. SmallVectorImpl<const MachineMemOperand *> &Accesses) const;
  257. /// If the specified machine instruction is a direct
  258. /// store to a stack slot, return the virtual or physical register number of
  259. /// the source reg along with the FrameIndex of the loaded stack slot. If
  260. /// not, return 0. This predicate must return 0 if the instruction has
  261. /// any side effects other than storing to the stack slot.
  262. virtual unsigned isStoreToStackSlot(const MachineInstr &MI,
  263. int &FrameIndex) const {
  264. return 0;
  265. }
  266. /// Optional extension of isStoreToStackSlot that returns the number of
  267. /// bytes stored to the stack. This must be implemented if a backend
  268. /// supports partial stack slot spills/loads to further disambiguate
  269. /// what the store does.
  270. virtual unsigned isStoreToStackSlot(const MachineInstr &MI,
  271. int &FrameIndex,
  272. unsigned &MemBytes) const {
  273. MemBytes = 0;
  274. return isStoreToStackSlot(MI, FrameIndex);
  275. }
  276. /// Check for post-frame ptr elimination stack locations as well.
  277. /// This uses a heuristic, so it isn't reliable for correctness.
  278. virtual unsigned isStoreToStackSlotPostFE(const MachineInstr &MI,
  279. int &FrameIndex) const {
  280. return 0;
  281. }
  282. /// If the specified machine instruction has a store to a stack slot,
  283. /// return true along with the FrameIndices of the loaded stack slot and the
  284. /// machine mem operands containing the reference.
  285. /// If not, return false. Unlike isStoreToStackSlot,
  286. /// this returns true for any instructions that stores to the
  287. /// stack. This is just a hint, as some cases may be missed.
  288. virtual bool hasStoreToStackSlot(
  289. const MachineInstr &MI,
  290. SmallVectorImpl<const MachineMemOperand *> &Accesses) const;
  291. /// Return true if the specified machine instruction
  292. /// is a copy of one stack slot to another and has no other effect.
  293. /// Provide the identity of the two frame indices.
  294. virtual bool isStackSlotCopy(const MachineInstr &MI, int &DestFrameIndex,
  295. int &SrcFrameIndex) const {
  296. return false;
  297. }
  298. /// Compute the size in bytes and offset within a stack slot of a spilled
  299. /// register or subregister.
  300. ///
  301. /// \param [out] Size in bytes of the spilled value.
  302. /// \param [out] Offset in bytes within the stack slot.
  303. /// \returns true if both Size and Offset are successfully computed.
  304. ///
  305. /// Not all subregisters have computable spill slots. For example,
  306. /// subregisters registers may not be byte-sized, and a pair of discontiguous
  307. /// subregisters has no single offset.
  308. ///
  309. /// Targets with nontrivial bigendian implementations may need to override
  310. /// this, particularly to support spilled vector registers.
  311. virtual bool getStackSlotRange(const TargetRegisterClass *RC, unsigned SubIdx,
  312. unsigned &Size, unsigned &Offset,
  313. const MachineFunction &MF) const;
  314. /// Return true if the given instruction is terminator that is unspillable,
  315. /// according to isUnspillableTerminatorImpl.
  316. bool isUnspillableTerminator(const MachineInstr *MI) const {
  317. return MI->isTerminator() && isUnspillableTerminatorImpl(MI);
  318. }
  319. /// Returns the size in bytes of the specified MachineInstr, or ~0U
  320. /// when this function is not implemented by a target.
  321. virtual unsigned getInstSizeInBytes(const MachineInstr &MI) const {
  322. return ~0U;
  323. }
  324. /// Return true if the instruction is as cheap as a move instruction.
  325. ///
  326. /// Targets for different archs need to override this, and different
  327. /// micro-architectures can also be finely tuned inside.
  328. virtual bool isAsCheapAsAMove(const MachineInstr &MI) const {
  329. return MI.isAsCheapAsAMove();
  330. }
  331. /// Return true if the instruction should be sunk by MachineSink.
  332. ///
  333. /// MachineSink determines on its own whether the instruction is safe to sink;
  334. /// this gives the target a hook to override the default behavior with regards
  335. /// to which instructions should be sunk.
  336. virtual bool shouldSink(const MachineInstr &MI) const { return true; }
  337. /// Re-issue the specified 'original' instruction at the
  338. /// specific location targeting a new destination register.
  339. /// The register in Orig->getOperand(0).getReg() will be substituted by
  340. /// DestReg:SubIdx. Any existing subreg index is preserved or composed with
  341. /// SubIdx.
  342. virtual void reMaterialize(MachineBasicBlock &MBB,
  343. MachineBasicBlock::iterator MI, Register DestReg,
  344. unsigned SubIdx, const MachineInstr &Orig,
  345. const TargetRegisterInfo &TRI) const;
  346. /// Clones instruction or the whole instruction bundle \p Orig and
  347. /// insert into \p MBB before \p InsertBefore. The target may update operands
  348. /// that are required to be unique.
  349. ///
  350. /// \p Orig must not return true for MachineInstr::isNotDuplicable().
  351. virtual MachineInstr &duplicate(MachineBasicBlock &MBB,
  352. MachineBasicBlock::iterator InsertBefore,
  353. const MachineInstr &Orig) const;
  354. /// This method must be implemented by targets that
  355. /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
  356. /// may be able to convert a two-address instruction into one or more true
  357. /// three-address instructions on demand. This allows the X86 target (for
  358. /// example) to convert ADD and SHL instructions into LEA instructions if they
  359. /// would require register copies due to two-addressness.
  360. ///
  361. /// This method returns a null pointer if the transformation cannot be
  362. /// performed, otherwise it returns the last new instruction.
  363. ///
  364. virtual MachineInstr *convertToThreeAddress(MachineFunction::iterator &MFI,
  365. MachineInstr &MI,
  366. LiveVariables *LV) const {
  367. return nullptr;
  368. }
  369. // This constant can be used as an input value of operand index passed to
  370. // the method findCommutedOpIndices() to tell the method that the
  371. // corresponding operand index is not pre-defined and that the method
  372. // can pick any commutable operand.
  373. static const unsigned CommuteAnyOperandIndex = ~0U;
  374. /// This method commutes the operands of the given machine instruction MI.
  375. ///
  376. /// The operands to be commuted are specified by their indices OpIdx1 and
  377. /// OpIdx2. OpIdx1 and OpIdx2 arguments may be set to a special value
  378. /// 'CommuteAnyOperandIndex', which means that the method is free to choose
  379. /// any arbitrarily chosen commutable operand. If both arguments are set to
  380. /// 'CommuteAnyOperandIndex' then the method looks for 2 different commutable
  381. /// operands; then commutes them if such operands could be found.
  382. ///
  383. /// If NewMI is false, MI is modified in place and returned; otherwise, a
  384. /// new machine instruction is created and returned.
  385. ///
  386. /// Do not call this method for a non-commutable instruction or
  387. /// for non-commuable operands.
  388. /// Even though the instruction is commutable, the method may still
  389. /// fail to commute the operands, null pointer is returned in such cases.
  390. MachineInstr *
  391. commuteInstruction(MachineInstr &MI, bool NewMI = false,
  392. unsigned OpIdx1 = CommuteAnyOperandIndex,
  393. unsigned OpIdx2 = CommuteAnyOperandIndex) const;
  394. /// Returns true iff the routine could find two commutable operands in the
  395. /// given machine instruction.
  396. /// The 'SrcOpIdx1' and 'SrcOpIdx2' are INPUT and OUTPUT arguments.
  397. /// If any of the INPUT values is set to the special value
  398. /// 'CommuteAnyOperandIndex' then the method arbitrarily picks a commutable
  399. /// operand, then returns its index in the corresponding argument.
  400. /// If both of INPUT values are set to 'CommuteAnyOperandIndex' then method
  401. /// looks for 2 commutable operands.
  402. /// If INPUT values refer to some operands of MI, then the method simply
  403. /// returns true if the corresponding operands are commutable and returns
  404. /// false otherwise.
  405. ///
  406. /// For example, calling this method this way:
  407. /// unsigned Op1 = 1, Op2 = CommuteAnyOperandIndex;
  408. /// findCommutedOpIndices(MI, Op1, Op2);
  409. /// can be interpreted as a query asking to find an operand that would be
  410. /// commutable with the operand#1.
  411. virtual bool findCommutedOpIndices(const MachineInstr &MI,
  412. unsigned &SrcOpIdx1,
  413. unsigned &SrcOpIdx2) const;
  414. /// A pair composed of a register and a sub-register index.
  415. /// Used to give some type checking when modeling Reg:SubReg.
  416. struct RegSubRegPair {
  417. Register Reg;
  418. unsigned SubReg;
  419. RegSubRegPair(Register Reg = Register(), unsigned SubReg = 0)
  420. : Reg(Reg), SubReg(SubReg) {}
  421. bool operator==(const RegSubRegPair& P) const {
  422. return Reg == P.Reg && SubReg == P.SubReg;
  423. }
  424. bool operator!=(const RegSubRegPair& P) const {
  425. return !(*this == P);
  426. }
  427. };
  428. /// A pair composed of a pair of a register and a sub-register index,
  429. /// and another sub-register index.
  430. /// Used to give some type checking when modeling Reg:SubReg1, SubReg2.
  431. struct RegSubRegPairAndIdx : RegSubRegPair {
  432. unsigned SubIdx;
  433. RegSubRegPairAndIdx(Register Reg = Register(), unsigned SubReg = 0,
  434. unsigned SubIdx = 0)
  435. : RegSubRegPair(Reg, SubReg), SubIdx(SubIdx) {}
  436. };
  437. /// Build the equivalent inputs of a REG_SEQUENCE for the given \p MI
  438. /// and \p DefIdx.
  439. /// \p [out] InputRegs of the equivalent REG_SEQUENCE. Each element of
  440. /// the list is modeled as <Reg:SubReg, SubIdx>. Operands with the undef
  441. /// flag are not added to this list.
  442. /// E.g., REG_SEQUENCE %1:sub1, sub0, %2, sub1 would produce
  443. /// two elements:
  444. /// - %1:sub1, sub0
  445. /// - %2<:0>, sub1
  446. ///
  447. /// \returns true if it is possible to build such an input sequence
  448. /// with the pair \p MI, \p DefIdx. False otherwise.
  449. ///
  450. /// \pre MI.isRegSequence() or MI.isRegSequenceLike().
  451. ///
  452. /// \note The generic implementation does not provide any support for
  453. /// MI.isRegSequenceLike(). In other words, one has to override
  454. /// getRegSequenceLikeInputs for target specific instructions.
  455. bool
  456. getRegSequenceInputs(const MachineInstr &MI, unsigned DefIdx,
  457. SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const;
  458. /// Build the equivalent inputs of a EXTRACT_SUBREG for the given \p MI
  459. /// and \p DefIdx.
  460. /// \p [out] InputReg of the equivalent EXTRACT_SUBREG.
  461. /// E.g., EXTRACT_SUBREG %1:sub1, sub0, sub1 would produce:
  462. /// - %1:sub1, sub0
  463. ///
  464. /// \returns true if it is possible to build such an input sequence
  465. /// with the pair \p MI, \p DefIdx and the operand has no undef flag set.
  466. /// False otherwise.
  467. ///
  468. /// \pre MI.isExtractSubreg() or MI.isExtractSubregLike().
  469. ///
  470. /// \note The generic implementation does not provide any support for
  471. /// MI.isExtractSubregLike(). In other words, one has to override
  472. /// getExtractSubregLikeInputs for target specific instructions.
  473. bool getExtractSubregInputs(const MachineInstr &MI, unsigned DefIdx,
  474. RegSubRegPairAndIdx &InputReg) const;
  475. /// Build the equivalent inputs of a INSERT_SUBREG for the given \p MI
  476. /// and \p DefIdx.
  477. /// \p [out] BaseReg and \p [out] InsertedReg contain
  478. /// the equivalent inputs of INSERT_SUBREG.
  479. /// E.g., INSERT_SUBREG %0:sub0, %1:sub1, sub3 would produce:
  480. /// - BaseReg: %0:sub0
  481. /// - InsertedReg: %1:sub1, sub3
  482. ///
  483. /// \returns true if it is possible to build such an input sequence
  484. /// with the pair \p MI, \p DefIdx and the operand has no undef flag set.
  485. /// False otherwise.
  486. ///
  487. /// \pre MI.isInsertSubreg() or MI.isInsertSubregLike().
  488. ///
  489. /// \note The generic implementation does not provide any support for
  490. /// MI.isInsertSubregLike(). In other words, one has to override
  491. /// getInsertSubregLikeInputs for target specific instructions.
  492. bool getInsertSubregInputs(const MachineInstr &MI, unsigned DefIdx,
  493. RegSubRegPair &BaseReg,
  494. RegSubRegPairAndIdx &InsertedReg) const;
  495. /// Return true if two machine instructions would produce identical values.
  496. /// By default, this is only true when the two instructions
  497. /// are deemed identical except for defs. If this function is called when the
  498. /// IR is still in SSA form, the caller can pass the MachineRegisterInfo for
  499. /// aggressive checks.
  500. virtual bool produceSameValue(const MachineInstr &MI0,
  501. const MachineInstr &MI1,
  502. const MachineRegisterInfo *MRI = nullptr) const;
  503. /// \returns true if a branch from an instruction with opcode \p BranchOpc
  504. /// bytes is capable of jumping to a position \p BrOffset bytes away.
  505. virtual bool isBranchOffsetInRange(unsigned BranchOpc,
  506. int64_t BrOffset) const {
  507. llvm_unreachable("target did not implement");
  508. }
  509. /// \returns The block that branch instruction \p MI jumps to.
  510. virtual MachineBasicBlock *getBranchDestBlock(const MachineInstr &MI) const {
  511. llvm_unreachable("target did not implement");
  512. }
  513. /// Insert an unconditional indirect branch at the end of \p MBB to \p
  514. /// NewDestBB. \p BrOffset indicates the offset of \p NewDestBB relative to
  515. /// the offset of the position to insert the new branch.
  516. ///
  517. /// \returns The number of bytes added to the block.
  518. virtual unsigned insertIndirectBranch(MachineBasicBlock &MBB,
  519. MachineBasicBlock &NewDestBB,
  520. const DebugLoc &DL,
  521. int64_t BrOffset = 0,
  522. RegScavenger *RS = nullptr) const {
  523. llvm_unreachable("target did not implement");
  524. }
  525. /// Analyze the branching code at the end of MBB, returning
  526. /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
  527. /// implemented for a target). Upon success, this returns false and returns
  528. /// with the following information in various cases:
  529. ///
  530. /// 1. If this block ends with no branches (it just falls through to its succ)
  531. /// just return false, leaving TBB/FBB null.
  532. /// 2. If this block ends with only an unconditional branch, it sets TBB to be
  533. /// the destination block.
  534. /// 3. If this block ends with a conditional branch and it falls through to a
  535. /// successor block, it sets TBB to be the branch destination block and a
  536. /// list of operands that evaluate the condition. These operands can be
  537. /// passed to other TargetInstrInfo methods to create new branches.
  538. /// 4. If this block ends with a conditional branch followed by an
  539. /// unconditional branch, it returns the 'true' destination in TBB, the
  540. /// 'false' destination in FBB, and a list of operands that evaluate the
  541. /// condition. These operands can be passed to other TargetInstrInfo
  542. /// methods to create new branches.
  543. ///
  544. /// Note that removeBranch and insertBranch must be implemented to support
  545. /// cases where this method returns success.
  546. ///
  547. /// If AllowModify is true, then this routine is allowed to modify the basic
  548. /// block (e.g. delete instructions after the unconditional branch).
  549. ///
  550. /// The CFG information in MBB.Predecessors and MBB.Successors must be valid
  551. /// before calling this function.
  552. virtual bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
  553. MachineBasicBlock *&FBB,
  554. SmallVectorImpl<MachineOperand> &Cond,
  555. bool AllowModify = false) const {
  556. return true;
  557. }
  558. /// Represents a predicate at the MachineFunction level. The control flow a
  559. /// MachineBranchPredicate represents is:
  560. ///
  561. /// Reg = LHS `Predicate` RHS == ConditionDef
  562. /// if Reg then goto TrueDest else goto FalseDest
  563. ///
  564. struct MachineBranchPredicate {
  565. enum ComparePredicate {
  566. PRED_EQ, // True if two values are equal
  567. PRED_NE, // True if two values are not equal
  568. PRED_INVALID // Sentinel value
  569. };
  570. ComparePredicate Predicate = PRED_INVALID;
  571. MachineOperand LHS = MachineOperand::CreateImm(0);
  572. MachineOperand RHS = MachineOperand::CreateImm(0);
  573. MachineBasicBlock *TrueDest = nullptr;
  574. MachineBasicBlock *FalseDest = nullptr;
  575. MachineInstr *ConditionDef = nullptr;
  576. /// SingleUseCondition is true if ConditionDef is dead except for the
  577. /// branch(es) at the end of the basic block.
  578. ///
  579. bool SingleUseCondition = false;
  580. explicit MachineBranchPredicate() = default;
  581. };
  582. /// Analyze the branching code at the end of MBB and parse it into the
  583. /// MachineBranchPredicate structure if possible. Returns false on success
  584. /// and true on failure.
  585. ///
  586. /// If AllowModify is true, then this routine is allowed to modify the basic
  587. /// block (e.g. delete instructions after the unconditional branch).
  588. ///
  589. virtual bool analyzeBranchPredicate(MachineBasicBlock &MBB,
  590. MachineBranchPredicate &MBP,
  591. bool AllowModify = false) const {
  592. return true;
  593. }
  594. /// Remove the branching code at the end of the specific MBB.
  595. /// This is only invoked in cases where analyzeBranch returns success. It
  596. /// returns the number of instructions that were removed.
  597. /// If \p BytesRemoved is non-null, report the change in code size from the
  598. /// removed instructions.
  599. virtual unsigned removeBranch(MachineBasicBlock &MBB,
  600. int *BytesRemoved = nullptr) const {
  601. llvm_unreachable("Target didn't implement TargetInstrInfo::removeBranch!");
  602. }
  603. /// Insert branch code into the end of the specified MachineBasicBlock. The
  604. /// operands to this method are the same as those returned by analyzeBranch.
  605. /// This is only invoked in cases where analyzeBranch returns success. It
  606. /// returns the number of instructions inserted. If \p BytesAdded is non-null,
  607. /// report the change in code size from the added instructions.
  608. ///
  609. /// It is also invoked by tail merging to add unconditional branches in
  610. /// cases where analyzeBranch doesn't apply because there was no original
  611. /// branch to analyze. At least this much must be implemented, else tail
  612. /// merging needs to be disabled.
  613. ///
  614. /// The CFG information in MBB.Predecessors and MBB.Successors must be valid
  615. /// before calling this function.
  616. virtual unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
  617. MachineBasicBlock *FBB,
  618. ArrayRef<MachineOperand> Cond,
  619. const DebugLoc &DL,
  620. int *BytesAdded = nullptr) const {
  621. llvm_unreachable("Target didn't implement TargetInstrInfo::insertBranch!");
  622. }
  623. unsigned insertUnconditionalBranch(MachineBasicBlock &MBB,
  624. MachineBasicBlock *DestBB,
  625. const DebugLoc &DL,
  626. int *BytesAdded = nullptr) const {
  627. return insertBranch(MBB, DestBB, nullptr, ArrayRef<MachineOperand>(), DL,
  628. BytesAdded);
  629. }
  630. /// Object returned by analyzeLoopForPipelining. Allows software pipelining
  631. /// implementations to query attributes of the loop being pipelined and to
  632. /// apply target-specific updates to the loop once pipelining is complete.
  633. class PipelinerLoopInfo {
  634. public:
  635. virtual ~PipelinerLoopInfo();
  636. /// Return true if the given instruction should not be pipelined and should
  637. /// be ignored. An example could be a loop comparison, or induction variable
  638. /// update with no users being pipelined.
  639. virtual bool shouldIgnoreForPipelining(const MachineInstr *MI) const = 0;
  640. /// Create a condition to determine if the trip count of the loop is greater
  641. /// than TC.
  642. ///
  643. /// If the trip count is statically known to be greater than TC, return
  644. /// true. If the trip count is statically known to be not greater than TC,
  645. /// return false. Otherwise return nullopt and fill out Cond with the test
  646. /// condition.
  647. virtual Optional<bool>
  648. createTripCountGreaterCondition(int TC, MachineBasicBlock &MBB,
  649. SmallVectorImpl<MachineOperand> &Cond) = 0;
  650. /// Modify the loop such that the trip count is
  651. /// OriginalTC + TripCountAdjust.
  652. virtual void adjustTripCount(int TripCountAdjust) = 0;
  653. /// Called when the loop's preheader has been modified to NewPreheader.
  654. virtual void setPreheader(MachineBasicBlock *NewPreheader) = 0;
  655. /// Called when the loop is being removed. Any instructions in the preheader
  656. /// should be removed.
  657. ///
  658. /// Once this function is called, no other functions on this object are
  659. /// valid; the loop has been removed.
  660. virtual void disposed() = 0;
  661. };
  662. /// Analyze loop L, which must be a single-basic-block loop, and if the
  663. /// conditions can be understood enough produce a PipelinerLoopInfo object.
  664. virtual std::unique_ptr<PipelinerLoopInfo>
  665. analyzeLoopForPipelining(MachineBasicBlock *LoopBB) const {
  666. return nullptr;
  667. }
  668. /// Analyze the loop code, return true if it cannot be understood. Upon
  669. /// success, this function returns false and returns information about the
  670. /// induction variable and compare instruction used at the end.
  671. virtual bool analyzeLoop(MachineLoop &L, MachineInstr *&IndVarInst,
  672. MachineInstr *&CmpInst) const {
  673. return true;
  674. }
  675. /// Generate code to reduce the loop iteration by one and check if the loop
  676. /// is finished. Return the value/register of the new loop count. We need
  677. /// this function when peeling off one or more iterations of a loop. This
  678. /// function assumes the nth iteration is peeled first.
  679. virtual unsigned reduceLoopCount(MachineBasicBlock &MBB,
  680. MachineBasicBlock &PreHeader,
  681. MachineInstr *IndVar, MachineInstr &Cmp,
  682. SmallVectorImpl<MachineOperand> &Cond,
  683. SmallVectorImpl<MachineInstr *> &PrevInsts,
  684. unsigned Iter, unsigned MaxIter) const {
  685. llvm_unreachable("Target didn't implement ReduceLoopCount");
  686. }
  687. /// Delete the instruction OldInst and everything after it, replacing it with
  688. /// an unconditional branch to NewDest. This is used by the tail merging pass.
  689. virtual void ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
  690. MachineBasicBlock *NewDest) const;
  691. /// Return true if it's legal to split the given basic
  692. /// block at the specified instruction (i.e. instruction would be the start
  693. /// of a new basic block).
  694. virtual bool isLegalToSplitMBBAt(MachineBasicBlock &MBB,
  695. MachineBasicBlock::iterator MBBI) const {
  696. return true;
  697. }
  698. /// Return true if it's profitable to predicate
  699. /// instructions with accumulated instruction latency of "NumCycles"
  700. /// of the specified basic block, where the probability of the instructions
  701. /// being executed is given by Probability, and Confidence is a measure
  702. /// of our confidence that it will be properly predicted.
  703. virtual bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
  704. unsigned ExtraPredCycles,
  705. BranchProbability Probability) const {
  706. return false;
  707. }
  708. /// Second variant of isProfitableToIfCvt. This one
  709. /// checks for the case where two basic blocks from true and false path
  710. /// of a if-then-else (diamond) are predicated on mutually exclusive
  711. /// predicates, where the probability of the true path being taken is given
  712. /// by Probability, and Confidence is a measure of our confidence that it
  713. /// will be properly predicted.
  714. virtual bool isProfitableToIfCvt(MachineBasicBlock &TMBB, unsigned NumTCycles,
  715. unsigned ExtraTCycles,
  716. MachineBasicBlock &FMBB, unsigned NumFCycles,
  717. unsigned ExtraFCycles,
  718. BranchProbability Probability) const {
  719. return false;
  720. }
  721. /// Return true if it's profitable for if-converter to duplicate instructions
  722. /// of specified accumulated instruction latencies in the specified MBB to
  723. /// enable if-conversion.
  724. /// The probability of the instructions being executed is given by
  725. /// Probability, and Confidence is a measure of our confidence that it
  726. /// will be properly predicted.
  727. virtual bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB,
  728. unsigned NumCycles,
  729. BranchProbability Probability) const {
  730. return false;
  731. }
  732. /// Return the increase in code size needed to predicate a contiguous run of
  733. /// NumInsts instructions.
  734. virtual unsigned extraSizeToPredicateInstructions(const MachineFunction &MF,
  735. unsigned NumInsts) const {
  736. return 0;
  737. }
  738. /// Return an estimate for the code size reduction (in bytes) which will be
  739. /// caused by removing the given branch instruction during if-conversion.
  740. virtual unsigned predictBranchSizeForIfCvt(MachineInstr &MI) const {
  741. return getInstSizeInBytes(MI);
  742. }
  743. /// Return true if it's profitable to unpredicate
  744. /// one side of a 'diamond', i.e. two sides of if-else predicated on mutually
  745. /// exclusive predicates.
  746. /// e.g.
  747. /// subeq r0, r1, #1
  748. /// addne r0, r1, #1
  749. /// =>
  750. /// sub r0, r1, #1
  751. /// addne r0, r1, #1
  752. ///
  753. /// This may be profitable is conditional instructions are always executed.
  754. virtual bool isProfitableToUnpredicate(MachineBasicBlock &TMBB,
  755. MachineBasicBlock &FMBB) const {
  756. return false;
  757. }
  758. /// Return true if it is possible to insert a select
  759. /// instruction that chooses between TrueReg and FalseReg based on the
  760. /// condition code in Cond.
  761. ///
  762. /// When successful, also return the latency in cycles from TrueReg,
  763. /// FalseReg, and Cond to the destination register. In most cases, a select
  764. /// instruction will be 1 cycle, so CondCycles = TrueCycles = FalseCycles = 1
  765. ///
  766. /// Some x86 implementations have 2-cycle cmov instructions.
  767. ///
  768. /// @param MBB Block where select instruction would be inserted.
  769. /// @param Cond Condition returned by analyzeBranch.
  770. /// @param DstReg Virtual dest register that the result should write to.
  771. /// @param TrueReg Virtual register to select when Cond is true.
  772. /// @param FalseReg Virtual register to select when Cond is false.
  773. /// @param CondCycles Latency from Cond+Branch to select output.
  774. /// @param TrueCycles Latency from TrueReg to select output.
  775. /// @param FalseCycles Latency from FalseReg to select output.
  776. virtual bool canInsertSelect(const MachineBasicBlock &MBB,
  777. ArrayRef<MachineOperand> Cond, Register DstReg,
  778. Register TrueReg, Register FalseReg,
  779. int &CondCycles, int &TrueCycles,
  780. int &FalseCycles) const {
  781. return false;
  782. }
  783. /// Insert a select instruction into MBB before I that will copy TrueReg to
  784. /// DstReg when Cond is true, and FalseReg to DstReg when Cond is false.
  785. ///
  786. /// This function can only be called after canInsertSelect() returned true.
  787. /// The condition in Cond comes from analyzeBranch, and it can be assumed
  788. /// that the same flags or registers required by Cond are available at the
  789. /// insertion point.
  790. ///
  791. /// @param MBB Block where select instruction should be inserted.
  792. /// @param I Insertion point.
  793. /// @param DL Source location for debugging.
  794. /// @param DstReg Virtual register to be defined by select instruction.
  795. /// @param Cond Condition as computed by analyzeBranch.
  796. /// @param TrueReg Virtual register to copy when Cond is true.
  797. /// @param FalseReg Virtual register to copy when Cons is false.
  798. virtual void insertSelect(MachineBasicBlock &MBB,
  799. MachineBasicBlock::iterator I, const DebugLoc &DL,
  800. Register DstReg, ArrayRef<MachineOperand> Cond,
  801. Register TrueReg, Register FalseReg) const {
  802. llvm_unreachable("Target didn't implement TargetInstrInfo::insertSelect!");
  803. }
  804. /// Analyze the given select instruction, returning true if
  805. /// it cannot be understood. It is assumed that MI->isSelect() is true.
  806. ///
  807. /// When successful, return the controlling condition and the operands that
  808. /// determine the true and false result values.
  809. ///
  810. /// Result = SELECT Cond, TrueOp, FalseOp
  811. ///
  812. /// Some targets can optimize select instructions, for example by predicating
  813. /// the instruction defining one of the operands. Such targets should set
  814. /// Optimizable.
  815. ///
  816. /// @param MI Select instruction to analyze.
  817. /// @param Cond Condition controlling the select.
  818. /// @param TrueOp Operand number of the value selected when Cond is true.
  819. /// @param FalseOp Operand number of the value selected when Cond is false.
  820. /// @param Optimizable Returned as true if MI is optimizable.
  821. /// @returns False on success.
  822. virtual bool analyzeSelect(const MachineInstr &MI,
  823. SmallVectorImpl<MachineOperand> &Cond,
  824. unsigned &TrueOp, unsigned &FalseOp,
  825. bool &Optimizable) const {
  826. assert(MI.getDesc().isSelect() && "MI must be a select instruction");
  827. return true;
  828. }
  829. /// Given a select instruction that was understood by
  830. /// analyzeSelect and returned Optimizable = true, attempt to optimize MI by
  831. /// merging it with one of its operands. Returns NULL on failure.
  832. ///
  833. /// When successful, returns the new select instruction. The client is
  834. /// responsible for deleting MI.
  835. ///
  836. /// If both sides of the select can be optimized, PreferFalse is used to pick
  837. /// a side.
  838. ///
  839. /// @param MI Optimizable select instruction.
  840. /// @param NewMIs Set that record all MIs in the basic block up to \p
  841. /// MI. Has to be updated with any newly created MI or deleted ones.
  842. /// @param PreferFalse Try to optimize FalseOp instead of TrueOp.
  843. /// @returns Optimized instruction or NULL.
  844. virtual MachineInstr *optimizeSelect(MachineInstr &MI,
  845. SmallPtrSetImpl<MachineInstr *> &NewMIs,
  846. bool PreferFalse = false) const {
  847. // This function must be implemented if Optimizable is ever set.
  848. llvm_unreachable("Target must implement TargetInstrInfo::optimizeSelect!");
  849. }
  850. /// Emit instructions to copy a pair of physical registers.
  851. ///
  852. /// This function should support copies within any legal register class as
  853. /// well as any cross-class copies created during instruction selection.
  854. ///
  855. /// The source and destination registers may overlap, which may require a
  856. /// careful implementation when multiple copy instructions are required for
  857. /// large registers. See for example the ARM target.
  858. virtual void copyPhysReg(MachineBasicBlock &MBB,
  859. MachineBasicBlock::iterator MI, const DebugLoc &DL,
  860. MCRegister DestReg, MCRegister SrcReg,
  861. bool KillSrc) const {
  862. llvm_unreachable("Target didn't implement TargetInstrInfo::copyPhysReg!");
  863. }
  864. /// Allow targets to tell MachineVerifier whether a specific register
  865. /// MachineOperand can be used as part of PC-relative addressing.
  866. /// PC-relative addressing modes in many CISC architectures contain
  867. /// (non-PC) registers as offsets or scaling values, which inherently
  868. /// tags the corresponding MachineOperand with OPERAND_PCREL.
  869. ///
  870. /// @param MO The MachineOperand in question. MO.isReg() should always
  871. /// be true.
  872. /// @return Whether this operand is allowed to be used PC-relatively.
  873. virtual bool isPCRelRegisterOperandLegal(const MachineOperand &MO) const {
  874. return false;
  875. }
  876. protected:
  877. /// Target-dependent implementation for IsCopyInstr.
  878. /// If the specific machine instruction is a instruction that moves/copies
  879. /// value from one register to another register return destination and source
  880. /// registers as machine operands.
  881. virtual Optional<DestSourcePair>
  882. isCopyInstrImpl(const MachineInstr &MI) const {
  883. return None;
  884. }
  885. /// Return true if the given terminator MI is not expected to spill. This
  886. /// sets the live interval as not spillable and adjusts phi node lowering to
  887. /// not introduce copies after the terminator. Use with care, these are
  888. /// currently used for hardware loop intrinsics in very controlled situations,
  889. /// created prior to registry allocation in loops that only have single phi
  890. /// users for the terminators value. They may run out of registers if not used
  891. /// carefully.
  892. virtual bool isUnspillableTerminatorImpl(const MachineInstr *MI) const {
  893. return false;
  894. }
  895. public:
  896. /// If the specific machine instruction is a instruction that moves/copies
  897. /// value from one register to another register return destination and source
  898. /// registers as machine operands.
  899. /// For COPY-instruction the method naturally returns destination and source
  900. /// registers as machine operands, for all other instructions the method calls
  901. /// target-dependent implementation.
  902. Optional<DestSourcePair> isCopyInstr(const MachineInstr &MI) const {
  903. if (MI.isCopy()) {
  904. return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
  905. }
  906. return isCopyInstrImpl(MI);
  907. }
  908. /// If the specific machine instruction is an instruction that adds an
  909. /// immediate value and a physical register, and stores the result in
  910. /// the given physical register \c Reg, return a pair of the source
  911. /// register and the offset which has been added.
  912. virtual Optional<RegImmPair> isAddImmediate(const MachineInstr &MI,
  913. Register Reg) const {
  914. return None;
  915. }
  916. /// Returns true if MI is an instruction that defines Reg to have a constant
  917. /// value and the value is recorded in ImmVal. The ImmVal is a result that
  918. /// should be interpreted as modulo size of Reg.
  919. virtual bool getConstValDefinedInReg(const MachineInstr &MI,
  920. const Register Reg,
  921. int64_t &ImmVal) const {
  922. return false;
  923. }
  924. /// Store the specified register of the given register class to the specified
  925. /// stack frame index. The store instruction is to be added to the given
  926. /// machine basic block before the specified machine instruction. If isKill
  927. /// is true, the register operand is the last use and must be marked kill.
  928. virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
  929. MachineBasicBlock::iterator MI,
  930. Register SrcReg, bool isKill, int FrameIndex,
  931. const TargetRegisterClass *RC,
  932. const TargetRegisterInfo *TRI) const {
  933. llvm_unreachable("Target didn't implement "
  934. "TargetInstrInfo::storeRegToStackSlot!");
  935. }
  936. /// Load the specified register of the given register class from the specified
  937. /// stack frame index. The load instruction is to be added to the given
  938. /// machine basic block before the specified machine instruction.
  939. virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
  940. MachineBasicBlock::iterator MI,
  941. Register DestReg, int FrameIndex,
  942. const TargetRegisterClass *RC,
  943. const TargetRegisterInfo *TRI) const {
  944. llvm_unreachable("Target didn't implement "
  945. "TargetInstrInfo::loadRegFromStackSlot!");
  946. }
  947. /// This function is called for all pseudo instructions
  948. /// that remain after register allocation. Many pseudo instructions are
  949. /// created to help register allocation. This is the place to convert them
  950. /// into real instructions. The target can edit MI in place, or it can insert
  951. /// new instructions and erase MI. The function should return true if
  952. /// anything was changed.
  953. virtual bool expandPostRAPseudo(MachineInstr &MI) const { return false; }
  954. /// Check whether the target can fold a load that feeds a subreg operand
  955. /// (or a subreg operand that feeds a store).
  956. /// For example, X86 may want to return true if it can fold
  957. /// movl (%esp), %eax
  958. /// subb, %al, ...
  959. /// Into:
  960. /// subb (%esp), ...
  961. ///
  962. /// Ideally, we'd like the target implementation of foldMemoryOperand() to
  963. /// reject subregs - but since this behavior used to be enforced in the
  964. /// target-independent code, moving this responsibility to the targets
  965. /// has the potential of causing nasty silent breakage in out-of-tree targets.
  966. virtual bool isSubregFoldable() const { return false; }
  967. /// For a patchpoint, stackmap, or statepoint intrinsic, return the range of
  968. /// operands which can't be folded into stack references. Operands outside
  969. /// of the range are most likely foldable but it is not guaranteed.
  970. /// These instructions are unique in that stack references for some operands
  971. /// have the same execution cost (e.g. none) as the unfolded register forms.
  972. /// The ranged return is guaranteed to include all operands which can't be
  973. /// folded at zero cost.
  974. virtual std::pair<unsigned, unsigned>
  975. getPatchpointUnfoldableRange(const MachineInstr &MI) const;
  976. /// Attempt to fold a load or store of the specified stack
  977. /// slot into the specified machine instruction for the specified operand(s).
  978. /// If this is possible, a new instruction is returned with the specified
  979. /// operand folded, otherwise NULL is returned.
  980. /// The new instruction is inserted before MI, and the client is responsible
  981. /// for removing the old instruction.
  982. /// If VRM is passed, the assigned physregs can be inspected by target to
  983. /// decide on using an opcode (note that those assignments can still change).
  984. MachineInstr *foldMemoryOperand(MachineInstr &MI, ArrayRef<unsigned> Ops,
  985. int FI,
  986. LiveIntervals *LIS = nullptr,
  987. VirtRegMap *VRM = nullptr) const;
  988. /// Same as the previous version except it allows folding of any load and
  989. /// store from / to any address, not just from a specific stack slot.
  990. MachineInstr *foldMemoryOperand(MachineInstr &MI, ArrayRef<unsigned> Ops,
  991. MachineInstr &LoadMI,
  992. LiveIntervals *LIS = nullptr) const;
  993. /// Return true when there is potentially a faster code sequence
  994. /// for an instruction chain ending in \p Root. All potential patterns are
  995. /// returned in the \p Pattern vector. Pattern should be sorted in priority
  996. /// order since the pattern evaluator stops checking as soon as it finds a
  997. /// faster sequence.
  998. /// \param Root - Instruction that could be combined with one of its operands
  999. /// \param Patterns - Vector of possible combination patterns
  1000. virtual bool
  1001. getMachineCombinerPatterns(MachineInstr &Root,
  1002. SmallVectorImpl<MachineCombinerPattern> &Patterns,
  1003. bool DoRegPressureReduce) const;
  1004. /// Return true if target supports reassociation of instructions in machine
  1005. /// combiner pass to reduce register pressure for a given BB.
  1006. virtual bool
  1007. shouldReduceRegisterPressure(MachineBasicBlock *MBB,
  1008. RegisterClassInfo *RegClassInfo) const {
  1009. return false;
  1010. }
  1011. /// Fix up the placeholder we may add in genAlternativeCodeSequence().
  1012. virtual void
  1013. finalizeInsInstrs(MachineInstr &Root, MachineCombinerPattern &P,
  1014. SmallVectorImpl<MachineInstr *> &InsInstrs) const {}
  1015. /// Return true when a code sequence can improve throughput. It
  1016. /// should be called only for instructions in loops.
  1017. /// \param Pattern - combiner pattern
  1018. virtual bool isThroughputPattern(MachineCombinerPattern Pattern) const;
  1019. /// Return true if the input \P Inst is part of a chain of dependent ops
  1020. /// that are suitable for reassociation, otherwise return false.
  1021. /// If the instruction's operands must be commuted to have a previous
  1022. /// instruction of the same type define the first source operand, \P Commuted
  1023. /// will be set to true.
  1024. bool isReassociationCandidate(const MachineInstr &Inst, bool &Commuted) const;
  1025. /// Return true when \P Inst is both associative and commutative.
  1026. virtual bool isAssociativeAndCommutative(const MachineInstr &Inst) const {
  1027. return false;
  1028. }
  1029. /// Return true when \P Inst has reassociable operands in the same \P MBB.
  1030. virtual bool hasReassociableOperands(const MachineInstr &Inst,
  1031. const MachineBasicBlock *MBB) const;
  1032. /// Return true when \P Inst has reassociable sibling.
  1033. bool hasReassociableSibling(const MachineInstr &Inst, bool &Commuted) const;
  1034. /// When getMachineCombinerPatterns() finds patterns, this function generates
  1035. /// the instructions that could replace the original code sequence. The client
  1036. /// has to decide whether the actual replacement is beneficial or not.
  1037. /// \param Root - Instruction that could be combined with one of its operands
  1038. /// \param Pattern - Combination pattern for Root
  1039. /// \param InsInstrs - Vector of new instructions that implement P
  1040. /// \param DelInstrs - Old instructions, including Root, that could be
  1041. /// replaced by InsInstr
  1042. /// \param InstIdxForVirtReg - map of virtual register to instruction in
  1043. /// InsInstr that defines it
  1044. virtual void genAlternativeCodeSequence(
  1045. MachineInstr &Root, MachineCombinerPattern Pattern,
  1046. SmallVectorImpl<MachineInstr *> &InsInstrs,
  1047. SmallVectorImpl<MachineInstr *> &DelInstrs,
  1048. DenseMap<unsigned, unsigned> &InstIdxForVirtReg) const;
  1049. /// Attempt to reassociate \P Root and \P Prev according to \P Pattern to
  1050. /// reduce critical path length.
  1051. void reassociateOps(MachineInstr &Root, MachineInstr &Prev,
  1052. MachineCombinerPattern Pattern,
  1053. SmallVectorImpl<MachineInstr *> &InsInstrs,
  1054. SmallVectorImpl<MachineInstr *> &DelInstrs,
  1055. DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const;
  1056. /// The limit on resource length extension we accept in MachineCombiner Pass.
  1057. virtual int getExtendResourceLenLimit() const { return 0; }
  1058. /// This is an architecture-specific helper function of reassociateOps.
  1059. /// Set special operand attributes for new instructions after reassociation.
  1060. virtual void setSpecialOperandAttr(MachineInstr &OldMI1, MachineInstr &OldMI2,
  1061. MachineInstr &NewMI1,
  1062. MachineInstr &NewMI2) const {}
  1063. virtual void setSpecialOperandAttr(MachineInstr &MI, uint16_t Flags) const {}
  1064. /// Return true when a target supports MachineCombiner.
  1065. virtual bool useMachineCombiner() const { return false; }
  1066. /// Return true if the given SDNode can be copied during scheduling
  1067. /// even if it has glue.
  1068. virtual bool canCopyGluedNodeDuringSchedule(SDNode *N) const { return false; }
  1069. protected:
  1070. /// Target-dependent implementation for foldMemoryOperand.
  1071. /// Target-independent code in foldMemoryOperand will
  1072. /// take care of adding a MachineMemOperand to the newly created instruction.
  1073. /// The instruction and any auxiliary instructions necessary will be inserted
  1074. /// at InsertPt.
  1075. virtual MachineInstr *
  1076. foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
  1077. ArrayRef<unsigned> Ops,
  1078. MachineBasicBlock::iterator InsertPt, int FrameIndex,
  1079. LiveIntervals *LIS = nullptr,
  1080. VirtRegMap *VRM = nullptr) const {
  1081. return nullptr;
  1082. }
  1083. /// Target-dependent implementation for foldMemoryOperand.
  1084. /// Target-independent code in foldMemoryOperand will
  1085. /// take care of adding a MachineMemOperand to the newly created instruction.
  1086. /// The instruction and any auxiliary instructions necessary will be inserted
  1087. /// at InsertPt.
  1088. virtual MachineInstr *foldMemoryOperandImpl(
  1089. MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
  1090. MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
  1091. LiveIntervals *LIS = nullptr) const {
  1092. return nullptr;
  1093. }
  1094. /// Target-dependent implementation of getRegSequenceInputs.
  1095. ///
  1096. /// \returns true if it is possible to build the equivalent
  1097. /// REG_SEQUENCE inputs with the pair \p MI, \p DefIdx. False otherwise.
  1098. ///
  1099. /// \pre MI.isRegSequenceLike().
  1100. ///
  1101. /// \see TargetInstrInfo::getRegSequenceInputs.
  1102. virtual bool getRegSequenceLikeInputs(
  1103. const MachineInstr &MI, unsigned DefIdx,
  1104. SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
  1105. return false;
  1106. }
  1107. /// Target-dependent implementation of getExtractSubregInputs.
  1108. ///
  1109. /// \returns true if it is possible to build the equivalent
  1110. /// EXTRACT_SUBREG inputs with the pair \p MI, \p DefIdx. False otherwise.
  1111. ///
  1112. /// \pre MI.isExtractSubregLike().
  1113. ///
  1114. /// \see TargetInstrInfo::getExtractSubregInputs.
  1115. virtual bool getExtractSubregLikeInputs(const MachineInstr &MI,
  1116. unsigned DefIdx,
  1117. RegSubRegPairAndIdx &InputReg) const {
  1118. return false;
  1119. }
  1120. /// Target-dependent implementation of getInsertSubregInputs.
  1121. ///
  1122. /// \returns true if it is possible to build the equivalent
  1123. /// INSERT_SUBREG inputs with the pair \p MI, \p DefIdx. False otherwise.
  1124. ///
  1125. /// \pre MI.isInsertSubregLike().
  1126. ///
  1127. /// \see TargetInstrInfo::getInsertSubregInputs.
  1128. virtual bool
  1129. getInsertSubregLikeInputs(const MachineInstr &MI, unsigned DefIdx,
  1130. RegSubRegPair &BaseReg,
  1131. RegSubRegPairAndIdx &InsertedReg) const {
  1132. return false;
  1133. }
  1134. public:
  1135. /// getAddressSpaceForPseudoSourceKind - Given the kind of memory
  1136. /// (e.g. stack) the target returns the corresponding address space.
  1137. virtual unsigned
  1138. getAddressSpaceForPseudoSourceKind(unsigned Kind) const {
  1139. return 0;
  1140. }
  1141. /// unfoldMemoryOperand - Separate a single instruction which folded a load or
  1142. /// a store or a load and a store into two or more instruction. If this is
  1143. /// possible, returns true as well as the new instructions by reference.
  1144. virtual bool
  1145. unfoldMemoryOperand(MachineFunction &MF, MachineInstr &MI, unsigned Reg,
  1146. bool UnfoldLoad, bool UnfoldStore,
  1147. SmallVectorImpl<MachineInstr *> &NewMIs) const {
  1148. return false;
  1149. }
  1150. virtual bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
  1151. SmallVectorImpl<SDNode *> &NewNodes) const {
  1152. return false;
  1153. }
  1154. /// Returns the opcode of the would be new
  1155. /// instruction after load / store are unfolded from an instruction of the
  1156. /// specified opcode. It returns zero if the specified unfolding is not
  1157. /// possible. If LoadRegIndex is non-null, it is filled in with the operand
  1158. /// index of the operand which will hold the register holding the loaded
  1159. /// value.
  1160. virtual unsigned
  1161. getOpcodeAfterMemoryUnfold(unsigned Opc, bool UnfoldLoad, bool UnfoldStore,
  1162. unsigned *LoadRegIndex = nullptr) const {
  1163. return 0;
  1164. }
  1165. /// This is used by the pre-regalloc scheduler to determine if two loads are
  1166. /// loading from the same base address. It should only return true if the base
  1167. /// pointers are the same and the only differences between the two addresses
  1168. /// are the offset. It also returns the offsets by reference.
  1169. virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
  1170. int64_t &Offset1,
  1171. int64_t &Offset2) const {
  1172. return false;
  1173. }
  1174. /// This is a used by the pre-regalloc scheduler to determine (in conjunction
  1175. /// with areLoadsFromSameBasePtr) if two loads should be scheduled together.
  1176. /// On some targets if two loads are loading from
  1177. /// addresses in the same cache line, it's better if they are scheduled
  1178. /// together. This function takes two integers that represent the load offsets
  1179. /// from the common base address. It returns true if it decides it's desirable
  1180. /// to schedule the two loads together. "NumLoads" is the number of loads that
  1181. /// have already been scheduled after Load1.
  1182. virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
  1183. int64_t Offset1, int64_t Offset2,
  1184. unsigned NumLoads) const {
  1185. return false;
  1186. }
  1187. /// Get the base operand and byte offset of an instruction that reads/writes
  1188. /// memory. This is a convenience function for callers that are only prepared
  1189. /// to handle a single base operand.
  1190. bool getMemOperandWithOffset(const MachineInstr &MI,
  1191. const MachineOperand *&BaseOp, int64_t &Offset,
  1192. bool &OffsetIsScalable,
  1193. const TargetRegisterInfo *TRI) const;
  1194. /// Get zero or more base operands and the byte offset of an instruction that
  1195. /// reads/writes memory. Note that there may be zero base operands if the
  1196. /// instruction accesses a constant address.
  1197. /// It returns false if MI does not read/write memory.
  1198. /// It returns false if base operands and offset could not be determined.
  1199. /// It is not guaranteed to always recognize base operands and offsets in all
  1200. /// cases.
  1201. virtual bool getMemOperandsWithOffsetWidth(
  1202. const MachineInstr &MI, SmallVectorImpl<const MachineOperand *> &BaseOps,
  1203. int64_t &Offset, bool &OffsetIsScalable, unsigned &Width,
  1204. const TargetRegisterInfo *TRI) const {
  1205. return false;
  1206. }
  1207. /// Return true if the instruction contains a base register and offset. If
  1208. /// true, the function also sets the operand position in the instruction
  1209. /// for the base register and offset.
  1210. virtual bool getBaseAndOffsetPosition(const MachineInstr &MI,
  1211. unsigned &BasePos,
  1212. unsigned &OffsetPos) const {
  1213. return false;
  1214. }
  1215. /// Target dependent implementation to get the values constituting the address
  1216. /// MachineInstr that is accessing memory. These values are returned as a
  1217. /// struct ExtAddrMode which contains all relevant information to make up the
  1218. /// address.
  1219. virtual Optional<ExtAddrMode>
  1220. getAddrModeFromMemoryOp(const MachineInstr &MemI,
  1221. const TargetRegisterInfo *TRI) const {
  1222. return None;
  1223. }
  1224. /// Returns true if MI's Def is NullValueReg, and the MI
  1225. /// does not change the Zero value. i.e. cases such as rax = shr rax, X where
  1226. /// NullValueReg = rax. Note that if the NullValueReg is non-zero, this
  1227. /// function can return true even if becomes zero. Specifically cases such as
  1228. /// NullValueReg = shl NullValueReg, 63.
  1229. virtual bool preservesZeroValueInReg(const MachineInstr *MI,
  1230. const Register NullValueReg,
  1231. const TargetRegisterInfo *TRI) const {
  1232. return false;
  1233. }
  1234. /// If the instruction is an increment of a constant value, return the amount.
  1235. virtual bool getIncrementValue(const MachineInstr &MI, int &Value) const {
  1236. return false;
  1237. }
  1238. /// Returns true if the two given memory operations should be scheduled
  1239. /// adjacent. Note that you have to add:
  1240. /// DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI));
  1241. /// or
  1242. /// DAG->addMutation(createStoreClusterDAGMutation(DAG->TII, DAG->TRI));
  1243. /// to TargetPassConfig::createMachineScheduler() to have an effect.
  1244. ///
  1245. /// \p BaseOps1 and \p BaseOps2 are memory operands of two memory operations.
  1246. /// \p NumLoads is the number of loads that will be in the cluster if this
  1247. /// hook returns true.
  1248. /// \p NumBytes is the number of bytes that will be loaded from all the
  1249. /// clustered loads if this hook returns true.
  1250. virtual bool shouldClusterMemOps(ArrayRef<const MachineOperand *> BaseOps1,
  1251. ArrayRef<const MachineOperand *> BaseOps2,
  1252. unsigned NumLoads, unsigned NumBytes) const {
  1253. llvm_unreachable("target did not implement shouldClusterMemOps()");
  1254. }
  1255. /// Reverses the branch condition of the specified condition list,
  1256. /// returning false on success and true if it cannot be reversed.
  1257. virtual bool
  1258. reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
  1259. return true;
  1260. }
  1261. /// Insert a noop into the instruction stream at the specified point.
  1262. virtual void insertNoop(MachineBasicBlock &MBB,
  1263. MachineBasicBlock::iterator MI) const;
  1264. /// Insert noops into the instruction stream at the specified point.
  1265. virtual void insertNoops(MachineBasicBlock &MBB,
  1266. MachineBasicBlock::iterator MI,
  1267. unsigned Quantity) const;
  1268. /// Return the noop instruction to use for a noop.
  1269. virtual MCInst getNop() const;
  1270. /// Return true for post-incremented instructions.
  1271. virtual bool isPostIncrement(const MachineInstr &MI) const { return false; }
  1272. /// Returns true if the instruction is already predicated.
  1273. virtual bool isPredicated(const MachineInstr &MI) const { return false; }
  1274. // Returns a MIRPrinter comment for this machine operand.
  1275. virtual std::string
  1276. createMIROperandComment(const MachineInstr &MI, const MachineOperand &Op,
  1277. unsigned OpIdx, const TargetRegisterInfo *TRI) const;
  1278. /// Returns true if the instruction is a
  1279. /// terminator instruction that has not been predicated.
  1280. bool isUnpredicatedTerminator(const MachineInstr &MI) const;
  1281. /// Returns true if MI is an unconditional tail call.
  1282. virtual bool isUnconditionalTailCall(const MachineInstr &MI) const {
  1283. return false;
  1284. }
  1285. /// Returns true if the tail call can be made conditional on BranchCond.
  1286. virtual bool canMakeTailCallConditional(SmallVectorImpl<MachineOperand> &Cond,
  1287. const MachineInstr &TailCall) const {
  1288. return false;
  1289. }
  1290. /// Replace the conditional branch in MBB with a conditional tail call.
  1291. virtual void replaceBranchWithTailCall(MachineBasicBlock &MBB,
  1292. SmallVectorImpl<MachineOperand> &Cond,
  1293. const MachineInstr &TailCall) const {
  1294. llvm_unreachable("Target didn't implement replaceBranchWithTailCall!");
  1295. }
  1296. /// Convert the instruction into a predicated instruction.
  1297. /// It returns true if the operation was successful.
  1298. virtual bool PredicateInstruction(MachineInstr &MI,
  1299. ArrayRef<MachineOperand> Pred) const;
  1300. /// Returns true if the first specified predicate
  1301. /// subsumes the second, e.g. GE subsumes GT.
  1302. virtual bool SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
  1303. ArrayRef<MachineOperand> Pred2) const {
  1304. return false;
  1305. }
  1306. /// If the specified instruction defines any predicate
  1307. /// or condition code register(s) used for predication, returns true as well
  1308. /// as the definition predicate(s) by reference.
  1309. /// SkipDead should be set to false at any point that dead
  1310. /// predicate instructions should be considered as being defined.
  1311. /// A dead predicate instruction is one that is guaranteed to be removed
  1312. /// after a call to PredicateInstruction.
  1313. virtual bool ClobbersPredicate(MachineInstr &MI,
  1314. std::vector<MachineOperand> &Pred,
  1315. bool SkipDead) const {
  1316. return false;
  1317. }
  1318. /// Return true if the specified instruction can be predicated.
  1319. /// By default, this returns true for every instruction with a
  1320. /// PredicateOperand.
  1321. virtual bool isPredicable(const MachineInstr &MI) const {
  1322. return MI.getDesc().isPredicable();
  1323. }
  1324. /// Return true if it's safe to move a machine
  1325. /// instruction that defines the specified register class.
  1326. virtual bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
  1327. return true;
  1328. }
  1329. /// Test if the given instruction should be considered a scheduling boundary.
  1330. /// This primarily includes labels and terminators.
  1331. virtual bool isSchedulingBoundary(const MachineInstr &MI,
  1332. const MachineBasicBlock *MBB,
  1333. const MachineFunction &MF) const;
  1334. /// Measure the specified inline asm to determine an approximation of its
  1335. /// length.
  1336. virtual unsigned getInlineAsmLength(
  1337. const char *Str, const MCAsmInfo &MAI,
  1338. const TargetSubtargetInfo *STI = nullptr) const;
  1339. /// Allocate and return a hazard recognizer to use for this target when
  1340. /// scheduling the machine instructions before register allocation.
  1341. virtual ScheduleHazardRecognizer *
  1342. CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
  1343. const ScheduleDAG *DAG) const;
  1344. /// Allocate and return a hazard recognizer to use for this target when
  1345. /// scheduling the machine instructions before register allocation.
  1346. virtual ScheduleHazardRecognizer *
  1347. CreateTargetMIHazardRecognizer(const InstrItineraryData *,
  1348. const ScheduleDAGMI *DAG) const;
  1349. /// Allocate and return a hazard recognizer to use for this target when
  1350. /// scheduling the machine instructions after register allocation.
  1351. virtual ScheduleHazardRecognizer *
  1352. CreateTargetPostRAHazardRecognizer(const InstrItineraryData *,
  1353. const ScheduleDAG *DAG) const;
  1354. /// Allocate and return a hazard recognizer to use for by non-scheduling
  1355. /// passes.
  1356. virtual ScheduleHazardRecognizer *
  1357. CreateTargetPostRAHazardRecognizer(const MachineFunction &MF) const {
  1358. return nullptr;
  1359. }
  1360. /// Provide a global flag for disabling the PreRA hazard recognizer that
  1361. /// targets may choose to honor.
  1362. bool usePreRAHazardRecognizer() const;
  1363. /// For a comparison instruction, return the source registers
  1364. /// in SrcReg and SrcReg2 if having two register operands, and the value it
  1365. /// compares against in CmpValue. Return true if the comparison instruction
  1366. /// can be analyzed.
  1367. virtual bool analyzeCompare(const MachineInstr &MI, Register &SrcReg,
  1368. Register &SrcReg2, int &Mask, int &Value) const {
  1369. return false;
  1370. }
  1371. /// See if the comparison instruction can be converted
  1372. /// into something more efficient. E.g., on ARM most instructions can set the
  1373. /// flags register, obviating the need for a separate CMP.
  1374. virtual bool optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
  1375. Register SrcReg2, int Mask, int Value,
  1376. const MachineRegisterInfo *MRI) const {
  1377. return false;
  1378. }
  1379. virtual bool optimizeCondBranch(MachineInstr &MI) const { return false; }
  1380. /// Try to remove the load by folding it to a register operand at the use.
  1381. /// We fold the load instructions if and only if the
  1382. /// def and use are in the same BB. We only look at one load and see
  1383. /// whether it can be folded into MI. FoldAsLoadDefReg is the virtual register
  1384. /// defined by the load we are trying to fold. DefMI returns the machine
  1385. /// instruction that defines FoldAsLoadDefReg, and the function returns
  1386. /// the machine instruction generated due to folding.
  1387. virtual MachineInstr *optimizeLoadInstr(MachineInstr &MI,
  1388. const MachineRegisterInfo *MRI,
  1389. Register &FoldAsLoadDefReg,
  1390. MachineInstr *&DefMI) const {
  1391. return nullptr;
  1392. }
  1393. /// 'Reg' is known to be defined by a move immediate instruction,
  1394. /// try to fold the immediate into the use instruction.
  1395. /// If MRI->hasOneNonDBGUse(Reg) is true, and this function returns true,
  1396. /// then the caller may assume that DefMI has been erased from its parent
  1397. /// block. The caller may assume that it will not be erased by this
  1398. /// function otherwise.
  1399. virtual bool FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
  1400. Register Reg, MachineRegisterInfo *MRI) const {
  1401. return false;
  1402. }
  1403. /// Return the number of u-operations the given machine
  1404. /// instruction will be decoded to on the target cpu. The itinerary's
  1405. /// IssueWidth is the number of microops that can be dispatched each
  1406. /// cycle. An instruction with zero microops takes no dispatch resources.
  1407. virtual unsigned getNumMicroOps(const InstrItineraryData *ItinData,
  1408. const MachineInstr &MI) const;
  1409. /// Return true for pseudo instructions that don't consume any
  1410. /// machine resources in their current form. These are common cases that the
  1411. /// scheduler should consider free, rather than conservatively handling them
  1412. /// as instructions with no itinerary.
  1413. bool isZeroCost(unsigned Opcode) const {
  1414. return Opcode <= TargetOpcode::COPY;
  1415. }
  1416. virtual int getOperandLatency(const InstrItineraryData *ItinData,
  1417. SDNode *DefNode, unsigned DefIdx,
  1418. SDNode *UseNode, unsigned UseIdx) const;
  1419. /// Compute and return the use operand latency of a given pair of def and use.
  1420. /// In most cases, the static scheduling itinerary was enough to determine the
  1421. /// operand latency. But it may not be possible for instructions with variable
  1422. /// number of defs / uses.
  1423. ///
  1424. /// This is a raw interface to the itinerary that may be directly overridden
  1425. /// by a target. Use computeOperandLatency to get the best estimate of
  1426. /// latency.
  1427. virtual int getOperandLatency(const InstrItineraryData *ItinData,
  1428. const MachineInstr &DefMI, unsigned DefIdx,
  1429. const MachineInstr &UseMI,
  1430. unsigned UseIdx) const;
  1431. /// Compute the instruction latency of a given instruction.
  1432. /// If the instruction has higher cost when predicated, it's returned via
  1433. /// PredCost.
  1434. virtual unsigned getInstrLatency(const InstrItineraryData *ItinData,
  1435. const MachineInstr &MI,
  1436. unsigned *PredCost = nullptr) const;
  1437. virtual unsigned getPredicationCost(const MachineInstr &MI) const;
  1438. virtual int getInstrLatency(const InstrItineraryData *ItinData,
  1439. SDNode *Node) const;
  1440. /// Return the default expected latency for a def based on its opcode.
  1441. unsigned defaultDefLatency(const MCSchedModel &SchedModel,
  1442. const MachineInstr &DefMI) const;
  1443. int computeDefOperandLatency(const InstrItineraryData *ItinData,
  1444. const MachineInstr &DefMI) const;
  1445. /// Return true if this opcode has high latency to its result.
  1446. virtual bool isHighLatencyDef(int opc) const { return false; }
  1447. /// Compute operand latency between a def of 'Reg'
  1448. /// and a use in the current loop. Return true if the target considered
  1449. /// it 'high'. This is used by optimization passes such as machine LICM to
  1450. /// determine whether it makes sense to hoist an instruction out even in a
  1451. /// high register pressure situation.
  1452. virtual bool hasHighOperandLatency(const TargetSchedModel &SchedModel,
  1453. const MachineRegisterInfo *MRI,
  1454. const MachineInstr &DefMI, unsigned DefIdx,
  1455. const MachineInstr &UseMI,
  1456. unsigned UseIdx) const {
  1457. return false;
  1458. }
  1459. /// Compute operand latency of a def of 'Reg'. Return true
  1460. /// if the target considered it 'low'.
  1461. virtual bool hasLowDefLatency(const TargetSchedModel &SchedModel,
  1462. const MachineInstr &DefMI,
  1463. unsigned DefIdx) const;
  1464. /// Perform target-specific instruction verification.
  1465. virtual bool verifyInstruction(const MachineInstr &MI,
  1466. StringRef &ErrInfo) const {
  1467. return true;
  1468. }
  1469. /// Return the current execution domain and bit mask of
  1470. /// possible domains for instruction.
  1471. ///
  1472. /// Some micro-architectures have multiple execution domains, and multiple
  1473. /// opcodes that perform the same operation in different domains. For
  1474. /// example, the x86 architecture provides the por, orps, and orpd
  1475. /// instructions that all do the same thing. There is a latency penalty if a
  1476. /// register is written in one domain and read in another.
  1477. ///
  1478. /// This function returns a pair (domain, mask) containing the execution
  1479. /// domain of MI, and a bit mask of possible domains. The setExecutionDomain
  1480. /// function can be used to change the opcode to one of the domains in the
  1481. /// bit mask. Instructions whose execution domain can't be changed should
  1482. /// return a 0 mask.
  1483. ///
  1484. /// The execution domain numbers don't have any special meaning except domain
  1485. /// 0 is used for instructions that are not associated with any interesting
  1486. /// execution domain.
  1487. ///
  1488. virtual std::pair<uint16_t, uint16_t>
  1489. getExecutionDomain(const MachineInstr &MI) const {
  1490. return std::make_pair(0, 0);
  1491. }
  1492. /// Change the opcode of MI to execute in Domain.
  1493. ///
  1494. /// The bit (1 << Domain) must be set in the mask returned from
  1495. /// getExecutionDomain(MI).
  1496. virtual void setExecutionDomain(MachineInstr &MI, unsigned Domain) const {}
  1497. /// Returns the preferred minimum clearance
  1498. /// before an instruction with an unwanted partial register update.
  1499. ///
  1500. /// Some instructions only write part of a register, and implicitly need to
  1501. /// read the other parts of the register. This may cause unwanted stalls
  1502. /// preventing otherwise unrelated instructions from executing in parallel in
  1503. /// an out-of-order CPU.
  1504. ///
  1505. /// For example, the x86 instruction cvtsi2ss writes its result to bits
  1506. /// [31:0] of the destination xmm register. Bits [127:32] are unaffected, so
  1507. /// the instruction needs to wait for the old value of the register to become
  1508. /// available:
  1509. ///
  1510. /// addps %xmm1, %xmm0
  1511. /// movaps %xmm0, (%rax)
  1512. /// cvtsi2ss %rbx, %xmm0
  1513. ///
  1514. /// In the code above, the cvtsi2ss instruction needs to wait for the addps
  1515. /// instruction before it can issue, even though the high bits of %xmm0
  1516. /// probably aren't needed.
  1517. ///
  1518. /// This hook returns the preferred clearance before MI, measured in
  1519. /// instructions. Other defs of MI's operand OpNum are avoided in the last N
  1520. /// instructions before MI. It should only return a positive value for
  1521. /// unwanted dependencies. If the old bits of the defined register have
  1522. /// useful values, or if MI is determined to otherwise read the dependency,
  1523. /// the hook should return 0.
  1524. ///
  1525. /// The unwanted dependency may be handled by:
  1526. ///
  1527. /// 1. Allocating the same register for an MI def and use. That makes the
  1528. /// unwanted dependency identical to a required dependency.
  1529. ///
  1530. /// 2. Allocating a register for the def that has no defs in the previous N
  1531. /// instructions.
  1532. ///
  1533. /// 3. Calling breakPartialRegDependency() with the same arguments. This
  1534. /// allows the target to insert a dependency breaking instruction.
  1535. ///
  1536. virtual unsigned
  1537. getPartialRegUpdateClearance(const MachineInstr &MI, unsigned OpNum,
  1538. const TargetRegisterInfo *TRI) const {
  1539. // The default implementation returns 0 for no partial register dependency.
  1540. return 0;
  1541. }
  1542. /// Return the minimum clearance before an instruction that reads an
  1543. /// unused register.
  1544. ///
  1545. /// For example, AVX instructions may copy part of a register operand into
  1546. /// the unused high bits of the destination register.
  1547. ///
  1548. /// vcvtsi2sdq %rax, undef %xmm0, %xmm14
  1549. ///
  1550. /// In the code above, vcvtsi2sdq copies %xmm0[127:64] into %xmm14 creating a
  1551. /// false dependence on any previous write to %xmm0.
  1552. ///
  1553. /// This hook works similarly to getPartialRegUpdateClearance, except that it
  1554. /// does not take an operand index. Instead sets \p OpNum to the index of the
  1555. /// unused register.
  1556. virtual unsigned getUndefRegClearance(const MachineInstr &MI, unsigned OpNum,
  1557. const TargetRegisterInfo *TRI) const {
  1558. // The default implementation returns 0 for no undef register dependency.
  1559. return 0;
  1560. }
  1561. /// Insert a dependency-breaking instruction
  1562. /// before MI to eliminate an unwanted dependency on OpNum.
  1563. ///
  1564. /// If it wasn't possible to avoid a def in the last N instructions before MI
  1565. /// (see getPartialRegUpdateClearance), this hook will be called to break the
  1566. /// unwanted dependency.
  1567. ///
  1568. /// On x86, an xorps instruction can be used as a dependency breaker:
  1569. ///
  1570. /// addps %xmm1, %xmm0
  1571. /// movaps %xmm0, (%rax)
  1572. /// xorps %xmm0, %xmm0
  1573. /// cvtsi2ss %rbx, %xmm0
  1574. ///
  1575. /// An <imp-kill> operand should be added to MI if an instruction was
  1576. /// inserted. This ties the instructions together in the post-ra scheduler.
  1577. ///
  1578. virtual void breakPartialRegDependency(MachineInstr &MI, unsigned OpNum,
  1579. const TargetRegisterInfo *TRI) const {}
  1580. /// Create machine specific model for scheduling.
  1581. virtual DFAPacketizer *
  1582. CreateTargetScheduleState(const TargetSubtargetInfo &) const {
  1583. return nullptr;
  1584. }
  1585. /// Sometimes, it is possible for the target
  1586. /// to tell, even without aliasing information, that two MIs access different
  1587. /// memory addresses. This function returns true if two MIs access different
  1588. /// memory addresses and false otherwise.
  1589. ///
  1590. /// Assumes any physical registers used to compute addresses have the same
  1591. /// value for both instructions. (This is the most useful assumption for
  1592. /// post-RA scheduling.)
  1593. ///
  1594. /// See also MachineInstr::mayAlias, which is implemented on top of this
  1595. /// function.
  1596. virtual bool
  1597. areMemAccessesTriviallyDisjoint(const MachineInstr &MIa,
  1598. const MachineInstr &MIb) const {
  1599. assert(MIa.mayLoadOrStore() &&
  1600. "MIa must load from or modify a memory location");
  1601. assert(MIb.mayLoadOrStore() &&
  1602. "MIb must load from or modify a memory location");
  1603. return false;
  1604. }
  1605. /// Return the value to use for the MachineCSE's LookAheadLimit,
  1606. /// which is a heuristic used for CSE'ing phys reg defs.
  1607. virtual unsigned getMachineCSELookAheadLimit() const {
  1608. // The default lookahead is small to prevent unprofitable quadratic
  1609. // behavior.
  1610. return 5;
  1611. }
  1612. /// Return the maximal number of alias checks on memory operands. For
  1613. /// instructions with more than one memory operands, the alias check on a
  1614. /// single MachineInstr pair has quadratic overhead and results in
  1615. /// unacceptable performance in the worst case. The limit here is to clamp
  1616. /// that maximal checks performed. Usually, that's the product of memory
  1617. /// operand numbers from that pair of MachineInstr to be checked. For
  1618. /// instance, with two MachineInstrs with 4 and 5 memory operands
  1619. /// correspondingly, a total of 20 checks are required. With this limit set to
  1620. /// 16, their alias check is skipped. We choose to limit the product instead
  1621. /// of the individual instruction as targets may have special MachineInstrs
  1622. /// with a considerably high number of memory operands, such as `ldm` in ARM.
  1623. /// Setting this limit per MachineInstr would result in either too high
  1624. /// overhead or too rigid restriction.
  1625. virtual unsigned getMemOperandAACheckLimit() const { return 16; }
  1626. /// Return an array that contains the ids of the target indices (used for the
  1627. /// TargetIndex machine operand) and their names.
  1628. ///
  1629. /// MIR Serialization is able to serialize only the target indices that are
  1630. /// defined by this method.
  1631. virtual ArrayRef<std::pair<int, const char *>>
  1632. getSerializableTargetIndices() const {
  1633. return None;
  1634. }
  1635. /// Decompose the machine operand's target flags into two values - the direct
  1636. /// target flag value and any of bit flags that are applied.
  1637. virtual std::pair<unsigned, unsigned>
  1638. decomposeMachineOperandsTargetFlags(unsigned /*TF*/) const {
  1639. return std::make_pair(0u, 0u);
  1640. }
  1641. /// Return an array that contains the direct target flag values and their
  1642. /// names.
  1643. ///
  1644. /// MIR Serialization is able to serialize only the target flags that are
  1645. /// defined by this method.
  1646. virtual ArrayRef<std::pair<unsigned, const char *>>
  1647. getSerializableDirectMachineOperandTargetFlags() const {
  1648. return None;
  1649. }
  1650. /// Return an array that contains the bitmask target flag values and their
  1651. /// names.
  1652. ///
  1653. /// MIR Serialization is able to serialize only the target flags that are
  1654. /// defined by this method.
  1655. virtual ArrayRef<std::pair<unsigned, const char *>>
  1656. getSerializableBitmaskMachineOperandTargetFlags() const {
  1657. return None;
  1658. }
  1659. /// Return an array that contains the MMO target flag values and their
  1660. /// names.
  1661. ///
  1662. /// MIR Serialization is able to serialize only the MMO target flags that are
  1663. /// defined by this method.
  1664. virtual ArrayRef<std::pair<MachineMemOperand::Flags, const char *>>
  1665. getSerializableMachineMemOperandTargetFlags() const {
  1666. return None;
  1667. }
  1668. /// Determines whether \p Inst is a tail call instruction. Override this
  1669. /// method on targets that do not properly set MCID::Return and MCID::Call on
  1670. /// tail call instructions."
  1671. virtual bool isTailCall(const MachineInstr &Inst) const {
  1672. return Inst.isReturn() && Inst.isCall();
  1673. }
  1674. /// True if the instruction is bound to the top of its basic block and no
  1675. /// other instructions shall be inserted before it. This can be implemented
  1676. /// to prevent register allocator to insert spills before such instructions.
  1677. virtual bool isBasicBlockPrologue(const MachineInstr &MI) const {
  1678. return false;
  1679. }
  1680. /// During PHI eleimination lets target to make necessary checks and
  1681. /// insert the copy to the PHI destination register in a target specific
  1682. /// manner.
  1683. virtual MachineInstr *createPHIDestinationCopy(
  1684. MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt,
  1685. const DebugLoc &DL, Register Src, Register Dst) const {
  1686. return BuildMI(MBB, InsPt, DL, get(TargetOpcode::COPY), Dst)
  1687. .addReg(Src);
  1688. }
  1689. /// During PHI eleimination lets target to make necessary checks and
  1690. /// insert the copy to the PHI destination register in a target specific
  1691. /// manner.
  1692. virtual MachineInstr *createPHISourceCopy(MachineBasicBlock &MBB,
  1693. MachineBasicBlock::iterator InsPt,
  1694. const DebugLoc &DL, Register Src,
  1695. unsigned SrcSubReg,
  1696. Register Dst) const {
  1697. return BuildMI(MBB, InsPt, DL, get(TargetOpcode::COPY), Dst)
  1698. .addReg(Src, 0, SrcSubReg);
  1699. }
  1700. /// Returns a \p outliner::OutlinedFunction struct containing target-specific
  1701. /// information for a set of outlining candidates.
  1702. virtual outliner::OutlinedFunction getOutliningCandidateInfo(
  1703. std::vector<outliner::Candidate> &RepeatedSequenceLocs) const {
  1704. llvm_unreachable(
  1705. "Target didn't implement TargetInstrInfo::getOutliningCandidateInfo!");
  1706. }
  1707. /// Returns how or if \p MI should be outlined.
  1708. virtual outliner::InstrType
  1709. getOutliningType(MachineBasicBlock::iterator &MIT, unsigned Flags) const {
  1710. llvm_unreachable(
  1711. "Target didn't implement TargetInstrInfo::getOutliningType!");
  1712. }
  1713. /// Optional target hook that returns true if \p MBB is safe to outline from,
  1714. /// and returns any target-specific information in \p Flags.
  1715. virtual bool isMBBSafeToOutlineFrom(MachineBasicBlock &MBB,
  1716. unsigned &Flags) const {
  1717. return true;
  1718. }
  1719. /// Insert a custom frame for outlined functions.
  1720. virtual void buildOutlinedFrame(MachineBasicBlock &MBB, MachineFunction &MF,
  1721. const outliner::OutlinedFunction &OF) const {
  1722. llvm_unreachable(
  1723. "Target didn't implement TargetInstrInfo::buildOutlinedFrame!");
  1724. }
  1725. /// Insert a call to an outlined function into the program.
  1726. /// Returns an iterator to the spot where we inserted the call. This must be
  1727. /// implemented by the target.
  1728. virtual MachineBasicBlock::iterator
  1729. insertOutlinedCall(Module &M, MachineBasicBlock &MBB,
  1730. MachineBasicBlock::iterator &It, MachineFunction &MF,
  1731. const outliner::Candidate &C) const {
  1732. llvm_unreachable(
  1733. "Target didn't implement TargetInstrInfo::insertOutlinedCall!");
  1734. }
  1735. /// Return true if the function can safely be outlined from.
  1736. /// A function \p MF is considered safe for outlining if an outlined function
  1737. /// produced from instructions in F will produce a program which produces the
  1738. /// same output for any set of given inputs.
  1739. virtual bool isFunctionSafeToOutlineFrom(MachineFunction &MF,
  1740. bool OutlineFromLinkOnceODRs) const {
  1741. llvm_unreachable("Target didn't implement "
  1742. "TargetInstrInfo::isFunctionSafeToOutlineFrom!");
  1743. }
  1744. /// Return true if the function should be outlined from by default.
  1745. virtual bool shouldOutlineFromFunctionByDefault(MachineFunction &MF) const {
  1746. return false;
  1747. }
  1748. /// Produce the expression describing the \p MI loading a value into
  1749. /// the physical register \p Reg. This hook should only be used with
  1750. /// \p MIs belonging to VReg-less functions.
  1751. virtual Optional<ParamLoadedValue> describeLoadedValue(const MachineInstr &MI,
  1752. Register Reg) const;
  1753. /// Given the generic extension instruction \p ExtMI, returns true if this
  1754. /// extension is a likely candidate for being folded into an another
  1755. /// instruction.
  1756. virtual bool isExtendLikelyToBeFolded(MachineInstr &ExtMI,
  1757. MachineRegisterInfo &MRI) const {
  1758. return false;
  1759. }
  1760. /// Return MIR formatter to format/parse MIR operands. Target can override
  1761. /// this virtual function and return target specific MIR formatter.
  1762. virtual const MIRFormatter *getMIRFormatter() const {
  1763. if (!Formatter.get())
  1764. Formatter = std::make_unique<MIRFormatter>();
  1765. return Formatter.get();
  1766. }
  1767. /// Returns the target-specific default value for tail duplication.
  1768. /// This value will be used if the tail-dup-placement-threshold argument is
  1769. /// not provided.
  1770. virtual unsigned getTailDuplicateSize(CodeGenOpt::Level OptLevel) const {
  1771. return OptLevel >= CodeGenOpt::Aggressive ? 4 : 2;
  1772. }
  1773. /// Returns the callee operand from the given \p MI.
  1774. virtual const MachineOperand &getCalleeOperand(const MachineInstr &MI) const {
  1775. return MI.getOperand(0);
  1776. }
  1777. private:
  1778. mutable std::unique_ptr<MIRFormatter> Formatter;
  1779. unsigned CallFrameSetupOpcode, CallFrameDestroyOpcode;
  1780. unsigned CatchRetOpcode;
  1781. unsigned ReturnOpcode;
  1782. };
  1783. /// Provide DenseMapInfo for TargetInstrInfo::RegSubRegPair.
  1784. template <> struct DenseMapInfo<TargetInstrInfo::RegSubRegPair> {
  1785. using RegInfo = DenseMapInfo<unsigned>;
  1786. static inline TargetInstrInfo::RegSubRegPair getEmptyKey() {
  1787. return TargetInstrInfo::RegSubRegPair(RegInfo::getEmptyKey(),
  1788. RegInfo::getEmptyKey());
  1789. }
  1790. static inline TargetInstrInfo::RegSubRegPair getTombstoneKey() {
  1791. return TargetInstrInfo::RegSubRegPair(RegInfo::getTombstoneKey(),
  1792. RegInfo::getTombstoneKey());
  1793. }
  1794. /// Reuse getHashValue implementation from
  1795. /// std::pair<unsigned, unsigned>.
  1796. static unsigned getHashValue(const TargetInstrInfo::RegSubRegPair &Val) {
  1797. std::pair<unsigned, unsigned> PairVal = std::make_pair(Val.Reg, Val.SubReg);
  1798. return DenseMapInfo<std::pair<unsigned, unsigned>>::getHashValue(PairVal);
  1799. }
  1800. static bool isEqual(const TargetInstrInfo::RegSubRegPair &LHS,
  1801. const TargetInstrInfo::RegSubRegPair &RHS) {
  1802. return RegInfo::isEqual(LHS.Reg, RHS.Reg) &&
  1803. RegInfo::isEqual(LHS.SubReg, RHS.SubReg);
  1804. }
  1805. };
  1806. } // end namespace llvm
  1807. #endif // LLVM_CODEGEN_TARGETINSTRINFO_H