X86InstrInfo.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. //===-- X86InstrInfo.h - X86 Instruction Information ------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains the X86 implementation of the TargetInstrInfo class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_TARGET_X86_X86INSTRINFO_H
  13. #define LLVM_LIB_TARGET_X86_X86INSTRINFO_H
  14. #include "MCTargetDesc/X86BaseInfo.h"
  15. #include "X86InstrFMA3Info.h"
  16. #include "X86RegisterInfo.h"
  17. #include "llvm/CodeGen/ISDOpcodes.h"
  18. #include "llvm/CodeGen/TargetInstrInfo.h"
  19. #include <vector>
  20. #define GET_INSTRINFO_HEADER
  21. #include "X86GenInstrInfo.inc"
  22. namespace llvm {
  23. class X86Subtarget;
  24. namespace X86 {
  25. enum AsmComments {
  26. // For instr that was compressed from EVEX to VEX.
  27. AC_EVEX_2_VEX = MachineInstr::TAsmComments
  28. };
  29. /// Return a pair of condition code for the given predicate and whether
  30. /// the instruction operands should be swaped to match the condition code.
  31. std::pair<CondCode, bool> getX86ConditionCode(CmpInst::Predicate Predicate);
  32. /// Return a cmov opcode for the given register size in bytes, and operand type.
  33. unsigned getCMovOpcode(unsigned RegBytes, bool HasMemoryOperand = false);
  34. /// Return the source operand # for condition code by \p MCID. If the
  35. /// instruction doesn't have a condition code, return -1.
  36. int getCondSrcNoFromDesc(const MCInstrDesc &MCID);
  37. /// Return the condition code of the instruction. If the instruction doesn't
  38. /// have a condition code, return X86::COND_INVALID.
  39. CondCode getCondFromMI(const MachineInstr &MI);
  40. // Turn JCC instruction into condition code.
  41. CondCode getCondFromBranch(const MachineInstr &MI);
  42. // Turn SETCC instruction into condition code.
  43. CondCode getCondFromSETCC(const MachineInstr &MI);
  44. // Turn CMOV instruction into condition code.
  45. CondCode getCondFromCMov(const MachineInstr &MI);
  46. /// GetOppositeBranchCondition - Return the inverse of the specified cond,
  47. /// e.g. turning COND_E to COND_NE.
  48. CondCode GetOppositeBranchCondition(CondCode CC);
  49. /// Get the VPCMP immediate for the given condition.
  50. unsigned getVPCMPImmForCond(ISD::CondCode CC);
  51. /// Get the VPCMP immediate if the opcodes are swapped.
  52. unsigned getSwappedVPCMPImm(unsigned Imm);
  53. /// Get the VPCOM immediate if the opcodes are swapped.
  54. unsigned getSwappedVPCOMImm(unsigned Imm);
  55. /// Get the VCMP immediate if the opcodes are swapped.
  56. unsigned getSwappedVCMPImm(unsigned Imm);
  57. /// Check if the instruction is X87 instruction.
  58. bool isX87Instruction(MachineInstr &MI);
  59. } // namespace X86
  60. /// isGlobalStubReference - Return true if the specified TargetFlag operand is
  61. /// a reference to a stub for a global, not the global itself.
  62. inline static bool isGlobalStubReference(unsigned char TargetFlag) {
  63. switch (TargetFlag) {
  64. case X86II::MO_DLLIMPORT: // dllimport stub.
  65. case X86II::MO_GOTPCREL: // rip-relative GOT reference.
  66. case X86II::MO_GOTPCREL_NORELAX: // rip-relative GOT reference.
  67. case X86II::MO_GOT: // normal GOT reference.
  68. case X86II::MO_DARWIN_NONLAZY_PIC_BASE: // Normal $non_lazy_ptr ref.
  69. case X86II::MO_DARWIN_NONLAZY: // Normal $non_lazy_ptr ref.
  70. case X86II::MO_COFFSTUB: // COFF .refptr stub.
  71. return true;
  72. default:
  73. return false;
  74. }
  75. }
  76. /// isGlobalRelativeToPICBase - Return true if the specified global value
  77. /// reference is relative to a 32-bit PIC base (X86ISD::GlobalBaseReg). If this
  78. /// is true, the addressing mode has the PIC base register added in (e.g. EBX).
  79. inline static bool isGlobalRelativeToPICBase(unsigned char TargetFlag) {
  80. switch (TargetFlag) {
  81. case X86II::MO_GOTOFF: // isPICStyleGOT: local global.
  82. case X86II::MO_GOT: // isPICStyleGOT: other global.
  83. case X86II::MO_PIC_BASE_OFFSET: // Darwin local global.
  84. case X86II::MO_DARWIN_NONLAZY_PIC_BASE: // Darwin/32 external global.
  85. case X86II::MO_TLVP: // ??? Pretty sure..
  86. return true;
  87. default:
  88. return false;
  89. }
  90. }
  91. inline static bool isScale(const MachineOperand &MO) {
  92. return MO.isImm() && (MO.getImm() == 1 || MO.getImm() == 2 ||
  93. MO.getImm() == 4 || MO.getImm() == 8);
  94. }
  95. inline static bool isLeaMem(const MachineInstr &MI, unsigned Op) {
  96. if (MI.getOperand(Op).isFI())
  97. return true;
  98. return Op + X86::AddrSegmentReg <= MI.getNumOperands() &&
  99. MI.getOperand(Op + X86::AddrBaseReg).isReg() &&
  100. isScale(MI.getOperand(Op + X86::AddrScaleAmt)) &&
  101. MI.getOperand(Op + X86::AddrIndexReg).isReg() &&
  102. (MI.getOperand(Op + X86::AddrDisp).isImm() ||
  103. MI.getOperand(Op + X86::AddrDisp).isGlobal() ||
  104. MI.getOperand(Op + X86::AddrDisp).isCPI() ||
  105. MI.getOperand(Op + X86::AddrDisp).isJTI());
  106. }
  107. inline static bool isMem(const MachineInstr &MI, unsigned Op) {
  108. if (MI.getOperand(Op).isFI())
  109. return true;
  110. return Op + X86::AddrNumOperands <= MI.getNumOperands() &&
  111. MI.getOperand(Op + X86::AddrSegmentReg).isReg() && isLeaMem(MI, Op);
  112. }
  113. class X86InstrInfo final : public X86GenInstrInfo {
  114. X86Subtarget &Subtarget;
  115. const X86RegisterInfo RI;
  116. virtual void anchor();
  117. bool AnalyzeBranchImpl(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
  118. MachineBasicBlock *&FBB,
  119. SmallVectorImpl<MachineOperand> &Cond,
  120. SmallVectorImpl<MachineInstr *> &CondBranches,
  121. bool AllowModify) const;
  122. public:
  123. explicit X86InstrInfo(X86Subtarget &STI);
  124. /// getRegisterInfo - TargetInstrInfo is a superset of MRegister info. As
  125. /// such, whenever a client has an instance of instruction info, it should
  126. /// always be able to get register info as well (through this method).
  127. ///
  128. const X86RegisterInfo &getRegisterInfo() const { return RI; }
  129. /// Returns the stack pointer adjustment that happens inside the frame
  130. /// setup..destroy sequence (e.g. by pushes, or inside the callee).
  131. int64_t getFrameAdjustment(const MachineInstr &I) const {
  132. assert(isFrameInstr(I));
  133. if (isFrameSetup(I))
  134. return I.getOperand(2).getImm();
  135. return I.getOperand(1).getImm();
  136. }
  137. /// Sets the stack pointer adjustment made inside the frame made up by this
  138. /// instruction.
  139. void setFrameAdjustment(MachineInstr &I, int64_t V) const {
  140. assert(isFrameInstr(I));
  141. if (isFrameSetup(I))
  142. I.getOperand(2).setImm(V);
  143. else
  144. I.getOperand(1).setImm(V);
  145. }
  146. /// getSPAdjust - This returns the stack pointer adjustment made by
  147. /// this instruction. For x86, we need to handle more complex call
  148. /// sequences involving PUSHes.
  149. int getSPAdjust(const MachineInstr &MI) const override;
  150. /// isCoalescableExtInstr - Return true if the instruction is a "coalescable"
  151. /// extension instruction. That is, it's like a copy where it's legal for the
  152. /// source to overlap the destination. e.g. X86::MOVSX64rr32. If this returns
  153. /// true, then it's expected the pre-extension value is available as a subreg
  154. /// of the result register. This also returns the sub-register index in
  155. /// SubIdx.
  156. bool isCoalescableExtInstr(const MachineInstr &MI, Register &SrcReg,
  157. Register &DstReg, unsigned &SubIdx) const override;
  158. /// Returns true if the instruction has no behavior (specified or otherwise)
  159. /// that is based on the value of any of its register operands
  160. ///
  161. /// Instructions are considered data invariant even if they set EFLAGS.
  162. ///
  163. /// A classical example of something that is inherently not data invariant is
  164. /// an indirect jump -- the destination is loaded into icache based on the
  165. /// bits set in the jump destination register.
  166. ///
  167. /// FIXME: This should become part of our instruction tables.
  168. static bool isDataInvariant(MachineInstr &MI);
  169. /// Returns true if the instruction has no behavior (specified or otherwise)
  170. /// that is based on the value loaded from memory or the value of any
  171. /// non-address register operands.
  172. ///
  173. /// For example, if the latency of the instruction is dependent on the
  174. /// particular bits set in any of the registers *or* any of the bits loaded
  175. /// from memory.
  176. ///
  177. /// Instructions are considered data invariant even if they set EFLAGS.
  178. ///
  179. /// A classical example of something that is inherently not data invariant is
  180. /// an indirect jump -- the destination is loaded into icache based on the
  181. /// bits set in the jump destination register.
  182. ///
  183. /// FIXME: This should become part of our instruction tables.
  184. static bool isDataInvariantLoad(MachineInstr &MI);
  185. unsigned isLoadFromStackSlot(const MachineInstr &MI,
  186. int &FrameIndex) const override;
  187. unsigned isLoadFromStackSlot(const MachineInstr &MI,
  188. int &FrameIndex,
  189. unsigned &MemBytes) const override;
  190. /// isLoadFromStackSlotPostFE - Check for post-frame ptr elimination
  191. /// stack locations as well. This uses a heuristic so it isn't
  192. /// reliable for correctness.
  193. unsigned isLoadFromStackSlotPostFE(const MachineInstr &MI,
  194. int &FrameIndex) const override;
  195. unsigned isStoreToStackSlot(const MachineInstr &MI,
  196. int &FrameIndex) const override;
  197. unsigned isStoreToStackSlot(const MachineInstr &MI,
  198. int &FrameIndex,
  199. unsigned &MemBytes) const override;
  200. /// isStoreToStackSlotPostFE - Check for post-frame ptr elimination
  201. /// stack locations as well. This uses a heuristic so it isn't
  202. /// reliable for correctness.
  203. unsigned isStoreToStackSlotPostFE(const MachineInstr &MI,
  204. int &FrameIndex) const override;
  205. bool isReallyTriviallyReMaterializable(const MachineInstr &MI) const override;
  206. void reMaterialize(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
  207. Register DestReg, unsigned SubIdx,
  208. const MachineInstr &Orig,
  209. const TargetRegisterInfo &TRI) const override;
  210. /// Given an operand within a MachineInstr, insert preceding code to put it
  211. /// into the right format for a particular kind of LEA instruction. This may
  212. /// involve using an appropriate super-register instead (with an implicit use
  213. /// of the original) or creating a new virtual register and inserting COPY
  214. /// instructions to get the data into the right class.
  215. ///
  216. /// Reference parameters are set to indicate how caller should add this
  217. /// operand to the LEA instruction.
  218. bool classifyLEAReg(MachineInstr &MI, const MachineOperand &Src,
  219. unsigned LEAOpcode, bool AllowSP, Register &NewSrc,
  220. bool &isKill, MachineOperand &ImplicitOp,
  221. LiveVariables *LV, LiveIntervals *LIS) const;
  222. /// convertToThreeAddress - This method must be implemented by targets that
  223. /// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
  224. /// may be able to convert a two-address instruction into a true
  225. /// three-address instruction on demand. This allows the X86 target (for
  226. /// example) to convert ADD and SHL instructions into LEA instructions if they
  227. /// would require register copies due to two-addressness.
  228. ///
  229. /// This method returns a null pointer if the transformation cannot be
  230. /// performed, otherwise it returns the new instruction.
  231. ///
  232. MachineInstr *convertToThreeAddress(MachineInstr &MI, LiveVariables *LV,
  233. LiveIntervals *LIS) const override;
  234. /// Returns true iff the routine could find two commutable operands in the
  235. /// given machine instruction.
  236. /// The 'SrcOpIdx1' and 'SrcOpIdx2' are INPUT and OUTPUT arguments. Their
  237. /// input values can be re-defined in this method only if the input values
  238. /// are not pre-defined, which is designated by the special value
  239. /// 'CommuteAnyOperandIndex' assigned to it.
  240. /// If both of indices are pre-defined and refer to some operands, then the
  241. /// method simply returns true if the corresponding operands are commutable
  242. /// and returns false otherwise.
  243. ///
  244. /// For example, calling this method this way:
  245. /// unsigned Op1 = 1, Op2 = CommuteAnyOperandIndex;
  246. /// findCommutedOpIndices(MI, Op1, Op2);
  247. /// can be interpreted as a query asking to find an operand that would be
  248. /// commutable with the operand#1.
  249. bool findCommutedOpIndices(const MachineInstr &MI, unsigned &SrcOpIdx1,
  250. unsigned &SrcOpIdx2) const override;
  251. /// Returns true if we have preference on the operands order in MI, the
  252. /// commute decision is returned in Commute.
  253. bool hasCommutePreference(MachineInstr &MI, bool &Commute) const override;
  254. /// Returns an adjusted FMA opcode that must be used in FMA instruction that
  255. /// performs the same computations as the given \p MI but which has the
  256. /// operands \p SrcOpIdx1 and \p SrcOpIdx2 commuted.
  257. /// It may return 0 if it is unsafe to commute the operands.
  258. /// Note that a machine instruction (instead of its opcode) is passed as the
  259. /// first parameter to make it possible to analyze the instruction's uses and
  260. /// commute the first operand of FMA even when it seems unsafe when you look
  261. /// at the opcode. For example, it is Ok to commute the first operand of
  262. /// VFMADD*SD_Int, if ONLY the lowest 64-bit element of the result is used.
  263. ///
  264. /// The returned FMA opcode may differ from the opcode in the given \p MI.
  265. /// For example, commuting the operands #1 and #3 in the following FMA
  266. /// FMA213 #1, #2, #3
  267. /// results into instruction with adjusted opcode:
  268. /// FMA231 #3, #2, #1
  269. unsigned
  270. getFMA3OpcodeToCommuteOperands(const MachineInstr &MI, unsigned SrcOpIdx1,
  271. unsigned SrcOpIdx2,
  272. const X86InstrFMA3Group &FMA3Group) const;
  273. // Branch analysis.
  274. bool isUnconditionalTailCall(const MachineInstr &MI) const override;
  275. bool canMakeTailCallConditional(SmallVectorImpl<MachineOperand> &Cond,
  276. const MachineInstr &TailCall) const override;
  277. void replaceBranchWithTailCall(MachineBasicBlock &MBB,
  278. SmallVectorImpl<MachineOperand> &Cond,
  279. const MachineInstr &TailCall) const override;
  280. bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
  281. MachineBasicBlock *&FBB,
  282. SmallVectorImpl<MachineOperand> &Cond,
  283. bool AllowModify) const override;
  284. std::optional<ExtAddrMode>
  285. getAddrModeFromMemoryOp(const MachineInstr &MemI,
  286. const TargetRegisterInfo *TRI) const override;
  287. bool getConstValDefinedInReg(const MachineInstr &MI, const Register Reg,
  288. int64_t &ImmVal) const override;
  289. bool preservesZeroValueInReg(const MachineInstr *MI,
  290. const Register NullValueReg,
  291. const TargetRegisterInfo *TRI) const override;
  292. bool getMemOperandsWithOffsetWidth(
  293. const MachineInstr &LdSt,
  294. SmallVectorImpl<const MachineOperand *> &BaseOps, int64_t &Offset,
  295. bool &OffsetIsScalable, unsigned &Width,
  296. const TargetRegisterInfo *TRI) const override;
  297. bool analyzeBranchPredicate(MachineBasicBlock &MBB,
  298. TargetInstrInfo::MachineBranchPredicate &MBP,
  299. bool AllowModify = false) const override;
  300. unsigned removeBranch(MachineBasicBlock &MBB,
  301. int *BytesRemoved = nullptr) const override;
  302. unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
  303. MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
  304. const DebugLoc &DL,
  305. int *BytesAdded = nullptr) const override;
  306. bool canInsertSelect(const MachineBasicBlock &, ArrayRef<MachineOperand> Cond,
  307. Register, Register, Register, int &, int &,
  308. int &) const override;
  309. void insertSelect(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
  310. const DebugLoc &DL, Register DstReg,
  311. ArrayRef<MachineOperand> Cond, Register TrueReg,
  312. Register FalseReg) const override;
  313. void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
  314. const DebugLoc &DL, MCRegister DestReg, MCRegister SrcReg,
  315. bool KillSrc) const override;
  316. void storeRegToStackSlot(MachineBasicBlock &MBB,
  317. MachineBasicBlock::iterator MI, Register SrcReg,
  318. bool isKill, int FrameIndex,
  319. const TargetRegisterClass *RC,
  320. const TargetRegisterInfo *TRI,
  321. Register VReg) const override;
  322. void loadRegFromStackSlot(MachineBasicBlock &MBB,
  323. MachineBasicBlock::iterator MI, Register DestReg,
  324. int FrameIndex, const TargetRegisterClass *RC,
  325. const TargetRegisterInfo *TRI,
  326. Register VReg) const override;
  327. void loadStoreTileReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
  328. unsigned Opc, Register Reg, int FrameIdx,
  329. bool isKill = false) const;
  330. bool expandPostRAPseudo(MachineInstr &MI) const override;
  331. /// Check whether the target can fold a load that feeds a subreg operand
  332. /// (or a subreg operand that feeds a store).
  333. bool isSubregFoldable() const override { return true; }
  334. /// foldMemoryOperand - If this target supports it, fold a load or store of
  335. /// the specified stack slot into the specified machine instruction for the
  336. /// specified operand(s). If this is possible, the target should perform the
  337. /// folding and return true, otherwise it should return false. If it folds
  338. /// the instruction, it is likely that the MachineInstruction the iterator
  339. /// references has been changed.
  340. MachineInstr *
  341. foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
  342. ArrayRef<unsigned> Ops,
  343. MachineBasicBlock::iterator InsertPt, int FrameIndex,
  344. LiveIntervals *LIS = nullptr,
  345. VirtRegMap *VRM = nullptr) const override;
  346. /// foldMemoryOperand - Same as the previous version except it allows folding
  347. /// of any load and store from / to any address, not just from a specific
  348. /// stack slot.
  349. MachineInstr *foldMemoryOperandImpl(
  350. MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
  351. MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
  352. LiveIntervals *LIS = nullptr) const override;
  353. /// unfoldMemoryOperand - Separate a single instruction which folded a load or
  354. /// a store or a load and a store into two or more instruction. If this is
  355. /// possible, returns true as well as the new instructions by reference.
  356. bool
  357. unfoldMemoryOperand(MachineFunction &MF, MachineInstr &MI, unsigned Reg,
  358. bool UnfoldLoad, bool UnfoldStore,
  359. SmallVectorImpl<MachineInstr *> &NewMIs) const override;
  360. bool unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
  361. SmallVectorImpl<SDNode *> &NewNodes) const override;
  362. /// getOpcodeAfterMemoryUnfold - Returns the opcode of the would be new
  363. /// instruction after load / store are unfolded from an instruction of the
  364. /// specified opcode. It returns zero if the specified unfolding is not
  365. /// possible. If LoadRegIndex is non-null, it is filled in with the operand
  366. /// index of the operand which will hold the register holding the loaded
  367. /// value.
  368. unsigned
  369. getOpcodeAfterMemoryUnfold(unsigned Opc, bool UnfoldLoad, bool UnfoldStore,
  370. unsigned *LoadRegIndex = nullptr) const override;
  371. /// areLoadsFromSameBasePtr - This is used by the pre-regalloc scheduler
  372. /// to determine if two loads are loading from the same base address. It
  373. /// should only return true if the base pointers are the same and the
  374. /// only differences between the two addresses are the offset. It also returns
  375. /// the offsets by reference.
  376. bool areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2, int64_t &Offset1,
  377. int64_t &Offset2) const override;
  378. /// isSchedulingBoundary - Overrides the isSchedulingBoundary from
  379. /// Codegen/TargetInstrInfo.cpp to make it capable of identifying ENDBR
  380. /// intructions and prevent it from being re-scheduled.
  381. bool isSchedulingBoundary(const MachineInstr &MI,
  382. const MachineBasicBlock *MBB,
  383. const MachineFunction &MF) const override;
  384. /// shouldScheduleLoadsNear - This is a used by the pre-regalloc scheduler to
  385. /// determine (in conjunction with areLoadsFromSameBasePtr) if two loads
  386. /// should be scheduled togther. On some targets if two loads are loading from
  387. /// addresses in the same cache line, it's better if they are scheduled
  388. /// together. This function takes two integers that represent the load offsets
  389. /// from the common base address. It returns true if it decides it's desirable
  390. /// to schedule the two loads together. "NumLoads" is the number of loads that
  391. /// have already been scheduled after Load1.
  392. bool shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2, int64_t Offset1,
  393. int64_t Offset2,
  394. unsigned NumLoads) const override;
  395. MCInst getNop() const override;
  396. bool
  397. reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
  398. /// isSafeToMoveRegClassDefs - Return true if it's safe to move a machine
  399. /// instruction that defines the specified register class.
  400. bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const override;
  401. /// True if MI has a condition code def, e.g. EFLAGS, that is
  402. /// not marked dead.
  403. bool hasLiveCondCodeDef(MachineInstr &MI) const;
  404. /// getGlobalBaseReg - Return a virtual register initialized with the
  405. /// the global base register value. Output instructions required to
  406. /// initialize the register in the function entry block, if necessary.
  407. ///
  408. unsigned getGlobalBaseReg(MachineFunction *MF) const;
  409. std::pair<uint16_t, uint16_t>
  410. getExecutionDomain(const MachineInstr &MI) const override;
  411. uint16_t getExecutionDomainCustom(const MachineInstr &MI) const;
  412. void setExecutionDomain(MachineInstr &MI, unsigned Domain) const override;
  413. bool setExecutionDomainCustom(MachineInstr &MI, unsigned Domain) const;
  414. unsigned
  415. getPartialRegUpdateClearance(const MachineInstr &MI, unsigned OpNum,
  416. const TargetRegisterInfo *TRI) const override;
  417. unsigned getUndefRegClearance(const MachineInstr &MI, unsigned OpNum,
  418. const TargetRegisterInfo *TRI) const override;
  419. void breakPartialRegDependency(MachineInstr &MI, unsigned OpNum,
  420. const TargetRegisterInfo *TRI) const override;
  421. MachineInstr *foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI,
  422. unsigned OpNum,
  423. ArrayRef<MachineOperand> MOs,
  424. MachineBasicBlock::iterator InsertPt,
  425. unsigned Size, Align Alignment,
  426. bool AllowCommute) const;
  427. bool isHighLatencyDef(int opc) const override;
  428. bool hasHighOperandLatency(const TargetSchedModel &SchedModel,
  429. const MachineRegisterInfo *MRI,
  430. const MachineInstr &DefMI, unsigned DefIdx,
  431. const MachineInstr &UseMI,
  432. unsigned UseIdx) const override;
  433. bool useMachineCombiner() const override { return true; }
  434. bool isAssociativeAndCommutative(const MachineInstr &Inst,
  435. bool Invert) const override;
  436. bool hasReassociableOperands(const MachineInstr &Inst,
  437. const MachineBasicBlock *MBB) const override;
  438. void setSpecialOperandAttr(MachineInstr &OldMI1, MachineInstr &OldMI2,
  439. MachineInstr &NewMI1,
  440. MachineInstr &NewMI2) const override;
  441. /// analyzeCompare - For a comparison instruction, return the source registers
  442. /// in SrcReg and SrcReg2 if having two register operands, and the value it
  443. /// compares against in CmpValue. Return true if the comparison instruction
  444. /// can be analyzed.
  445. bool analyzeCompare(const MachineInstr &MI, Register &SrcReg,
  446. Register &SrcReg2, int64_t &CmpMask,
  447. int64_t &CmpValue) const override;
  448. /// optimizeCompareInstr - Check if there exists an earlier instruction that
  449. /// operates on the same source operands and sets flags in the same way as
  450. /// Compare; remove Compare if possible.
  451. bool optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
  452. Register SrcReg2, int64_t CmpMask, int64_t CmpValue,
  453. const MachineRegisterInfo *MRI) const override;
  454. /// optimizeLoadInstr - Try to remove the load by folding it to a register
  455. /// operand at the use. We fold the load instructions if and only if the
  456. /// def and use are in the same BB. We only look at one load and see
  457. /// whether it can be folded into MI. FoldAsLoadDefReg is the virtual register
  458. /// defined by the load we are trying to fold. DefMI returns the machine
  459. /// instruction that defines FoldAsLoadDefReg, and the function returns
  460. /// the machine instruction generated due to folding.
  461. MachineInstr *optimizeLoadInstr(MachineInstr &MI,
  462. const MachineRegisterInfo *MRI,
  463. Register &FoldAsLoadDefReg,
  464. MachineInstr *&DefMI) const override;
  465. std::pair<unsigned, unsigned>
  466. decomposeMachineOperandsTargetFlags(unsigned TF) const override;
  467. ArrayRef<std::pair<unsigned, const char *>>
  468. getSerializableDirectMachineOperandTargetFlags() const override;
  469. outliner::OutlinedFunction getOutliningCandidateInfo(
  470. std::vector<outliner::Candidate> &RepeatedSequenceLocs) const override;
  471. bool isFunctionSafeToOutlineFrom(MachineFunction &MF,
  472. bool OutlineFromLinkOnceODRs) const override;
  473. outliner::InstrType
  474. getOutliningType(MachineBasicBlock::iterator &MIT, unsigned Flags) const override;
  475. void buildOutlinedFrame(MachineBasicBlock &MBB, MachineFunction &MF,
  476. const outliner::OutlinedFunction &OF) const override;
  477. MachineBasicBlock::iterator
  478. insertOutlinedCall(Module &M, MachineBasicBlock &MBB,
  479. MachineBasicBlock::iterator &It, MachineFunction &MF,
  480. outliner::Candidate &C) const override;
  481. bool verifyInstruction(const MachineInstr &MI,
  482. StringRef &ErrInfo) const override;
  483. #define GET_INSTRINFO_HELPER_DECLS
  484. #include "X86GenInstrInfo.inc"
  485. static bool hasLockPrefix(const MachineInstr &MI) {
  486. return MI.getDesc().TSFlags & X86II::LOCK;
  487. }
  488. std::optional<ParamLoadedValue>
  489. describeLoadedValue(const MachineInstr &MI, Register Reg) const override;
  490. protected:
  491. /// Commutes the operands in the given instruction by changing the operands
  492. /// order and/or changing the instruction's opcode and/or the immediate value
  493. /// operand.
  494. ///
  495. /// The arguments 'CommuteOpIdx1' and 'CommuteOpIdx2' specify the operands
  496. /// to be commuted.
  497. ///
  498. /// Do not call this method for a non-commutable instruction or
  499. /// non-commutable operands.
  500. /// Even though the instruction is commutable, the method may still
  501. /// fail to commute the operands, null pointer is returned in such cases.
  502. MachineInstr *commuteInstructionImpl(MachineInstr &MI, bool NewMI,
  503. unsigned CommuteOpIdx1,
  504. unsigned CommuteOpIdx2) const override;
  505. /// If the specific machine instruction is a instruction that moves/copies
  506. /// value from one register to another register return destination and source
  507. /// registers as machine operands.
  508. std::optional<DestSourcePair>
  509. isCopyInstrImpl(const MachineInstr &MI) const override;
  510. private:
  511. /// This is a helper for convertToThreeAddress for 8 and 16-bit instructions.
  512. /// We use 32-bit LEA to form 3-address code by promoting to a 32-bit
  513. /// super-register and then truncating back down to a 8/16-bit sub-register.
  514. MachineInstr *convertToThreeAddressWithLEA(unsigned MIOpc, MachineInstr &MI,
  515. LiveVariables *LV,
  516. LiveIntervals *LIS,
  517. bool Is8BitOp) const;
  518. /// Handles memory folding for special case instructions, for instance those
  519. /// requiring custom manipulation of the address.
  520. MachineInstr *foldMemoryOperandCustom(MachineFunction &MF, MachineInstr &MI,
  521. unsigned OpNum,
  522. ArrayRef<MachineOperand> MOs,
  523. MachineBasicBlock::iterator InsertPt,
  524. unsigned Size, Align Alignment) const;
  525. /// isFrameOperand - Return true and the FrameIndex if the specified
  526. /// operand and follow operands form a reference to the stack frame.
  527. bool isFrameOperand(const MachineInstr &MI, unsigned int Op,
  528. int &FrameIndex) const;
  529. /// Returns true iff the routine could find two commutable operands in the
  530. /// given machine instruction with 3 vector inputs.
  531. /// The 'SrcOpIdx1' and 'SrcOpIdx2' are INPUT and OUTPUT arguments. Their
  532. /// input values can be re-defined in this method only if the input values
  533. /// are not pre-defined, which is designated by the special value
  534. /// 'CommuteAnyOperandIndex' assigned to it.
  535. /// If both of indices are pre-defined and refer to some operands, then the
  536. /// method simply returns true if the corresponding operands are commutable
  537. /// and returns false otherwise.
  538. ///
  539. /// For example, calling this method this way:
  540. /// unsigned Op1 = 1, Op2 = CommuteAnyOperandIndex;
  541. /// findThreeSrcCommutedOpIndices(MI, Op1, Op2);
  542. /// can be interpreted as a query asking to find an operand that would be
  543. /// commutable with the operand#1.
  544. ///
  545. /// If IsIntrinsic is set, operand 1 will be ignored for commuting.
  546. bool findThreeSrcCommutedOpIndices(const MachineInstr &MI,
  547. unsigned &SrcOpIdx1,
  548. unsigned &SrcOpIdx2,
  549. bool IsIntrinsic = false) const;
  550. /// Returns true when instruction \p FlagI produces the same flags as \p OI.
  551. /// The caller should pass in the results of calling analyzeCompare on \p OI:
  552. /// \p SrcReg, \p SrcReg2, \p ImmMask, \p ImmValue.
  553. /// If the flags match \p OI as if it had the input operands swapped then the
  554. /// function succeeds and sets \p IsSwapped to true.
  555. ///
  556. /// Examples of OI, FlagI pairs returning true:
  557. /// CMP %1, 42 and CMP %1, 42
  558. /// CMP %1, %2 and %3 = SUB %1, %2
  559. /// TEST %1, %1 and %2 = SUB %1, 0
  560. /// CMP %1, %2 and %3 = SUB %2, %1 ; IsSwapped=true
  561. bool isRedundantFlagInstr(const MachineInstr &FlagI, Register SrcReg,
  562. Register SrcReg2, int64_t ImmMask, int64_t ImmValue,
  563. const MachineInstr &OI, bool *IsSwapped,
  564. int64_t *ImmDelta) const;
  565. };
  566. } // namespace llvm
  567. #endif