PPCInstrInfo.h 32 KB

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