PPCInstrInfo.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. //===-- PPCInstrInfo.h - PowerPC 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 PowerPC implementation of the TargetInstrInfo class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_LIB_TARGET_POWERPC_PPCINSTRINFO_H
  13. #define LLVM_LIB_TARGET_POWERPC_PPCINSTRINFO_H
  14. #include "PPCRegisterInfo.h"
  15. #include "llvm/CodeGen/TargetInstrInfo.h"
  16. #define GET_INSTRINFO_HEADER
  17. #include "PPCGenInstrInfo.inc"
  18. namespace llvm {
  19. /// PPCII - This namespace holds all of the PowerPC target-specific
  20. /// per-instruction flags. These must match the corresponding definitions in
  21. /// PPC.td and PPCInstrFormats.td.
  22. namespace PPCII {
  23. enum {
  24. // PPC970 Instruction Flags. These flags describe the characteristics of the
  25. // PowerPC 970 (aka G5) dispatch groups and how they are formed out of
  26. // raw machine instructions.
  27. /// PPC970_First - This instruction starts a new dispatch group, so it will
  28. /// always be the first one in the group.
  29. PPC970_First = 0x1,
  30. /// PPC970_Single - This instruction starts a new dispatch group and
  31. /// terminates it, so it will be the sole instruction in the group.
  32. PPC970_Single = 0x2,
  33. /// PPC970_Cracked - This instruction is cracked into two pieces, requiring
  34. /// two dispatch pipes to be available to issue.
  35. PPC970_Cracked = 0x4,
  36. /// PPC970_Mask/Shift - This is a bitmask that selects the pipeline type that
  37. /// an instruction is issued to.
  38. PPC970_Shift = 3,
  39. PPC970_Mask = 0x07 << PPC970_Shift
  40. };
  41. enum PPC970_Unit {
  42. /// These are the various PPC970 execution unit pipelines. Each instruction
  43. /// is one of these.
  44. PPC970_Pseudo = 0 << PPC970_Shift, // Pseudo instruction
  45. PPC970_FXU = 1 << PPC970_Shift, // Fixed Point (aka Integer/ALU) Unit
  46. PPC970_LSU = 2 << PPC970_Shift, // Load Store Unit
  47. PPC970_FPU = 3 << PPC970_Shift, // Floating Point Unit
  48. PPC970_CRU = 4 << PPC970_Shift, // Control Register Unit
  49. PPC970_VALU = 5 << PPC970_Shift, // Vector ALU
  50. PPC970_VPERM = 6 << PPC970_Shift, // Vector Permute Unit
  51. PPC970_BRU = 7 << PPC970_Shift // Branch Unit
  52. };
  53. enum {
  54. /// Shift count to bypass PPC970 flags
  55. NewDef_Shift = 6,
  56. /// This instruction is an X-Form memory operation.
  57. XFormMemOp = 0x1 << NewDef_Shift,
  58. /// This instruction is prefixed.
  59. Prefixed = 0x1 << (NewDef_Shift+1)
  60. };
  61. } // end namespace PPCII
  62. // Instructions that have an immediate form might be convertible to that
  63. // form if the correct input is a result of a load immediate. In order to
  64. // know whether the transformation is special, we might need to know some
  65. // of the details of the two forms.
  66. struct ImmInstrInfo {
  67. // Is the immediate field in the immediate form signed or unsigned?
  68. uint64_t SignedImm : 1;
  69. // Does the immediate need to be a multiple of some value?
  70. uint64_t ImmMustBeMultipleOf : 5;
  71. // Is R0/X0 treated specially by the original r+r instruction?
  72. // If so, in which operand?
  73. uint64_t ZeroIsSpecialOrig : 3;
  74. // Is R0/X0 treated specially by the new r+i instruction?
  75. // If so, in which operand?
  76. uint64_t ZeroIsSpecialNew : 3;
  77. // Is the operation commutative?
  78. uint64_t IsCommutative : 1;
  79. // The operand number to check for add-immediate def.
  80. uint64_t OpNoForForwarding : 3;
  81. // The operand number for the immediate.
  82. uint64_t ImmOpNo : 3;
  83. // The opcode of the new instruction.
  84. uint64_t ImmOpcode : 16;
  85. // The size of the immediate.
  86. uint64_t ImmWidth : 5;
  87. // The immediate should be truncated to N bits.
  88. uint64_t TruncateImmTo : 5;
  89. // Is the instruction summing the operand
  90. uint64_t IsSummingOperands : 1;
  91. };
  92. // Information required to convert an instruction to just a materialized
  93. // immediate.
  94. struct LoadImmediateInfo {
  95. unsigned Imm : 16;
  96. unsigned Is64Bit : 1;
  97. unsigned SetCR : 1;
  98. };
  99. // Index into the OpcodesForSpill array.
  100. enum SpillOpcodeKey {
  101. SOK_Int4Spill,
  102. SOK_Int8Spill,
  103. SOK_Float8Spill,
  104. SOK_Float4Spill,
  105. SOK_CRSpill,
  106. SOK_CRBitSpill,
  107. SOK_VRVectorSpill,
  108. SOK_VSXVectorSpill,
  109. SOK_VectorFloat8Spill,
  110. SOK_VectorFloat4Spill,
  111. SOK_SpillToVSR,
  112. SOK_PairedVecSpill,
  113. SOK_AccumulatorSpill,
  114. SOK_UAccumulatorSpill,
  115. SOK_SPESpill,
  116. SOK_PairedG8Spill,
  117. SOK_LastOpcodeSpill // This must be last on the enum.
  118. };
  119. // Define list of load and store spill opcodes.
  120. #define NoInstr PPC::INSTRUCTION_LIST_END
  121. #define Pwr8LoadOpcodes \
  122. { \
  123. PPC::LWZ, PPC::LD, PPC::LFD, PPC::LFS, PPC::RESTORE_CR, \
  124. PPC::RESTORE_CRBIT, PPC::LVX, PPC::LXVD2X, PPC::LXSDX, PPC::LXSSPX, \
  125. PPC::SPILLTOVSR_LD, NoInstr, NoInstr, NoInstr, PPC::EVLDD, \
  126. PPC::RESTORE_QUADWORD \
  127. }
  128. #define Pwr9LoadOpcodes \
  129. { \
  130. PPC::LWZ, PPC::LD, PPC::LFD, PPC::LFS, PPC::RESTORE_CR, \
  131. PPC::RESTORE_CRBIT, PPC::LVX, PPC::LXV, PPC::DFLOADf64, \
  132. PPC::DFLOADf32, PPC::SPILLTOVSR_LD, NoInstr, NoInstr, NoInstr, \
  133. NoInstr, PPC::RESTORE_QUADWORD \
  134. }
  135. #define Pwr10LoadOpcodes \
  136. { \
  137. PPC::LWZ, PPC::LD, PPC::LFD, PPC::LFS, PPC::RESTORE_CR, \
  138. PPC::RESTORE_CRBIT, PPC::LVX, PPC::LXV, PPC::DFLOADf64, \
  139. PPC::DFLOADf32, PPC::SPILLTOVSR_LD, PPC::LXVP, PPC::RESTORE_ACC, \
  140. PPC::RESTORE_UACC, NoInstr, PPC::RESTORE_QUADWORD \
  141. }
  142. #define Pwr8StoreOpcodes \
  143. { \
  144. PPC::STW, PPC::STD, PPC::STFD, PPC::STFS, PPC::SPILL_CR, PPC::SPILL_CRBIT, \
  145. PPC::STVX, PPC::STXVD2X, PPC::STXSDX, PPC::STXSSPX, \
  146. PPC::SPILLTOVSR_ST, NoInstr, NoInstr, NoInstr, PPC::EVSTDD, \
  147. PPC::SPILL_QUADWORD \
  148. }
  149. #define Pwr9StoreOpcodes \
  150. { \
  151. PPC::STW, PPC::STD, PPC::STFD, PPC::STFS, PPC::SPILL_CR, PPC::SPILL_CRBIT, \
  152. PPC::STVX, PPC::STXV, PPC::DFSTOREf64, PPC::DFSTOREf32, \
  153. PPC::SPILLTOVSR_ST, NoInstr, NoInstr, NoInstr, NoInstr, \
  154. PPC::SPILL_QUADWORD \
  155. }
  156. #define Pwr10StoreOpcodes \
  157. { \
  158. PPC::STW, PPC::STD, PPC::STFD, PPC::STFS, PPC::SPILL_CR, PPC::SPILL_CRBIT, \
  159. PPC::STVX, PPC::STXV, PPC::DFSTOREf64, PPC::DFSTOREf32, \
  160. PPC::SPILLTOVSR_ST, PPC::STXVP, PPC::SPILL_ACC, PPC::SPILL_UACC, \
  161. NoInstr, PPC::SPILL_QUADWORD \
  162. }
  163. // Initialize arrays for load and store spill opcodes on supported subtargets.
  164. #define StoreOpcodesForSpill \
  165. { Pwr8StoreOpcodes, Pwr9StoreOpcodes, Pwr10StoreOpcodes }
  166. #define LoadOpcodesForSpill \
  167. { Pwr8LoadOpcodes, Pwr9LoadOpcodes, Pwr10LoadOpcodes }
  168. class PPCSubtarget;
  169. class PPCInstrInfo : public PPCGenInstrInfo {
  170. PPCSubtarget &Subtarget;
  171. const PPCRegisterInfo RI;
  172. const unsigned StoreSpillOpcodesArray[3][SOK_LastOpcodeSpill] =
  173. StoreOpcodesForSpill;
  174. const unsigned LoadSpillOpcodesArray[3][SOK_LastOpcodeSpill] =
  175. LoadOpcodesForSpill;
  176. void StoreRegToStackSlot(MachineFunction &MF, unsigned SrcReg, bool isKill,
  177. int FrameIdx, const TargetRegisterClass *RC,
  178. SmallVectorImpl<MachineInstr *> &NewMIs) const;
  179. void LoadRegFromStackSlot(MachineFunction &MF, const DebugLoc &DL,
  180. unsigned DestReg, int FrameIdx,
  181. const TargetRegisterClass *RC,
  182. SmallVectorImpl<MachineInstr *> &NewMIs) const;
  183. // Replace the instruction with single LI if possible. \p DefMI must be LI or
  184. // LI8.
  185. bool simplifyToLI(MachineInstr &MI, MachineInstr &DefMI,
  186. unsigned OpNoForForwarding, MachineInstr **KilledDef) const;
  187. // If the inst is imm-form and its register operand is produced by a ADDI, put
  188. // the imm into the inst directly and remove the ADDI if possible.
  189. bool transformToNewImmFormFedByAdd(MachineInstr &MI, MachineInstr &DefMI,
  190. unsigned OpNoForForwarding) const;
  191. // If the inst is x-form and has imm-form and one of its operand is produced
  192. // by a LI, put the imm into the inst directly and remove the LI if possible.
  193. bool transformToImmFormFedByLI(MachineInstr &MI, const ImmInstrInfo &III,
  194. unsigned ConstantOpNo,
  195. MachineInstr &DefMI) const;
  196. // If the inst is x-form and has imm-form and one of its operand is produced
  197. // by an add-immediate, try to transform it when possible.
  198. bool transformToImmFormFedByAdd(MachineInstr &MI, const ImmInstrInfo &III,
  199. unsigned ConstantOpNo, MachineInstr &DefMI,
  200. bool KillDefMI) const;
  201. // Try to find that, if the instruction 'MI' contains any operand that
  202. // could be forwarded from some inst that feeds it. If yes, return the
  203. // Def of that operand. And OpNoForForwarding is the operand index in
  204. // the 'MI' for that 'Def'. If we see another use of this Def between
  205. // the Def and the MI, SeenIntermediateUse becomes 'true'.
  206. MachineInstr *getForwardingDefMI(MachineInstr &MI,
  207. unsigned &OpNoForForwarding,
  208. bool &SeenIntermediateUse) const;
  209. // Can the user MI have it's source at index \p OpNoForForwarding
  210. // forwarded from an add-immediate that feeds it?
  211. bool isUseMIElgibleForForwarding(MachineInstr &MI, const ImmInstrInfo &III,
  212. unsigned OpNoForForwarding) const;
  213. bool isDefMIElgibleForForwarding(MachineInstr &DefMI,
  214. const ImmInstrInfo &III,
  215. MachineOperand *&ImmMO,
  216. MachineOperand *&RegMO) const;
  217. bool isImmElgibleForForwarding(const MachineOperand &ImmMO,
  218. const MachineInstr &DefMI,
  219. const ImmInstrInfo &III,
  220. int64_t &Imm,
  221. int64_t BaseImm = 0) const;
  222. bool isRegElgibleForForwarding(const MachineOperand &RegMO,
  223. const MachineInstr &DefMI,
  224. const MachineInstr &MI, bool KillDefMI,
  225. bool &IsFwdFeederRegKilled) const;
  226. unsigned getSpillTarget() const;
  227. const unsigned *getStoreOpcodesForSpillArray() const;
  228. const unsigned *getLoadOpcodesForSpillArray() const;
  229. unsigned getSpillIndex(const TargetRegisterClass *RC) const;
  230. int16_t getFMAOpIdxInfo(unsigned Opcode) const;
  231. void reassociateFMA(MachineInstr &Root, MachineCombinerPattern Pattern,
  232. SmallVectorImpl<MachineInstr *> &InsInstrs,
  233. SmallVectorImpl<MachineInstr *> &DelInstrs,
  234. DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const;
  235. bool isLoadFromConstantPool(MachineInstr *I) const;
  236. Register
  237. generateLoadForNewConst(unsigned Idx, MachineInstr *MI, Type *Ty,
  238. SmallVectorImpl<MachineInstr *> &InsInstrs) const;
  239. const Constant *getConstantFromConstantPool(MachineInstr *I) const;
  240. virtual void anchor();
  241. protected:
  242. /// Commutes the operands in the given instruction.
  243. /// The commutable operands are specified by their indices OpIdx1 and OpIdx2.
  244. ///
  245. /// Do not call this method for a non-commutable instruction or for
  246. /// non-commutable pair of operand indices OpIdx1 and OpIdx2.
  247. /// Even though the instruction is commutable, the method may still
  248. /// fail to commute the operands, null pointer is returned in such cases.
  249. ///
  250. /// For example, we can commute rlwimi instructions, but only if the
  251. /// rotate amt is zero. We also have to munge the immediates a bit.
  252. MachineInstr *commuteInstructionImpl(MachineInstr &MI, bool NewMI,
  253. unsigned OpIdx1,
  254. unsigned OpIdx2) const override;
  255. public:
  256. explicit PPCInstrInfo(PPCSubtarget &STI);
  257. /// getRegisterInfo - TargetInstrInfo is a superset of MRegister info. As
  258. /// such, whenever a client has an instance of instruction info, it should
  259. /// always be able to get register info as well (through this method).
  260. ///
  261. const PPCRegisterInfo &getRegisterInfo() const { return RI; }
  262. bool isXFormMemOp(unsigned Opcode) const {
  263. return get(Opcode).TSFlags & PPCII::XFormMemOp;
  264. }
  265. bool isPrefixed(unsigned Opcode) const {
  266. return get(Opcode).TSFlags & PPCII::Prefixed;
  267. }
  268. static bool isSameClassPhysRegCopy(unsigned Opcode) {
  269. unsigned CopyOpcodes[] = {PPC::OR, PPC::OR8, PPC::FMR,
  270. PPC::VOR, PPC::XXLOR, PPC::XXLORf,
  271. PPC::XSCPSGNDP, PPC::MCRF, PPC::CROR,
  272. PPC::EVOR, -1U};
  273. for (int i = 0; CopyOpcodes[i] != -1U; i++)
  274. if (Opcode == CopyOpcodes[i])
  275. return true;
  276. return false;
  277. }
  278. ScheduleHazardRecognizer *
  279. CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
  280. const ScheduleDAG *DAG) const override;
  281. ScheduleHazardRecognizer *
  282. CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
  283. const ScheduleDAG *DAG) const override;
  284. unsigned getInstrLatency(const InstrItineraryData *ItinData,
  285. const MachineInstr &MI,
  286. unsigned *PredCost = nullptr) const override;
  287. int getOperandLatency(const InstrItineraryData *ItinData,
  288. const MachineInstr &DefMI, unsigned DefIdx,
  289. const MachineInstr &UseMI,
  290. unsigned UseIdx) const override;
  291. int getOperandLatency(const InstrItineraryData *ItinData,
  292. SDNode *DefNode, unsigned DefIdx,
  293. SDNode *UseNode, unsigned UseIdx) const override {
  294. return PPCGenInstrInfo::getOperandLatency(ItinData, DefNode, DefIdx,
  295. UseNode, UseIdx);
  296. }
  297. bool hasLowDefLatency(const TargetSchedModel &SchedModel,
  298. const MachineInstr &DefMI,
  299. unsigned DefIdx) const override {
  300. // Machine LICM should hoist all instructions in low-register-pressure
  301. // situations; none are sufficiently free to justify leaving in a loop
  302. // body.
  303. return false;
  304. }
  305. bool useMachineCombiner() const override {
  306. return true;
  307. }
  308. /// When getMachineCombinerPatterns() finds patterns, this function generates
  309. /// the instructions that could replace the original code sequence
  310. void genAlternativeCodeSequence(
  311. MachineInstr &Root, MachineCombinerPattern Pattern,
  312. SmallVectorImpl<MachineInstr *> &InsInstrs,
  313. SmallVectorImpl<MachineInstr *> &DelInstrs,
  314. DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const override;
  315. /// Return true when there is potentially a faster code sequence for a fma
  316. /// chain ending in \p Root. All potential patterns are output in the \p
  317. /// P array.
  318. bool getFMAPatterns(MachineInstr &Root,
  319. SmallVectorImpl<MachineCombinerPattern> &P,
  320. bool DoRegPressureReduce) const;
  321. /// Return true when there is potentially a faster code sequence
  322. /// for an instruction chain ending in <Root>. All potential patterns are
  323. /// output in the <Pattern> array.
  324. bool getMachineCombinerPatterns(MachineInstr &Root,
  325. SmallVectorImpl<MachineCombinerPattern> &P,
  326. bool DoRegPressureReduce) const override;
  327. /// On PowerPC, we leverage machine combiner pass to reduce register pressure
  328. /// when the register pressure is high for one BB.
  329. /// Return true if register pressure for \p MBB is high and ABI is supported
  330. /// to reduce register pressure. Otherwise return false.
  331. bool
  332. shouldReduceRegisterPressure(MachineBasicBlock *MBB,
  333. RegisterClassInfo *RegClassInfo) const override;
  334. /// Fixup the placeholders we put in genAlternativeCodeSequence() for
  335. /// MachineCombiner.
  336. void
  337. finalizeInsInstrs(MachineInstr &Root, MachineCombinerPattern &P,
  338. SmallVectorImpl<MachineInstr *> &InsInstrs) const override;
  339. bool isAssociativeAndCommutative(const MachineInstr &Inst) const override;
  340. /// On PowerPC, we try to reassociate FMA chain which will increase
  341. /// instruction size. Set extension resource length limit to 1 for edge case.
  342. /// Resource Length is calculated by scaled resource usage in getCycles().
  343. /// Because of the division in getCycles(), it returns different cycles due to
  344. /// legacy scaled resource usage. So new resource length may be same with
  345. /// legacy or 1 bigger than legacy.
  346. /// We need to execlude the 1 bigger case even the resource length is not
  347. /// perserved for more FMA chain reassociations on PowerPC.
  348. int getExtendResourceLenLimit() const override { return 1; }
  349. void setSpecialOperandAttr(MachineInstr &OldMI1, MachineInstr &OldMI2,
  350. MachineInstr &NewMI1,
  351. MachineInstr &NewMI2) const override;
  352. // PowerPC specific version of setSpecialOperandAttr that copies Flags to MI
  353. // and clears nuw, nsw, and exact flags.
  354. void setSpecialOperandAttr(MachineInstr &MI, uint16_t Flags) const;
  355. bool isCoalescableExtInstr(const MachineInstr &MI,
  356. Register &SrcReg, Register &DstReg,
  357. unsigned &SubIdx) const override;
  358. unsigned isLoadFromStackSlot(const MachineInstr &MI,
  359. int &FrameIndex) const override;
  360. bool isReallyTriviallyReMaterializable(const MachineInstr &MI,
  361. AAResults *AA) const override;
  362. unsigned isStoreToStackSlot(const MachineInstr &MI,
  363. int &FrameIndex) const override;
  364. bool findCommutedOpIndices(const MachineInstr &MI, unsigned &SrcOpIdx1,
  365. unsigned &SrcOpIdx2) const override;
  366. void insertNoop(MachineBasicBlock &MBB,
  367. MachineBasicBlock::iterator MI) const override;
  368. // Branch analysis.
  369. bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
  370. MachineBasicBlock *&FBB,
  371. SmallVectorImpl<MachineOperand> &Cond,
  372. bool AllowModify) const override;
  373. unsigned removeBranch(MachineBasicBlock &MBB,
  374. int *BytesRemoved = nullptr) const override;
  375. unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
  376. MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
  377. const DebugLoc &DL,
  378. int *BytesAdded = nullptr) const override;
  379. // Select analysis.
  380. bool canInsertSelect(const MachineBasicBlock &, ArrayRef<MachineOperand> Cond,
  381. Register, Register, Register, int &, int &,
  382. int &) const override;
  383. void insertSelect(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
  384. const DebugLoc &DL, Register DstReg,
  385. ArrayRef<MachineOperand> Cond, Register TrueReg,
  386. Register FalseReg) const override;
  387. void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
  388. const DebugLoc &DL, MCRegister DestReg, MCRegister SrcReg,
  389. bool KillSrc) const override;
  390. void storeRegToStackSlot(MachineBasicBlock &MBB,
  391. MachineBasicBlock::iterator MBBI,
  392. Register SrcReg, bool isKill, int FrameIndex,
  393. const TargetRegisterClass *RC,
  394. const TargetRegisterInfo *TRI) const override;
  395. // Emits a register spill without updating the register class for vector
  396. // registers. This ensures that when we spill a vector register the
  397. // element order in the register is the same as it was in memory.
  398. void storeRegToStackSlotNoUpd(MachineBasicBlock &MBB,
  399. MachineBasicBlock::iterator MBBI,
  400. unsigned SrcReg, bool isKill, int FrameIndex,
  401. const TargetRegisterClass *RC,
  402. const TargetRegisterInfo *TRI) const;
  403. void loadRegFromStackSlot(MachineBasicBlock &MBB,
  404. MachineBasicBlock::iterator MBBI,
  405. Register DestReg, int FrameIndex,
  406. const TargetRegisterClass *RC,
  407. const TargetRegisterInfo *TRI) const override;
  408. // Emits a register reload without updating the register class for vector
  409. // registers. This ensures that when we reload a vector register the
  410. // element order in the register is the same as it was in memory.
  411. void loadRegFromStackSlotNoUpd(MachineBasicBlock &MBB,
  412. MachineBasicBlock::iterator MBBI,
  413. unsigned DestReg, int FrameIndex,
  414. const TargetRegisterClass *RC,
  415. const TargetRegisterInfo *TRI) const;
  416. unsigned getStoreOpcodeForSpill(const TargetRegisterClass *RC) const;
  417. unsigned getLoadOpcodeForSpill(const TargetRegisterClass *RC) const;
  418. bool
  419. reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
  420. bool FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, Register Reg,
  421. MachineRegisterInfo *MRI) const override;
  422. bool onlyFoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
  423. Register Reg) const;
  424. // If conversion by predication (only supported by some branch instructions).
  425. // All of the profitability checks always return true; it is always
  426. // profitable to use the predicated branches.
  427. bool isProfitableToIfCvt(MachineBasicBlock &MBB,
  428. unsigned NumCycles, unsigned ExtraPredCycles,
  429. BranchProbability Probability) const override {
  430. return true;
  431. }
  432. bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
  433. unsigned NumT, unsigned ExtraT,
  434. MachineBasicBlock &FMBB,
  435. unsigned NumF, unsigned ExtraF,
  436. BranchProbability Probability) const override;
  437. bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
  438. BranchProbability Probability) const override {
  439. return true;
  440. }
  441. bool isProfitableToUnpredicate(MachineBasicBlock &TMBB,
  442. MachineBasicBlock &FMBB) const override {
  443. return false;
  444. }
  445. // Predication support.
  446. bool isPredicated(const MachineInstr &MI) const override;
  447. bool isSchedulingBoundary(const MachineInstr &MI,
  448. const MachineBasicBlock *MBB,
  449. const MachineFunction &MF) const override;
  450. bool PredicateInstruction(MachineInstr &MI,
  451. ArrayRef<MachineOperand> Pred) const override;
  452. bool SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
  453. ArrayRef<MachineOperand> Pred2) const override;
  454. bool ClobbersPredicate(MachineInstr &MI, std::vector<MachineOperand> &Pred,
  455. bool SkipDead) const override;
  456. // Comparison optimization.
  457. bool analyzeCompare(const MachineInstr &MI, Register &SrcReg,
  458. Register &SrcReg2, int64_t &Mask,
  459. int64_t &Value) const override;
  460. bool optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
  461. Register SrcReg2, int64_t Mask, int64_t Value,
  462. const MachineRegisterInfo *MRI) const override;
  463. /// Return true if get the base operand, byte offset of an instruction and
  464. /// the memory width. Width is the size of memory that is being
  465. /// loaded/stored (e.g. 1, 2, 4, 8).
  466. bool getMemOperandWithOffsetWidth(const MachineInstr &LdSt,
  467. const MachineOperand *&BaseOp,
  468. int64_t &Offset, unsigned &Width,
  469. const TargetRegisterInfo *TRI) const;
  470. /// Get the base operand and byte offset of an instruction that reads/writes
  471. /// memory.
  472. bool getMemOperandsWithOffsetWidth(
  473. const MachineInstr &LdSt,
  474. SmallVectorImpl<const MachineOperand *> &BaseOps, int64_t &Offset,
  475. bool &OffsetIsScalable, unsigned &Width,
  476. const TargetRegisterInfo *TRI) const override;
  477. /// Returns true if the two given memory operations should be scheduled
  478. /// adjacent.
  479. bool shouldClusterMemOps(ArrayRef<const MachineOperand *> BaseOps1,
  480. ArrayRef<const MachineOperand *> BaseOps2,
  481. unsigned NumLoads, unsigned NumBytes) const override;
  482. /// Return true if two MIs access different memory addresses and false
  483. /// otherwise
  484. bool
  485. areMemAccessesTriviallyDisjoint(const MachineInstr &MIa,
  486. const MachineInstr &MIb) const override;
  487. /// GetInstSize - Return the number of bytes of code the specified
  488. /// instruction may be. This returns the maximum number of bytes.
  489. ///
  490. unsigned getInstSizeInBytes(const MachineInstr &MI) const override;
  491. MCInst getNop() const override;
  492. std::pair<unsigned, unsigned>
  493. decomposeMachineOperandsTargetFlags(unsigned TF) const override;
  494. ArrayRef<std::pair<unsigned, const char *>>
  495. getSerializableDirectMachineOperandTargetFlags() const override;
  496. ArrayRef<std::pair<unsigned, const char *>>
  497. getSerializableBitmaskMachineOperandTargetFlags() const override;
  498. // Expand VSX Memory Pseudo instruction to either a VSX or a FP instruction.
  499. bool expandVSXMemPseudo(MachineInstr &MI) const;
  500. // Lower pseudo instructions after register allocation.
  501. bool expandPostRAPseudo(MachineInstr &MI) const override;
  502. static bool isVFRegister(unsigned Reg) {
  503. return Reg >= PPC::VF0 && Reg <= PPC::VF31;
  504. }
  505. static bool isVRRegister(unsigned Reg) {
  506. return Reg >= PPC::V0 && Reg <= PPC::V31;
  507. }
  508. const TargetRegisterClass *updatedRC(const TargetRegisterClass *RC) const;
  509. static int getRecordFormOpcode(unsigned Opcode);
  510. bool isTOCSaveMI(const MachineInstr &MI) const;
  511. bool isSignOrZeroExtended(const MachineInstr &MI, bool SignExt,
  512. const unsigned PhiDepth) const;
  513. /// Return true if the output of the instruction is always a sign-extended,
  514. /// i.e. 0 to 31-th bits are same as 32-th bit.
  515. bool isSignExtended(const MachineInstr &MI, const unsigned depth = 0) const {
  516. return isSignOrZeroExtended(MI, true, depth);
  517. }
  518. /// Return true if the output of the instruction is always zero-extended,
  519. /// i.e. 0 to 31-th bits are all zeros
  520. bool isZeroExtended(const MachineInstr &MI, const unsigned depth = 0) const {
  521. return isSignOrZeroExtended(MI, false, depth);
  522. }
  523. bool convertToImmediateForm(MachineInstr &MI,
  524. MachineInstr **KilledDef = nullptr) const;
  525. bool foldFrameOffset(MachineInstr &MI) const;
  526. bool combineRLWINM(MachineInstr &MI, MachineInstr **ToErase = nullptr) const;
  527. bool isADDIInstrEligibleForFolding(MachineInstr &ADDIMI, int64_t &Imm) const;
  528. bool isADDInstrEligibleForFolding(MachineInstr &ADDMI) const;
  529. bool isImmInstrEligibleForFolding(MachineInstr &MI, unsigned &BaseReg,
  530. unsigned &XFormOpcode,
  531. int64_t &OffsetOfImmInstr,
  532. ImmInstrInfo &III) const;
  533. bool isValidToBeChangedReg(MachineInstr *ADDMI, unsigned Index,
  534. MachineInstr *&ADDIMI, int64_t &OffsetAddi,
  535. int64_t OffsetImm) const;
  536. /// Fixup killed/dead flag for register \p RegNo between instructions [\p
  537. /// StartMI, \p EndMI]. Some pre-RA or post-RA transformations may violate
  538. /// register killed/dead flags semantics, this function can be called to fix
  539. /// up. Before calling this function,
  540. /// 1. Ensure that \p RegNo liveness is killed after instruction \p EndMI.
  541. /// 2. Ensure that there is no new definition between (\p StartMI, \p EndMI)
  542. /// and possible definition for \p RegNo is \p StartMI or \p EndMI. For
  543. /// pre-RA cases, definition may be \p StartMI through COPY, \p StartMI
  544. /// will be adjust to true definition.
  545. /// 3. We can do accurate fixup for the case when all instructions between
  546. /// [\p StartMI, \p EndMI] are in same basic block.
  547. /// 4. For the case when \p StartMI and \p EndMI are not in same basic block,
  548. /// we conservatively clear kill flag for all uses of \p RegNo for pre-RA
  549. /// and for post-RA, we give an assertion as without reaching definition
  550. /// analysis post-RA, \p StartMI and \p EndMI are hard to keep right.
  551. void fixupIsDeadOrKill(MachineInstr *StartMI, MachineInstr *EndMI,
  552. unsigned RegNo) const;
  553. void replaceInstrWithLI(MachineInstr &MI, const LoadImmediateInfo &LII) const;
  554. void replaceInstrOperandWithImm(MachineInstr &MI, unsigned OpNo,
  555. int64_t Imm) const;
  556. bool instrHasImmForm(unsigned Opc, bool IsVFReg, ImmInstrInfo &III,
  557. bool PostRA) const;
  558. // In PostRA phase, try to find instruction defines \p Reg before \p MI.
  559. // \p SeenIntermediate is set to true if uses between DefMI and \p MI exist.
  560. MachineInstr *getDefMIPostRA(unsigned Reg, MachineInstr &MI,
  561. bool &SeenIntermediateUse) const;
  562. /// getRegNumForOperand - some operands use different numbering schemes
  563. /// for the same registers. For example, a VSX instruction may have any of
  564. /// vs0-vs63 allocated whereas an Altivec instruction could only have
  565. /// vs32-vs63 allocated (numbered as v0-v31). This function returns the actual
  566. /// register number needed for the opcode/operand number combination.
  567. /// The operand number argument will be useful when we need to extend this
  568. /// to instructions that use both Altivec and VSX numbering (for different
  569. /// operands).
  570. static unsigned getRegNumForOperand(const MCInstrDesc &Desc, unsigned Reg,
  571. unsigned OpNo) {
  572. int16_t regClass = Desc.OpInfo[OpNo].RegClass;
  573. switch (regClass) {
  574. // We store F0-F31, VF0-VF31 in MCOperand and it should be F0-F31,
  575. // VSX32-VSX63 during encoding/disassembling
  576. case PPC::VSSRCRegClassID:
  577. case PPC::VSFRCRegClassID:
  578. if (isVFRegister(Reg))
  579. return PPC::VSX32 + (Reg - PPC::VF0);
  580. break;
  581. // We store VSL0-VSL31, V0-V31 in MCOperand and it should be VSL0-VSL31,
  582. // VSX32-VSX63 during encoding/disassembling
  583. case PPC::VSRCRegClassID:
  584. if (isVRRegister(Reg))
  585. return PPC::VSX32 + (Reg - PPC::V0);
  586. break;
  587. // Other RegClass doesn't need mapping
  588. default:
  589. break;
  590. }
  591. return Reg;
  592. }
  593. /// Check \p Opcode is BDNZ (Decrement CTR and branch if it is still nonzero).
  594. bool isBDNZ(unsigned Opcode) const;
  595. /// Find the hardware loop instruction used to set-up the specified loop.
  596. /// On PPC, we have two instructions used to set-up the hardware loop
  597. /// (MTCTRloop, MTCTR8loop) with corresponding endloop (BDNZ, BDNZ8)
  598. /// instructions to indicate the end of a loop.
  599. MachineInstr *
  600. findLoopInstr(MachineBasicBlock &PreHeader,
  601. SmallPtrSet<MachineBasicBlock *, 8> &Visited) const;
  602. /// Analyze loop L, which must be a single-basic-block loop, and if the
  603. /// conditions can be understood enough produce a PipelinerLoopInfo object.
  604. std::unique_ptr<TargetInstrInfo::PipelinerLoopInfo>
  605. analyzeLoopForPipelining(MachineBasicBlock *LoopBB) const override;
  606. };
  607. }
  608. #endif