X86InstrInfo.h 31 KB

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