TargetInstrInfo.h 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. //===-- llvm/Target/TargetInstrInfo.h - Instruction Info --------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file describes the target machine instruction set to the code generator.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_TARGET_TARGETINSTRINFO_H
  14. #define LLVM_TARGET_TARGETINSTRINFO_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/SmallSet.h"
  17. #include "llvm/CodeGen/MachineCombinerPattern.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/MC/MCInstrInfo.h"
  20. #include "llvm/Target/TargetRegisterInfo.h"
  21. namespace llvm {
  22. class InstrItineraryData;
  23. class LiveVariables;
  24. class MCAsmInfo;
  25. class MachineMemOperand;
  26. class MachineRegisterInfo;
  27. class MDNode;
  28. class MCInst;
  29. struct MCSchedModel;
  30. class MCSymbolRefExpr;
  31. class SDNode;
  32. class ScheduleHazardRecognizer;
  33. class SelectionDAG;
  34. class ScheduleDAG;
  35. class TargetRegisterClass;
  36. class TargetRegisterInfo;
  37. class BranchProbability;
  38. class TargetSubtargetInfo;
  39. class TargetSchedModel;
  40. class DFAPacketizer;
  41. template<class T> class SmallVectorImpl;
  42. //---------------------------------------------------------------------------
  43. ///
  44. /// TargetInstrInfo - Interface to description of machine instruction set
  45. ///
  46. class TargetInstrInfo : public MCInstrInfo {
  47. TargetInstrInfo(const TargetInstrInfo &) = delete;
  48. void operator=(const TargetInstrInfo &) = delete;
  49. public:
  50. TargetInstrInfo(unsigned CFSetupOpcode = ~0u, unsigned CFDestroyOpcode = ~0u)
  51. : CallFrameSetupOpcode(CFSetupOpcode),
  52. CallFrameDestroyOpcode(CFDestroyOpcode) {
  53. }
  54. virtual ~TargetInstrInfo();
  55. /// Given a machine instruction descriptor, returns the register
  56. /// class constraint for OpNum, or NULL.
  57. const TargetRegisterClass *getRegClass(const MCInstrDesc &TID,
  58. unsigned OpNum,
  59. const TargetRegisterInfo *TRI,
  60. const MachineFunction &MF) const;
  61. /// Return true if the instruction is trivially rematerializable, meaning it
  62. /// has no side effects and requires no operands that aren't always available.
  63. /// This means the only allowed uses are constants and unallocatable physical
  64. /// registers so that the instructions result is independent of the place
  65. /// in the function.
  66. bool isTriviallyReMaterializable(const MachineInstr *MI,
  67. AliasAnalysis *AA = nullptr) const {
  68. return MI->getOpcode() == TargetOpcode::IMPLICIT_DEF ||
  69. (MI->getDesc().isRematerializable() &&
  70. (isReallyTriviallyReMaterializable(MI, AA) ||
  71. isReallyTriviallyReMaterializableGeneric(MI, AA)));
  72. }
  73. protected:
  74. /// For instructions with opcodes for which the M_REMATERIALIZABLE flag is
  75. /// set, this hook lets the target specify whether the instruction is actually
  76. /// trivially rematerializable, taking into consideration its operands. This
  77. /// predicate must return false if the instruction has any side effects other
  78. /// than producing a value, or if it requres any address registers that are
  79. /// not always available.
  80. /// Requirements must be check as stated in isTriviallyReMaterializable() .
  81. virtual bool isReallyTriviallyReMaterializable(const MachineInstr *MI,
  82. AliasAnalysis *AA) const {
  83. return false;
  84. }
  85. private:
  86. /// For instructions with opcodes for which the M_REMATERIALIZABLE flag is
  87. /// set and the target hook isReallyTriviallyReMaterializable returns false,
  88. /// this function does target-independent tests to determine if the
  89. /// instruction is really trivially rematerializable.
  90. bool isReallyTriviallyReMaterializableGeneric(const MachineInstr *MI,
  91. AliasAnalysis *AA) const;
  92. public:
  93. /// These methods return the opcode of the frame setup/destroy instructions
  94. /// if they exist (-1 otherwise). Some targets use pseudo instructions in
  95. /// order to abstract away the difference between operating with a frame
  96. /// pointer and operating without, through the use of these two instructions.
  97. ///
  98. unsigned getCallFrameSetupOpcode() const { return CallFrameSetupOpcode; }
  99. unsigned getCallFrameDestroyOpcode() const { return CallFrameDestroyOpcode; }
  100. /// Returns the actual stack pointer adjustment made by an instruction
  101. /// as part of a call sequence. By default, only call frame setup/destroy
  102. /// instructions adjust the stack, but targets may want to override this
  103. /// to enable more fine-grained adjustment, or adjust by a different value.
  104. virtual int getSPAdjust(const MachineInstr *MI) const;
  105. /// Return true if the instruction is a "coalescable" extension instruction.
  106. /// That is, it's like a copy where it's legal for the source to overlap the
  107. /// destination. e.g. X86::MOVSX64rr32. If this returns true, then it's
  108. /// expected the pre-extension value is available as a subreg of the result
  109. /// register. This also returns the sub-register index in SubIdx.
  110. virtual bool isCoalescableExtInstr(const MachineInstr &MI,
  111. unsigned &SrcReg, unsigned &DstReg,
  112. unsigned &SubIdx) const {
  113. return false;
  114. }
  115. /// If the specified machine instruction is a direct
  116. /// load from a stack slot, return the virtual or physical register number of
  117. /// the destination along with the FrameIndex of the loaded stack slot. If
  118. /// not, return 0. This predicate must return 0 if the instruction has
  119. /// any side effects other than loading from the stack slot.
  120. virtual unsigned isLoadFromStackSlot(const MachineInstr *MI,
  121. int &FrameIndex) const {
  122. return 0;
  123. }
  124. /// Check for post-frame ptr elimination stack locations as well.
  125. /// This uses a heuristic so it isn't reliable for correctness.
  126. virtual unsigned isLoadFromStackSlotPostFE(const MachineInstr *MI,
  127. int &FrameIndex) const {
  128. return 0;
  129. }
  130. /// If the specified machine instruction has a load from a stack slot,
  131. /// return true along with the FrameIndex of the loaded stack slot and the
  132. /// machine mem operand containing the reference.
  133. /// If not, return false. Unlike isLoadFromStackSlot, this returns true for
  134. /// any instructions that loads from the stack. This is just a hint, as some
  135. /// cases may be missed.
  136. virtual bool hasLoadFromStackSlot(const MachineInstr *MI,
  137. const MachineMemOperand *&MMO,
  138. int &FrameIndex) const;
  139. /// If the specified machine instruction is a direct
  140. /// store to a stack slot, return the virtual or physical register number of
  141. /// the source reg along with the FrameIndex of the loaded stack slot. If
  142. /// not, return 0. This predicate must return 0 if the instruction has
  143. /// any side effects other than storing to the stack slot.
  144. virtual unsigned isStoreToStackSlot(const MachineInstr *MI,
  145. int &FrameIndex) const {
  146. return 0;
  147. }
  148. /// Check for post-frame ptr elimination stack locations as well.
  149. /// This uses a heuristic, so it isn't reliable for correctness.
  150. virtual unsigned isStoreToStackSlotPostFE(const MachineInstr *MI,
  151. int &FrameIndex) const {
  152. return 0;
  153. }
  154. /// If the specified machine instruction has a store to a stack slot,
  155. /// return true along with the FrameIndex of the loaded stack slot and the
  156. /// machine mem operand containing the reference.
  157. /// If not, return false. Unlike isStoreToStackSlot,
  158. /// this returns true for any instructions that stores to the
  159. /// stack. This is just a hint, as some cases may be missed.
  160. virtual bool hasStoreToStackSlot(const MachineInstr *MI,
  161. const MachineMemOperand *&MMO,
  162. int &FrameIndex) const;
  163. /// Return true if the specified machine instruction
  164. /// is a copy of one stack slot to another and has no other effect.
  165. /// Provide the identity of the two frame indices.
  166. virtual bool isStackSlotCopy(const MachineInstr *MI, int &DestFrameIndex,
  167. int &SrcFrameIndex) const {
  168. return false;
  169. }
  170. /// Compute the size in bytes and offset within a stack slot of a spilled
  171. /// register or subregister.
  172. ///
  173. /// \param [out] Size in bytes of the spilled value.
  174. /// \param [out] Offset in bytes within the stack slot.
  175. /// \returns true if both Size and Offset are successfully computed.
  176. ///
  177. /// Not all subregisters have computable spill slots. For example,
  178. /// subregisters registers may not be byte-sized, and a pair of discontiguous
  179. /// subregisters has no single offset.
  180. ///
  181. /// Targets with nontrivial bigendian implementations may need to override
  182. /// this, particularly to support spilled vector registers.
  183. virtual bool getStackSlotRange(const TargetRegisterClass *RC, unsigned SubIdx,
  184. unsigned &Size, unsigned &Offset,
  185. const MachineFunction &MF) const;
  186. /// Return true if the instruction is as cheap as a move instruction.
  187. ///
  188. /// Targets for different archs need to override this, and different
  189. /// micro-architectures can also be finely tuned inside.
  190. virtual bool isAsCheapAsAMove(const MachineInstr *MI) const {
  191. return MI->isAsCheapAsAMove();
  192. }
  193. /// Re-issue the specified 'original' instruction at the
  194. /// specific location targeting a new destination register.
  195. /// The register in Orig->getOperand(0).getReg() will be substituted by
  196. /// DestReg:SubIdx. Any existing subreg index is preserved or composed with
  197. /// SubIdx.
  198. virtual void reMaterialize(MachineBasicBlock &MBB,
  199. MachineBasicBlock::iterator MI,
  200. unsigned DestReg, unsigned SubIdx,
  201. const MachineInstr *Orig,
  202. const TargetRegisterInfo &TRI) const;
  203. /// Create a duplicate of the Orig instruction in MF. This is like
  204. /// MachineFunction::CloneMachineInstr(), but the target may update operands
  205. /// that are required to be unique.
  206. ///
  207. /// The instruction must be duplicable as indicated by isNotDuplicable().
  208. virtual MachineInstr *duplicate(MachineInstr *Orig,
  209. MachineFunction &MF) const;
  210. /// This method must be implemented by targets that
  211. /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
  212. /// may be able to convert a two-address instruction into one or more true
  213. /// three-address instructions on demand. This allows the X86 target (for
  214. /// example) to convert ADD and SHL instructions into LEA instructions if they
  215. /// would require register copies due to two-addressness.
  216. ///
  217. /// This method returns a null pointer if the transformation cannot be
  218. /// performed, otherwise it returns the last new instruction.
  219. ///
  220. virtual MachineInstr *
  221. convertToThreeAddress(MachineFunction::iterator &MFI,
  222. MachineBasicBlock::iterator &MBBI, LiveVariables *LV) const {
  223. return nullptr;
  224. }
  225. /// If a target has any instructions that are commutable but require
  226. /// converting to different instructions or making non-trivial changes to
  227. /// commute them, this method can overloaded to do that.
  228. /// The default implementation simply swaps the commutable operands.
  229. /// If NewMI is false, MI is modified in place and returned; otherwise, a
  230. /// new machine instruction is created and returned. Do not call this
  231. /// method for a non-commutable instruction, but there may be some cases
  232. /// where this method fails and returns null.
  233. virtual MachineInstr *commuteInstruction(MachineInstr *MI,
  234. bool NewMI = false) const;
  235. /// If specified MI is commutable, return the two operand indices that would
  236. /// swap value. Return false if the instruction
  237. /// is not in a form which this routine understands.
  238. virtual bool findCommutedOpIndices(MachineInstr *MI, unsigned &SrcOpIdx1,
  239. unsigned &SrcOpIdx2) const;
  240. /// A pair composed of a register and a sub-register index.
  241. /// Used to give some type checking when modeling Reg:SubReg.
  242. struct RegSubRegPair {
  243. unsigned Reg;
  244. unsigned SubReg;
  245. RegSubRegPair(unsigned Reg = 0, unsigned SubReg = 0)
  246. : Reg(Reg), SubReg(SubReg) {}
  247. };
  248. /// A pair composed of a pair of a register and a sub-register index,
  249. /// and another sub-register index.
  250. /// Used to give some type checking when modeling Reg:SubReg1, SubReg2.
  251. struct RegSubRegPairAndIdx : RegSubRegPair {
  252. unsigned SubIdx;
  253. RegSubRegPairAndIdx(unsigned Reg = 0, unsigned SubReg = 0,
  254. unsigned SubIdx = 0)
  255. : RegSubRegPair(Reg, SubReg), SubIdx(SubIdx) {}
  256. };
  257. /// Build the equivalent inputs of a REG_SEQUENCE for the given \p MI
  258. /// and \p DefIdx.
  259. /// \p [out] InputRegs of the equivalent REG_SEQUENCE. Each element of
  260. /// the list is modeled as <Reg:SubReg, SubIdx>.
  261. /// E.g., REG_SEQUENCE vreg1:sub1, sub0, vreg2, sub1 would produce
  262. /// two elements:
  263. /// - vreg1:sub1, sub0
  264. /// - vreg2<:0>, sub1
  265. ///
  266. /// \returns true if it is possible to build such an input sequence
  267. /// with the pair \p MI, \p DefIdx. False otherwise.
  268. ///
  269. /// \pre MI.isRegSequence() or MI.isRegSequenceLike().
  270. ///
  271. /// \note The generic implementation does not provide any support for
  272. /// MI.isRegSequenceLike(). In other words, one has to override
  273. /// getRegSequenceLikeInputs for target specific instructions.
  274. bool
  275. getRegSequenceInputs(const MachineInstr &MI, unsigned DefIdx,
  276. SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const;
  277. /// Build the equivalent inputs of a EXTRACT_SUBREG for the given \p MI
  278. /// and \p DefIdx.
  279. /// \p [out] InputReg of the equivalent EXTRACT_SUBREG.
  280. /// E.g., EXTRACT_SUBREG vreg1:sub1, sub0, sub1 would produce:
  281. /// - vreg1:sub1, sub0
  282. ///
  283. /// \returns true if it is possible to build such an input sequence
  284. /// with the pair \p MI, \p DefIdx. False otherwise.
  285. ///
  286. /// \pre MI.isExtractSubreg() or MI.isExtractSubregLike().
  287. ///
  288. /// \note The generic implementation does not provide any support for
  289. /// MI.isExtractSubregLike(). In other words, one has to override
  290. /// getExtractSubregLikeInputs for target specific instructions.
  291. bool
  292. getExtractSubregInputs(const MachineInstr &MI, unsigned DefIdx,
  293. RegSubRegPairAndIdx &InputReg) const;
  294. /// Build the equivalent inputs of a INSERT_SUBREG for the given \p MI
  295. /// and \p DefIdx.
  296. /// \p [out] BaseReg and \p [out] InsertedReg contain
  297. /// the equivalent inputs of INSERT_SUBREG.
  298. /// E.g., INSERT_SUBREG vreg0:sub0, vreg1:sub1, sub3 would produce:
  299. /// - BaseReg: vreg0:sub0
  300. /// - InsertedReg: vreg1:sub1, sub3
  301. ///
  302. /// \returns true if it is possible to build such an input sequence
  303. /// with the pair \p MI, \p DefIdx. False otherwise.
  304. ///
  305. /// \pre MI.isInsertSubreg() or MI.isInsertSubregLike().
  306. ///
  307. /// \note The generic implementation does not provide any support for
  308. /// MI.isInsertSubregLike(). In other words, one has to override
  309. /// getInsertSubregLikeInputs for target specific instructions.
  310. bool
  311. getInsertSubregInputs(const MachineInstr &MI, unsigned DefIdx,
  312. RegSubRegPair &BaseReg,
  313. RegSubRegPairAndIdx &InsertedReg) const;
  314. /// Return true if two machine instructions would produce identical values.
  315. /// By default, this is only true when the two instructions
  316. /// are deemed identical except for defs. If this function is called when the
  317. /// IR is still in SSA form, the caller can pass the MachineRegisterInfo for
  318. /// aggressive checks.
  319. virtual bool produceSameValue(const MachineInstr *MI0,
  320. const MachineInstr *MI1,
  321. const MachineRegisterInfo *MRI = nullptr) const;
  322. /// Analyze the branching code at the end of MBB, returning
  323. /// true if it cannot be understood (e.g. it's a switch dispatch or isn't
  324. /// implemented for a target). Upon success, this returns false and returns
  325. /// with the following information in various cases:
  326. ///
  327. /// 1. If this block ends with no branches (it just falls through to its succ)
  328. /// just return false, leaving TBB/FBB null.
  329. /// 2. If this block ends with only an unconditional branch, it sets TBB to be
  330. /// the destination block.
  331. /// 3. If this block ends with a conditional branch and it falls through to a
  332. /// successor block, it sets TBB to be the branch destination block and a
  333. /// list of operands that evaluate the condition. These operands can be
  334. /// passed to other TargetInstrInfo methods to create new branches.
  335. /// 4. If this block ends with a conditional branch followed by an
  336. /// unconditional branch, it returns the 'true' destination in TBB, the
  337. /// 'false' destination in FBB, and a list of operands that evaluate the
  338. /// condition. These operands can be passed to other TargetInstrInfo
  339. /// methods to create new branches.
  340. ///
  341. /// Note that RemoveBranch and InsertBranch must be implemented to support
  342. /// cases where this method returns success.
  343. ///
  344. /// If AllowModify is true, then this routine is allowed to modify the basic
  345. /// block (e.g. delete instructions after the unconditional branch).
  346. ///
  347. virtual bool AnalyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
  348. MachineBasicBlock *&FBB,
  349. SmallVectorImpl<MachineOperand> &Cond,
  350. bool AllowModify = false) const {
  351. return true;
  352. }
  353. /// Represents a predicate at the MachineFunction level. The control flow a
  354. /// MachineBranchPredicate represents is:
  355. ///
  356. /// Reg <def>= LHS `Predicate` RHS == ConditionDef
  357. /// if Reg then goto TrueDest else goto FalseDest
  358. ///
  359. struct MachineBranchPredicate {
  360. enum ComparePredicate {
  361. PRED_EQ, // True if two values are equal
  362. PRED_NE, // True if two values are not equal
  363. PRED_INVALID // Sentinel value
  364. };
  365. ComparePredicate Predicate;
  366. MachineOperand LHS;
  367. MachineOperand RHS;
  368. MachineBasicBlock *TrueDest;
  369. MachineBasicBlock *FalseDest;
  370. MachineInstr *ConditionDef;
  371. /// SingleUseCondition is true if ConditionDef is dead except for the
  372. /// branch(es) at the end of the basic block.
  373. ///
  374. bool SingleUseCondition;
  375. explicit MachineBranchPredicate()
  376. : Predicate(PRED_INVALID), LHS(MachineOperand::CreateImm(0)),
  377. RHS(MachineOperand::CreateImm(0)), TrueDest(nullptr),
  378. FalseDest(nullptr), ConditionDef(nullptr), SingleUseCondition(false) {
  379. }
  380. };
  381. /// Analyze the branching code at the end of MBB and parse it into the
  382. /// MachineBranchPredicate structure if possible. Returns false on success
  383. /// and true on failure.
  384. ///
  385. /// If AllowModify is true, then this routine is allowed to modify the basic
  386. /// block (e.g. delete instructions after the unconditional branch).
  387. ///
  388. virtual bool AnalyzeBranchPredicate(MachineBasicBlock &MBB,
  389. MachineBranchPredicate &MBP,
  390. bool AllowModify = false) const {
  391. return true;
  392. }
  393. /// Remove the branching code at the end of the specific MBB.
  394. /// This is only invoked in cases where AnalyzeBranch returns success. It
  395. /// returns the number of instructions that were removed.
  396. virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const {
  397. llvm_unreachable("Target didn't implement TargetInstrInfo::RemoveBranch!");
  398. }
  399. /// Insert branch code into the end of the specified MachineBasicBlock.
  400. /// The operands to this method are the same as those
  401. /// returned by AnalyzeBranch. This is only invoked in cases where
  402. /// AnalyzeBranch returns success. It returns the number of instructions
  403. /// inserted.
  404. ///
  405. /// It is also invoked by tail merging to add unconditional branches in
  406. /// cases where AnalyzeBranch doesn't apply because there was no original
  407. /// branch to analyze. At least this much must be implemented, else tail
  408. /// merging needs to be disabled.
  409. virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
  410. MachineBasicBlock *FBB,
  411. ArrayRef<MachineOperand> Cond,
  412. DebugLoc DL) const {
  413. llvm_unreachable("Target didn't implement TargetInstrInfo::InsertBranch!");
  414. }
  415. /// Delete the instruction OldInst and everything after it, replacing it with
  416. /// an unconditional branch to NewDest. This is used by the tail merging pass.
  417. virtual void ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
  418. MachineBasicBlock *NewDest) const;
  419. /// Get an instruction that performs an unconditional branch to the given
  420. /// symbol.
  421. virtual void
  422. getUnconditionalBranch(MCInst &MI,
  423. const MCSymbolRefExpr *BranchTarget) const {
  424. llvm_unreachable("Target didn't implement "
  425. "TargetInstrInfo::getUnconditionalBranch!");
  426. }
  427. /// Get a machine trap instruction.
  428. virtual void getTrap(MCInst &MI) const {
  429. llvm_unreachable("Target didn't implement TargetInstrInfo::getTrap!");
  430. }
  431. /// Get a number of bytes that suffices to hold
  432. /// either the instruction returned by getUnconditionalBranch or the
  433. /// instruction returned by getTrap. This only makes sense because
  434. /// getUnconditionalBranch returns a single, specific instruction. This
  435. /// information is needed by the jumptable construction code, since it must
  436. /// decide how many bytes to use for a jumptable entry so it can generate the
  437. /// right mask.
  438. ///
  439. /// Note that if the jumptable instruction requires alignment, then that
  440. /// alignment should be factored into this required bound so that the
  441. /// resulting bound gives the right alignment for the instruction.
  442. virtual unsigned getJumpInstrTableEntryBound() const {
  443. // This method gets called by LLVMTargetMachine always, so it can't fail
  444. // just because there happens to be no implementation for this target.
  445. // Any code that tries to use a jumptable annotation without defining
  446. // getUnconditionalBranch on the appropriate Target will fail anyway, and
  447. // the value returned here won't matter in that case.
  448. return 0;
  449. }
  450. /// Return true if it's legal to split the given basic
  451. /// block at the specified instruction (i.e. instruction would be the start
  452. /// of a new basic block).
  453. virtual bool isLegalToSplitMBBAt(MachineBasicBlock &MBB,
  454. MachineBasicBlock::iterator MBBI) const {
  455. return true;
  456. }
  457. /// Return true if it's profitable to predicate
  458. /// instructions with accumulated instruction latency of "NumCycles"
  459. /// of the specified basic block, where the probability of the instructions
  460. /// being executed is given by Probability, and Confidence is a measure
  461. /// of our confidence that it will be properly predicted.
  462. virtual
  463. bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
  464. unsigned ExtraPredCycles,
  465. const BranchProbability &Probability) const {
  466. return false;
  467. }
  468. /// Second variant of isProfitableToIfCvt. This one
  469. /// checks for the case where two basic blocks from true and false path
  470. /// of a if-then-else (diamond) are predicated on mutally exclusive
  471. /// predicates, where the probability of the true path being taken is given
  472. /// by Probability, and Confidence is a measure of our confidence that it
  473. /// will be properly predicted.
  474. virtual bool
  475. isProfitableToIfCvt(MachineBasicBlock &TMBB,
  476. unsigned NumTCycles, unsigned ExtraTCycles,
  477. MachineBasicBlock &FMBB,
  478. unsigned NumFCycles, unsigned ExtraFCycles,
  479. const BranchProbability &Probability) const {
  480. return false;
  481. }
  482. /// Return true if it's profitable for if-converter to duplicate instructions
  483. /// of specified accumulated instruction latencies in the specified MBB to
  484. /// enable if-conversion.
  485. /// The probability of the instructions being executed is given by
  486. /// Probability, and Confidence is a measure of our confidence that it
  487. /// will be properly predicted.
  488. virtual bool
  489. isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
  490. const BranchProbability &Probability) const {
  491. return false;
  492. }
  493. /// Return true if it's profitable to unpredicate
  494. /// one side of a 'diamond', i.e. two sides of if-else predicated on mutually
  495. /// exclusive predicates.
  496. /// e.g.
  497. /// subeq r0, r1, #1
  498. /// addne r0, r1, #1
  499. /// =>
  500. /// sub r0, r1, #1
  501. /// addne r0, r1, #1
  502. ///
  503. /// This may be profitable is conditional instructions are always executed.
  504. virtual bool isProfitableToUnpredicate(MachineBasicBlock &TMBB,
  505. MachineBasicBlock &FMBB) const {
  506. return false;
  507. }
  508. /// Return true if it is possible to insert a select
  509. /// instruction that chooses between TrueReg and FalseReg based on the
  510. /// condition code in Cond.
  511. ///
  512. /// When successful, also return the latency in cycles from TrueReg,
  513. /// FalseReg, and Cond to the destination register. In most cases, a select
  514. /// instruction will be 1 cycle, so CondCycles = TrueCycles = FalseCycles = 1
  515. ///
  516. /// Some x86 implementations have 2-cycle cmov instructions.
  517. ///
  518. /// @param MBB Block where select instruction would be inserted.
  519. /// @param Cond Condition returned by AnalyzeBranch.
  520. /// @param TrueReg Virtual register to select when Cond is true.
  521. /// @param FalseReg Virtual register to select when Cond is false.
  522. /// @param CondCycles Latency from Cond+Branch to select output.
  523. /// @param TrueCycles Latency from TrueReg to select output.
  524. /// @param FalseCycles Latency from FalseReg to select output.
  525. virtual bool canInsertSelect(const MachineBasicBlock &MBB,
  526. ArrayRef<MachineOperand> Cond,
  527. unsigned TrueReg, unsigned FalseReg,
  528. int &CondCycles,
  529. int &TrueCycles, int &FalseCycles) const {
  530. return false;
  531. }
  532. /// Insert a select instruction into MBB before I that will copy TrueReg to
  533. /// DstReg when Cond is true, and FalseReg to DstReg when Cond is false.
  534. ///
  535. /// This function can only be called after canInsertSelect() returned true.
  536. /// The condition in Cond comes from AnalyzeBranch, and it can be assumed
  537. /// that the same flags or registers required by Cond are available at the
  538. /// insertion point.
  539. ///
  540. /// @param MBB Block where select instruction should be inserted.
  541. /// @param I Insertion point.
  542. /// @param DL Source location for debugging.
  543. /// @param DstReg Virtual register to be defined by select instruction.
  544. /// @param Cond Condition as computed by AnalyzeBranch.
  545. /// @param TrueReg Virtual register to copy when Cond is true.
  546. /// @param FalseReg Virtual register to copy when Cons is false.
  547. virtual void insertSelect(MachineBasicBlock &MBB,
  548. MachineBasicBlock::iterator I, DebugLoc DL,
  549. unsigned DstReg, ArrayRef<MachineOperand> Cond,
  550. unsigned TrueReg, unsigned FalseReg) const {
  551. llvm_unreachable("Target didn't implement TargetInstrInfo::insertSelect!");
  552. }
  553. /// Analyze the given select instruction, returning true if
  554. /// it cannot be understood. It is assumed that MI->isSelect() is true.
  555. ///
  556. /// When successful, return the controlling condition and the operands that
  557. /// determine the true and false result values.
  558. ///
  559. /// Result = SELECT Cond, TrueOp, FalseOp
  560. ///
  561. /// Some targets can optimize select instructions, for example by predicating
  562. /// the instruction defining one of the operands. Such targets should set
  563. /// Optimizable.
  564. ///
  565. /// @param MI Select instruction to analyze.
  566. /// @param Cond Condition controlling the select.
  567. /// @param TrueOp Operand number of the value selected when Cond is true.
  568. /// @param FalseOp Operand number of the value selected when Cond is false.
  569. /// @param Optimizable Returned as true if MI is optimizable.
  570. /// @returns False on success.
  571. virtual bool analyzeSelect(const MachineInstr *MI,
  572. SmallVectorImpl<MachineOperand> &Cond,
  573. unsigned &TrueOp, unsigned &FalseOp,
  574. bool &Optimizable) const {
  575. assert(MI && MI->getDesc().isSelect() && "MI must be a select instruction");
  576. return true;
  577. }
  578. /// Given a select instruction that was understood by
  579. /// analyzeSelect and returned Optimizable = true, attempt to optimize MI by
  580. /// merging it with one of its operands. Returns NULL on failure.
  581. ///
  582. /// When successful, returns the new select instruction. The client is
  583. /// responsible for deleting MI.
  584. ///
  585. /// If both sides of the select can be optimized, PreferFalse is used to pick
  586. /// a side.
  587. ///
  588. /// @param MI Optimizable select instruction.
  589. /// @param NewMIs Set that record all MIs in the basic block up to \p
  590. /// MI. Has to be updated with any newly created MI or deleted ones.
  591. /// @param PreferFalse Try to optimize FalseOp instead of TrueOp.
  592. /// @returns Optimized instruction or NULL.
  593. virtual MachineInstr *optimizeSelect(MachineInstr *MI,
  594. SmallPtrSetImpl<MachineInstr *> &NewMIs,
  595. bool PreferFalse = false) const {
  596. // This function must be implemented if Optimizable is ever set.
  597. llvm_unreachable("Target must implement TargetInstrInfo::optimizeSelect!");
  598. }
  599. /// Emit instructions to copy a pair of physical registers.
  600. ///
  601. /// This function should support copies within any legal register class as
  602. /// well as any cross-class copies created during instruction selection.
  603. ///
  604. /// The source and destination registers may overlap, which may require a
  605. /// careful implementation when multiple copy instructions are required for
  606. /// large registers. See for example the ARM target.
  607. virtual void copyPhysReg(MachineBasicBlock &MBB,
  608. MachineBasicBlock::iterator MI, DebugLoc DL,
  609. unsigned DestReg, unsigned SrcReg,
  610. bool KillSrc) const {
  611. llvm_unreachable("Target didn't implement TargetInstrInfo::copyPhysReg!");
  612. }
  613. /// Store the specified register of the given register class to the specified
  614. /// stack frame index. The store instruction is to be added to the given
  615. /// machine basic block before the specified machine instruction. If isKill
  616. /// is true, the register operand is the last use and must be marked kill.
  617. virtual void storeRegToStackSlot(MachineBasicBlock &MBB,
  618. MachineBasicBlock::iterator MI,
  619. unsigned SrcReg, bool isKill, int FrameIndex,
  620. const TargetRegisterClass *RC,
  621. const TargetRegisterInfo *TRI) const {
  622. llvm_unreachable("Target didn't implement "
  623. "TargetInstrInfo::storeRegToStackSlot!");
  624. }
  625. /// Load the specified register of the given register class from the specified
  626. /// stack frame index. The load instruction is to be added to the given
  627. /// machine basic block before the specified machine instruction.
  628. virtual void loadRegFromStackSlot(MachineBasicBlock &MBB,
  629. MachineBasicBlock::iterator MI,
  630. unsigned DestReg, int FrameIndex,
  631. const TargetRegisterClass *RC,
  632. const TargetRegisterInfo *TRI) const {
  633. llvm_unreachable("Target didn't implement "
  634. "TargetInstrInfo::loadRegFromStackSlot!");
  635. }
  636. /// This function is called for all pseudo instructions
  637. /// that remain after register allocation. Many pseudo instructions are
  638. /// created to help register allocation. This is the place to convert them
  639. /// into real instructions. The target can edit MI in place, or it can insert
  640. /// new instructions and erase MI. The function should return true if
  641. /// anything was changed.
  642. virtual bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
  643. return false;
  644. }
  645. /// Attempt to fold a load or store of the specified stack
  646. /// slot into the specified machine instruction for the specified operand(s).
  647. /// If this is possible, a new instruction is returned with the specified
  648. /// operand folded, otherwise NULL is returned.
  649. /// The new instruction is inserted before MI, and the client is responsible
  650. /// for removing the old instruction.
  651. MachineInstr *foldMemoryOperand(MachineBasicBlock::iterator MI,
  652. ArrayRef<unsigned> Ops, int FrameIndex) const;
  653. /// Same as the previous version except it allows folding of any load and
  654. /// store from / to any address, not just from a specific stack slot.
  655. MachineInstr *foldMemoryOperand(MachineBasicBlock::iterator MI,
  656. ArrayRef<unsigned> Ops,
  657. MachineInstr *LoadMI) const;
  658. /// Return true when there is potentially a faster code sequence
  659. /// for an instruction chain ending in \p Root. All potential patterns are
  660. /// returned in the \p Pattern vector. Pattern should be sorted in priority
  661. /// order since the pattern evaluator stops checking as soon as it finds a
  662. /// faster sequence.
  663. /// \param Root - Instruction that could be combined with one of its operands
  664. /// \param Pattern - Vector of possible combination patterns
  665. virtual bool getMachineCombinerPatterns(
  666. MachineInstr &Root,
  667. SmallVectorImpl<MachineCombinerPattern::MC_PATTERN> &Pattern) const {
  668. return false;
  669. }
  670. /// When getMachineCombinerPatterns() finds patterns, this function generates
  671. /// the instructions that could replace the original code sequence. The client
  672. /// has to decide whether the actual replacement is beneficial or not.
  673. /// \param Root - Instruction that could be combined with one of its operands
  674. /// \param Pattern - Combination pattern for Root
  675. /// \param InsInstrs - Vector of new instructions that implement P
  676. /// \param DelInstrs - Old instructions, including Root, that could be
  677. /// replaced by InsInstr
  678. /// \param InstrIdxForVirtReg - map of virtual register to instruction in
  679. /// InsInstr that defines it
  680. virtual void genAlternativeCodeSequence(
  681. MachineInstr &Root, MachineCombinerPattern::MC_PATTERN Pattern,
  682. SmallVectorImpl<MachineInstr *> &InsInstrs,
  683. SmallVectorImpl<MachineInstr *> &DelInstrs,
  684. DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
  685. return;
  686. }
  687. /// Return true when a target supports MachineCombiner.
  688. virtual bool useMachineCombiner() const { return false; }
  689. protected:
  690. /// Target-dependent implementation for foldMemoryOperand.
  691. /// Target-independent code in foldMemoryOperand will
  692. /// take care of adding a MachineMemOperand to the newly created instruction.
  693. /// The instruction and any auxiliary instructions necessary will be inserted
  694. /// at InsertPt.
  695. virtual MachineInstr *foldMemoryOperandImpl(
  696. MachineFunction &MF, MachineInstr *MI, ArrayRef<unsigned> Ops,
  697. MachineBasicBlock::iterator InsertPt, int FrameIndex) const {
  698. return nullptr;
  699. }
  700. /// Target-dependent implementation for foldMemoryOperand.
  701. /// Target-independent code in foldMemoryOperand will
  702. /// take care of adding a MachineMemOperand to the newly created instruction.
  703. /// The instruction and any auxiliary instructions necessary will be inserted
  704. /// at InsertPt.
  705. virtual MachineInstr *foldMemoryOperandImpl(
  706. MachineFunction &MF, MachineInstr *MI, ArrayRef<unsigned> Ops,
  707. MachineBasicBlock::iterator InsertPt, MachineInstr *LoadMI) const {
  708. return nullptr;
  709. }
  710. /// \brief Target-dependent implementation of getRegSequenceInputs.
  711. ///
  712. /// \returns true if it is possible to build the equivalent
  713. /// REG_SEQUENCE inputs with the pair \p MI, \p DefIdx. False otherwise.
  714. ///
  715. /// \pre MI.isRegSequenceLike().
  716. ///
  717. /// \see TargetInstrInfo::getRegSequenceInputs.
  718. virtual bool getRegSequenceLikeInputs(
  719. const MachineInstr &MI, unsigned DefIdx,
  720. SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
  721. return false;
  722. }
  723. /// \brief Target-dependent implementation of getExtractSubregInputs.
  724. ///
  725. /// \returns true if it is possible to build the equivalent
  726. /// EXTRACT_SUBREG inputs with the pair \p MI, \p DefIdx. False otherwise.
  727. ///
  728. /// \pre MI.isExtractSubregLike().
  729. ///
  730. /// \see TargetInstrInfo::getExtractSubregInputs.
  731. virtual bool getExtractSubregLikeInputs(
  732. const MachineInstr &MI, unsigned DefIdx,
  733. RegSubRegPairAndIdx &InputReg) const {
  734. return false;
  735. }
  736. /// \brief Target-dependent implementation of getInsertSubregInputs.
  737. ///
  738. /// \returns true if it is possible to build the equivalent
  739. /// INSERT_SUBREG inputs with the pair \p MI, \p DefIdx. False otherwise.
  740. ///
  741. /// \pre MI.isInsertSubregLike().
  742. ///
  743. /// \see TargetInstrInfo::getInsertSubregInputs.
  744. virtual bool
  745. getInsertSubregLikeInputs(const MachineInstr &MI, unsigned DefIdx,
  746. RegSubRegPair &BaseReg,
  747. RegSubRegPairAndIdx &InsertedReg) const {
  748. return false;
  749. }
  750. public:
  751. /// Returns true for the specified load / store if folding is possible.
  752. virtual bool canFoldMemoryOperand(const MachineInstr *MI,
  753. ArrayRef<unsigned> Ops) const;
  754. /// unfoldMemoryOperand - Separate a single instruction which folded a load or
  755. /// a store or a load and a store into two or more instruction. If this is
  756. /// possible, returns true as well as the new instructions by reference.
  757. virtual bool unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
  758. unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
  759. SmallVectorImpl<MachineInstr*> &NewMIs) const{
  760. return false;
  761. }
  762. virtual bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
  763. SmallVectorImpl<SDNode*> &NewNodes) const {
  764. return false;
  765. }
  766. /// Returns the opcode of the would be new
  767. /// instruction after load / store are unfolded from an instruction of the
  768. /// specified opcode. It returns zero if the specified unfolding is not
  769. /// possible. If LoadRegIndex is non-null, it is filled in with the operand
  770. /// index of the operand which will hold the register holding the loaded
  771. /// value.
  772. virtual unsigned getOpcodeAfterMemoryUnfold(unsigned Opc,
  773. bool UnfoldLoad, bool UnfoldStore,
  774. unsigned *LoadRegIndex = nullptr) const {
  775. return 0;
  776. }
  777. /// This is used by the pre-regalloc scheduler to determine if two loads are
  778. /// loading from the same base address. It should only return true if the base
  779. /// pointers are the same and the only differences between the two addresses
  780. /// are the offset. It also returns the offsets by reference.
  781. virtual bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
  782. int64_t &Offset1, int64_t &Offset2) const {
  783. return false;
  784. }
  785. /// This is a used by the pre-regalloc scheduler to determine (in conjunction
  786. /// with areLoadsFromSameBasePtr) if two loads should be scheduled together.
  787. /// On some targets if two loads are loading from
  788. /// addresses in the same cache line, it's better if they are scheduled
  789. /// together. This function takes two integers that represent the load offsets
  790. /// from the common base address. It returns true if it decides it's desirable
  791. /// to schedule the two loads together. "NumLoads" is the number of loads that
  792. /// have already been scheduled after Load1.
  793. virtual bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
  794. int64_t Offset1, int64_t Offset2,
  795. unsigned NumLoads) const {
  796. return false;
  797. }
  798. /// Get the base register and byte offset of an instruction that reads/writes
  799. /// memory.
  800. virtual bool getMemOpBaseRegImmOfs(MachineInstr *MemOp, unsigned &BaseReg,
  801. unsigned &Offset,
  802. const TargetRegisterInfo *TRI) const {
  803. return false;
  804. }
  805. virtual bool enableClusterLoads() const { return false; }
  806. virtual bool shouldClusterLoads(MachineInstr *FirstLdSt,
  807. MachineInstr *SecondLdSt,
  808. unsigned NumLoads) const {
  809. return false;
  810. }
  811. /// Can this target fuse the given instructions if they are scheduled
  812. /// adjacent.
  813. virtual bool shouldScheduleAdjacent(MachineInstr* First,
  814. MachineInstr *Second) const {
  815. return false;
  816. }
  817. /// Reverses the branch condition of the specified condition list,
  818. /// returning false on success and true if it cannot be reversed.
  819. virtual
  820. bool ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
  821. return true;
  822. }
  823. /// Insert a noop into the instruction stream at the specified point.
  824. virtual void insertNoop(MachineBasicBlock &MBB,
  825. MachineBasicBlock::iterator MI) const;
  826. /// Return the noop instruction to use for a noop.
  827. virtual void getNoopForMachoTarget(MCInst &NopInst) const;
  828. /// Returns true if the instruction is already predicated.
  829. virtual bool isPredicated(const MachineInstr *MI) const {
  830. return false;
  831. }
  832. /// Returns true if the instruction is a
  833. /// terminator instruction that has not been predicated.
  834. virtual bool isUnpredicatedTerminator(const MachineInstr *MI) const;
  835. /// Convert the instruction into a predicated instruction.
  836. /// It returns true if the operation was successful.
  837. virtual
  838. bool PredicateInstruction(MachineInstr *MI,
  839. ArrayRef<MachineOperand> Pred) const;
  840. /// Returns true if the first specified predicate
  841. /// subsumes the second, e.g. GE subsumes GT.
  842. virtual
  843. bool SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
  844. ArrayRef<MachineOperand> Pred2) const {
  845. return false;
  846. }
  847. /// If the specified instruction defines any predicate
  848. /// or condition code register(s) used for predication, returns true as well
  849. /// as the definition predicate(s) by reference.
  850. virtual bool DefinesPredicate(MachineInstr *MI,
  851. std::vector<MachineOperand> &Pred) const {
  852. return false;
  853. }
  854. /// Return true if the specified instruction can be predicated.
  855. /// By default, this returns true for every instruction with a
  856. /// PredicateOperand.
  857. virtual bool isPredicable(MachineInstr *MI) const {
  858. return MI->getDesc().isPredicable();
  859. }
  860. /// Return true if it's safe to move a machine
  861. /// instruction that defines the specified register class.
  862. virtual bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
  863. return true;
  864. }
  865. /// Test if the given instruction should be considered a scheduling boundary.
  866. /// This primarily includes labels and terminators.
  867. virtual bool isSchedulingBoundary(const MachineInstr *MI,
  868. const MachineBasicBlock *MBB,
  869. const MachineFunction &MF) const;
  870. /// Measure the specified inline asm to determine an approximation of its
  871. /// length.
  872. virtual unsigned getInlineAsmLength(const char *Str,
  873. const MCAsmInfo &MAI) const;
  874. /// Allocate and return a hazard recognizer to use for this target when
  875. /// scheduling the machine instructions before register allocation.
  876. virtual ScheduleHazardRecognizer*
  877. CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
  878. const ScheduleDAG *DAG) const;
  879. /// Allocate and return a hazard recognizer to use for this target when
  880. /// scheduling the machine instructions before register allocation.
  881. virtual ScheduleHazardRecognizer*
  882. CreateTargetMIHazardRecognizer(const InstrItineraryData*,
  883. const ScheduleDAG *DAG) const;
  884. /// Allocate and return a hazard recognizer to use for this target when
  885. /// scheduling the machine instructions after register allocation.
  886. virtual ScheduleHazardRecognizer*
  887. CreateTargetPostRAHazardRecognizer(const InstrItineraryData*,
  888. const ScheduleDAG *DAG) const;
  889. /// Provide a global flag for disabling the PreRA hazard recognizer that
  890. /// targets may choose to honor.
  891. bool usePreRAHazardRecognizer() const;
  892. /// For a comparison instruction, return the source registers
  893. /// in SrcReg and SrcReg2 if having two register operands, and the value it
  894. /// compares against in CmpValue. Return true if the comparison instruction
  895. /// can be analyzed.
  896. virtual bool analyzeCompare(const MachineInstr *MI,
  897. unsigned &SrcReg, unsigned &SrcReg2,
  898. int &Mask, int &Value) const {
  899. return false;
  900. }
  901. /// See if the comparison instruction can be converted
  902. /// into something more efficient. E.g., on ARM most instructions can set the
  903. /// flags register, obviating the need for a separate CMP.
  904. virtual bool optimizeCompareInstr(MachineInstr *CmpInstr,
  905. unsigned SrcReg, unsigned SrcReg2,
  906. int Mask, int Value,
  907. const MachineRegisterInfo *MRI) const {
  908. return false;
  909. }
  910. virtual bool optimizeCondBranch(MachineInstr *MI) const { return false; }
  911. /// Try to remove the load by folding it to a register operand at the use.
  912. /// We fold the load instructions if and only if the
  913. /// def and use are in the same BB. We only look at one load and see
  914. /// whether it can be folded into MI. FoldAsLoadDefReg is the virtual register
  915. /// defined by the load we are trying to fold. DefMI returns the machine
  916. /// instruction that defines FoldAsLoadDefReg, and the function returns
  917. /// the machine instruction generated due to folding.
  918. virtual MachineInstr* optimizeLoadInstr(MachineInstr *MI,
  919. const MachineRegisterInfo *MRI,
  920. unsigned &FoldAsLoadDefReg,
  921. MachineInstr *&DefMI) const {
  922. return nullptr;
  923. }
  924. /// 'Reg' is known to be defined by a move immediate instruction,
  925. /// try to fold the immediate into the use instruction.
  926. /// If MRI->hasOneNonDBGUse(Reg) is true, and this function returns true,
  927. /// then the caller may assume that DefMI has been erased from its parent
  928. /// block. The caller may assume that it will not be erased by this
  929. /// function otherwise.
  930. virtual bool FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
  931. unsigned Reg, MachineRegisterInfo *MRI) const {
  932. return false;
  933. }
  934. /// Return the number of u-operations the given machine
  935. /// instruction will be decoded to on the target cpu. The itinerary's
  936. /// IssueWidth is the number of microops that can be dispatched each
  937. /// cycle. An instruction with zero microops takes no dispatch resources.
  938. virtual unsigned getNumMicroOps(const InstrItineraryData *ItinData,
  939. const MachineInstr *MI) const;
  940. /// Return true for pseudo instructions that don't consume any
  941. /// machine resources in their current form. These are common cases that the
  942. /// scheduler should consider free, rather than conservatively handling them
  943. /// as instructions with no itinerary.
  944. bool isZeroCost(unsigned Opcode) const {
  945. return Opcode <= TargetOpcode::COPY;
  946. }
  947. virtual int getOperandLatency(const InstrItineraryData *ItinData,
  948. SDNode *DefNode, unsigned DefIdx,
  949. SDNode *UseNode, unsigned UseIdx) const;
  950. /// Compute and return the use operand latency of a given pair of def and use.
  951. /// In most cases, the static scheduling itinerary was enough to determine the
  952. /// operand latency. But it may not be possible for instructions with variable
  953. /// number of defs / uses.
  954. ///
  955. /// This is a raw interface to the itinerary that may be directly overridden
  956. /// by a target. Use computeOperandLatency to get the best estimate of
  957. /// latency.
  958. virtual int getOperandLatency(const InstrItineraryData *ItinData,
  959. const MachineInstr *DefMI, unsigned DefIdx,
  960. const MachineInstr *UseMI,
  961. unsigned UseIdx) const;
  962. /// Compute and return the latency of the given data
  963. /// dependent def and use when the operand indices are already known.
  964. unsigned computeOperandLatency(const InstrItineraryData *ItinData,
  965. const MachineInstr *DefMI, unsigned DefIdx,
  966. const MachineInstr *UseMI, unsigned UseIdx)
  967. const;
  968. /// Compute the instruction latency of a given instruction.
  969. /// If the instruction has higher cost when predicated, it's returned via
  970. /// PredCost.
  971. virtual unsigned getInstrLatency(const InstrItineraryData *ItinData,
  972. const MachineInstr *MI,
  973. unsigned *PredCost = nullptr) const;
  974. virtual unsigned getPredicationCost(const MachineInstr *MI) const;
  975. virtual int getInstrLatency(const InstrItineraryData *ItinData,
  976. SDNode *Node) const;
  977. /// Return the default expected latency for a def based on it's opcode.
  978. unsigned defaultDefLatency(const MCSchedModel &SchedModel,
  979. const MachineInstr *DefMI) const;
  980. int computeDefOperandLatency(const InstrItineraryData *ItinData,
  981. const MachineInstr *DefMI) const;
  982. /// Return true if this opcode has high latency to its result.
  983. virtual bool isHighLatencyDef(int opc) const { return false; }
  984. /// Compute operand latency between a def of 'Reg'
  985. /// and a use in the current loop. Return true if the target considered
  986. /// it 'high'. This is used by optimization passes such as machine LICM to
  987. /// determine whether it makes sense to hoist an instruction out even in a
  988. /// high register pressure situation.
  989. virtual
  990. bool hasHighOperandLatency(const TargetSchedModel &SchedModel,
  991. const MachineRegisterInfo *MRI,
  992. const MachineInstr *DefMI, unsigned DefIdx,
  993. const MachineInstr *UseMI, unsigned UseIdx) const {
  994. return false;
  995. }
  996. /// Compute operand latency of a def of 'Reg'. Return true
  997. /// if the target considered it 'low'.
  998. virtual
  999. bool hasLowDefLatency(const TargetSchedModel &SchedModel,
  1000. const MachineInstr *DefMI, unsigned DefIdx) const;
  1001. /// Perform target-specific instruction verification.
  1002. virtual
  1003. bool verifyInstruction(const MachineInstr *MI, StringRef &ErrInfo) const {
  1004. return true;
  1005. }
  1006. /// Return the current execution domain and bit mask of
  1007. /// possible domains for instruction.
  1008. ///
  1009. /// Some micro-architectures have multiple execution domains, and multiple
  1010. /// opcodes that perform the same operation in different domains. For
  1011. /// example, the x86 architecture provides the por, orps, and orpd
  1012. /// instructions that all do the same thing. There is a latency penalty if a
  1013. /// register is written in one domain and read in another.
  1014. ///
  1015. /// This function returns a pair (domain, mask) containing the execution
  1016. /// domain of MI, and a bit mask of possible domains. The setExecutionDomain
  1017. /// function can be used to change the opcode to one of the domains in the
  1018. /// bit mask. Instructions whose execution domain can't be changed should
  1019. /// return a 0 mask.
  1020. ///
  1021. /// The execution domain numbers don't have any special meaning except domain
  1022. /// 0 is used for instructions that are not associated with any interesting
  1023. /// execution domain.
  1024. ///
  1025. virtual std::pair<uint16_t, uint16_t>
  1026. getExecutionDomain(const MachineInstr *MI) const {
  1027. return std::make_pair(0, 0);
  1028. }
  1029. /// Change the opcode of MI to execute in Domain.
  1030. ///
  1031. /// The bit (1 << Domain) must be set in the mask returned from
  1032. /// getExecutionDomain(MI).
  1033. virtual void setExecutionDomain(MachineInstr *MI, unsigned Domain) const {}
  1034. /// Returns the preferred minimum clearance
  1035. /// before an instruction with an unwanted partial register update.
  1036. ///
  1037. /// Some instructions only write part of a register, and implicitly need to
  1038. /// read the other parts of the register. This may cause unwanted stalls
  1039. /// preventing otherwise unrelated instructions from executing in parallel in
  1040. /// an out-of-order CPU.
  1041. ///
  1042. /// For example, the x86 instruction cvtsi2ss writes its result to bits
  1043. /// [31:0] of the destination xmm register. Bits [127:32] are unaffected, so
  1044. /// the instruction needs to wait for the old value of the register to become
  1045. /// available:
  1046. ///
  1047. /// addps %xmm1, %xmm0
  1048. /// movaps %xmm0, (%rax)
  1049. /// cvtsi2ss %rbx, %xmm0
  1050. ///
  1051. /// In the code above, the cvtsi2ss instruction needs to wait for the addps
  1052. /// instruction before it can issue, even though the high bits of %xmm0
  1053. /// probably aren't needed.
  1054. ///
  1055. /// This hook returns the preferred clearance before MI, measured in
  1056. /// instructions. Other defs of MI's operand OpNum are avoided in the last N
  1057. /// instructions before MI. It should only return a positive value for
  1058. /// unwanted dependencies. If the old bits of the defined register have
  1059. /// useful values, or if MI is determined to otherwise read the dependency,
  1060. /// the hook should return 0.
  1061. ///
  1062. /// The unwanted dependency may be handled by:
  1063. ///
  1064. /// 1. Allocating the same register for an MI def and use. That makes the
  1065. /// unwanted dependency identical to a required dependency.
  1066. ///
  1067. /// 2. Allocating a register for the def that has no defs in the previous N
  1068. /// instructions.
  1069. ///
  1070. /// 3. Calling breakPartialRegDependency() with the same arguments. This
  1071. /// allows the target to insert a dependency breaking instruction.
  1072. ///
  1073. virtual unsigned
  1074. getPartialRegUpdateClearance(const MachineInstr *MI, unsigned OpNum,
  1075. const TargetRegisterInfo *TRI) const {
  1076. // The default implementation returns 0 for no partial register dependency.
  1077. return 0;
  1078. }
  1079. /// \brief Return the minimum clearance before an instruction that reads an
  1080. /// unused register.
  1081. ///
  1082. /// For example, AVX instructions may copy part of a register operand into
  1083. /// the unused high bits of the destination register.
  1084. ///
  1085. /// vcvtsi2sdq %rax, %xmm0<undef>, %xmm14
  1086. ///
  1087. /// In the code above, vcvtsi2sdq copies %xmm0[127:64] into %xmm14 creating a
  1088. /// false dependence on any previous write to %xmm0.
  1089. ///
  1090. /// This hook works similarly to getPartialRegUpdateClearance, except that it
  1091. /// does not take an operand index. Instead sets \p OpNum to the index of the
  1092. /// unused register.
  1093. virtual unsigned getUndefRegClearance(const MachineInstr *MI, unsigned &OpNum,
  1094. const TargetRegisterInfo *TRI) const {
  1095. // The default implementation returns 0 for no undef register dependency.
  1096. return 0;
  1097. }
  1098. /// Insert a dependency-breaking instruction
  1099. /// before MI to eliminate an unwanted dependency on OpNum.
  1100. ///
  1101. /// If it wasn't possible to avoid a def in the last N instructions before MI
  1102. /// (see getPartialRegUpdateClearance), this hook will be called to break the
  1103. /// unwanted dependency.
  1104. ///
  1105. /// On x86, an xorps instruction can be used as a dependency breaker:
  1106. ///
  1107. /// addps %xmm1, %xmm0
  1108. /// movaps %xmm0, (%rax)
  1109. /// xorps %xmm0, %xmm0
  1110. /// cvtsi2ss %rbx, %xmm0
  1111. ///
  1112. /// An <imp-kill> operand should be added to MI if an instruction was
  1113. /// inserted. This ties the instructions together in the post-ra scheduler.
  1114. ///
  1115. virtual void
  1116. breakPartialRegDependency(MachineBasicBlock::iterator MI, unsigned OpNum,
  1117. const TargetRegisterInfo *TRI) const {}
  1118. /// Create machine specific model for scheduling.
  1119. virtual DFAPacketizer *
  1120. CreateTargetScheduleState(const TargetSubtargetInfo &) const {
  1121. return nullptr;
  1122. }
  1123. // Sometimes, it is possible for the target
  1124. // to tell, even without aliasing information, that two MIs access different
  1125. // memory addresses. This function returns true if two MIs access different
  1126. // memory addresses and false otherwise.
  1127. virtual bool
  1128. areMemAccessesTriviallyDisjoint(MachineInstr *MIa, MachineInstr *MIb,
  1129. AliasAnalysis *AA = nullptr) const {
  1130. assert(MIa && (MIa->mayLoad() || MIa->mayStore()) &&
  1131. "MIa must load from or modify a memory location");
  1132. assert(MIb && (MIb->mayLoad() || MIb->mayStore()) &&
  1133. "MIb must load from or modify a memory location");
  1134. return false;
  1135. }
  1136. /// \brief Return the value to use for the MachineCSE's LookAheadLimit,
  1137. /// which is a heuristic used for CSE'ing phys reg defs.
  1138. virtual unsigned getMachineCSELookAheadLimit () const {
  1139. // The default lookahead is small to prevent unprofitable quadratic
  1140. // behavior.
  1141. return 5;
  1142. }
  1143. private:
  1144. unsigned CallFrameSetupOpcode, CallFrameDestroyOpcode;
  1145. };
  1146. } // End llvm namespace
  1147. #endif