PPCInstrInfo.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. /// This instruction produced a sign extended result.
  61. SExt32To64 = 0x1 << (NewDef_Shift + 2),
  62. /// This instruction produced a zero extended result.
  63. ZExt32To64 = 0x1 << (NewDef_Shift + 3)
  64. };
  65. } // end namespace PPCII
  66. // Instructions that have an immediate form might be convertible to that
  67. // form if the correct input is a result of a load immediate. In order to
  68. // know whether the transformation is special, we might need to know some
  69. // of the details of the two forms.
  70. struct ImmInstrInfo {
  71. // Is the immediate field in the immediate form signed or unsigned?
  72. uint64_t SignedImm : 1;
  73. // Does the immediate need to be a multiple of some value?
  74. uint64_t ImmMustBeMultipleOf : 5;
  75. // Is R0/X0 treated specially by the original r+r instruction?
  76. // If so, in which operand?
  77. uint64_t ZeroIsSpecialOrig : 3;
  78. // Is R0/X0 treated specially by the new r+i instruction?
  79. // If so, in which operand?
  80. uint64_t ZeroIsSpecialNew : 3;
  81. // Is the operation commutative?
  82. uint64_t IsCommutative : 1;
  83. // The operand number to check for add-immediate def.
  84. uint64_t OpNoForForwarding : 3;
  85. // The operand number for the immediate.
  86. uint64_t ImmOpNo : 3;
  87. // The opcode of the new instruction.
  88. uint64_t ImmOpcode : 16;
  89. // The size of the immediate.
  90. uint64_t ImmWidth : 5;
  91. // The immediate should be truncated to N bits.
  92. uint64_t TruncateImmTo : 5;
  93. // Is the instruction summing the operand
  94. uint64_t IsSummingOperands : 1;
  95. };
  96. // Information required to convert an instruction to just a materialized
  97. // immediate.
  98. struct LoadImmediateInfo {
  99. unsigned Imm : 16;
  100. unsigned Is64Bit : 1;
  101. unsigned SetCR : 1;
  102. };
  103. // Index into the OpcodesForSpill array.
  104. enum SpillOpcodeKey {
  105. SOK_Int4Spill,
  106. SOK_Int8Spill,
  107. SOK_Float8Spill,
  108. SOK_Float4Spill,
  109. SOK_CRSpill,
  110. SOK_CRBitSpill,
  111. SOK_VRVectorSpill,
  112. SOK_VSXVectorSpill,
  113. SOK_VectorFloat8Spill,
  114. SOK_VectorFloat4Spill,
  115. SOK_SpillToVSR,
  116. SOK_PairedVecSpill,
  117. SOK_AccumulatorSpill,
  118. SOK_UAccumulatorSpill,
  119. SOK_WAccumulatorSpill,
  120. SOK_SPESpill,
  121. SOK_PairedG8Spill,
  122. SOK_LastOpcodeSpill // This must be last on the enum.
  123. };
  124. // Define list of load and store spill opcodes.
  125. #define NoInstr PPC::INSTRUCTION_LIST_END
  126. #define Pwr8LoadOpcodes \
  127. { \
  128. PPC::LWZ, PPC::LD, PPC::LFD, PPC::LFS, PPC::RESTORE_CR, \
  129. PPC::RESTORE_CRBIT, PPC::LVX, PPC::LXVD2X, PPC::LXSDX, PPC::LXSSPX, \
  130. PPC::SPILLTOVSR_LD, NoInstr, NoInstr, NoInstr, NoInstr, PPC::EVLDD, \
  131. PPC::RESTORE_QUADWORD \
  132. }
  133. #define Pwr9LoadOpcodes \
  134. { \
  135. PPC::LWZ, PPC::LD, PPC::LFD, PPC::LFS, PPC::RESTORE_CR, \
  136. PPC::RESTORE_CRBIT, PPC::LVX, PPC::LXV, PPC::DFLOADf64, \
  137. PPC::DFLOADf32, PPC::SPILLTOVSR_LD, NoInstr, NoInstr, NoInstr, \
  138. NoInstr, NoInstr, PPC::RESTORE_QUADWORD \
  139. }
  140. #define Pwr10LoadOpcodes \
  141. { \
  142. PPC::LWZ, PPC::LD, PPC::LFD, PPC::LFS, PPC::RESTORE_CR, \
  143. PPC::RESTORE_CRBIT, PPC::LVX, PPC::LXV, PPC::DFLOADf64, \
  144. PPC::DFLOADf32, PPC::SPILLTOVSR_LD, PPC::LXVP, PPC::RESTORE_ACC, \
  145. PPC::RESTORE_UACC, NoInstr, NoInstr, PPC::RESTORE_QUADWORD \
  146. }
  147. #define FutureLoadOpcodes \
  148. { \
  149. PPC::LWZ, PPC::LD, PPC::LFD, PPC::LFS, PPC::RESTORE_CR, \
  150. PPC::RESTORE_CRBIT, PPC::LVX, PPC::LXV, PPC::DFLOADf64, \
  151. PPC::DFLOADf32, PPC::SPILLTOVSR_LD, PPC::LXVP, PPC::RESTORE_ACC, \
  152. PPC::RESTORE_UACC, PPC::RESTORE_WACC, NoInstr, PPC::RESTORE_QUADWORD \
  153. }
  154. #define Pwr8StoreOpcodes \
  155. { \
  156. PPC::STW, PPC::STD, PPC::STFD, PPC::STFS, PPC::SPILL_CR, PPC::SPILL_CRBIT, \
  157. PPC::STVX, PPC::STXVD2X, PPC::STXSDX, PPC::STXSSPX, \
  158. PPC::SPILLTOVSR_ST, NoInstr, NoInstr, NoInstr, NoInstr, PPC::EVSTDD, \
  159. PPC::SPILL_QUADWORD \
  160. }
  161. #define Pwr9StoreOpcodes \
  162. { \
  163. PPC::STW, PPC::STD, PPC::STFD, PPC::STFS, PPC::SPILL_CR, PPC::SPILL_CRBIT, \
  164. PPC::STVX, PPC::STXV, PPC::DFSTOREf64, PPC::DFSTOREf32, \
  165. PPC::SPILLTOVSR_ST, NoInstr, NoInstr, NoInstr, NoInstr, NoInstr, \
  166. PPC::SPILL_QUADWORD \
  167. }
  168. #define Pwr10StoreOpcodes \
  169. { \
  170. PPC::STW, PPC::STD, PPC::STFD, PPC::STFS, PPC::SPILL_CR, PPC::SPILL_CRBIT, \
  171. PPC::STVX, PPC::STXV, PPC::DFSTOREf64, PPC::DFSTOREf32, \
  172. PPC::SPILLTOVSR_ST, PPC::STXVP, PPC::SPILL_ACC, PPC::SPILL_UACC, \
  173. NoInstr, NoInstr, PPC::SPILL_QUADWORD \
  174. }
  175. #define FutureStoreOpcodes \
  176. { \
  177. PPC::STW, PPC::STD, PPC::STFD, PPC::STFS, PPC::SPILL_CR, PPC::SPILL_CRBIT, \
  178. PPC::STVX, PPC::STXV, PPC::DFSTOREf64, PPC::DFSTOREf32, \
  179. PPC::SPILLTOVSR_ST, PPC::STXVP, PPC::SPILL_ACC, PPC::SPILL_UACC, \
  180. PPC::SPILL_WACC, NoInstr, PPC::SPILL_QUADWORD \
  181. }
  182. // Initialize arrays for load and store spill opcodes on supported subtargets.
  183. #define StoreOpcodesForSpill \
  184. { Pwr8StoreOpcodes, Pwr9StoreOpcodes, Pwr10StoreOpcodes, FutureStoreOpcodes }
  185. #define LoadOpcodesForSpill \
  186. { Pwr8LoadOpcodes, Pwr9LoadOpcodes, Pwr10LoadOpcodes, FutureLoadOpcodes }
  187. class PPCSubtarget;
  188. class PPCInstrInfo : public PPCGenInstrInfo {
  189. PPCSubtarget &Subtarget;
  190. const PPCRegisterInfo RI;
  191. const unsigned StoreSpillOpcodesArray[4][SOK_LastOpcodeSpill] =
  192. StoreOpcodesForSpill;
  193. const unsigned LoadSpillOpcodesArray[4][SOK_LastOpcodeSpill] =
  194. LoadOpcodesForSpill;
  195. void StoreRegToStackSlot(MachineFunction &MF, unsigned SrcReg, bool isKill,
  196. int FrameIdx, const TargetRegisterClass *RC,
  197. SmallVectorImpl<MachineInstr *> &NewMIs) const;
  198. void LoadRegFromStackSlot(MachineFunction &MF, const DebugLoc &DL,
  199. unsigned DestReg, int FrameIdx,
  200. const TargetRegisterClass *RC,
  201. SmallVectorImpl<MachineInstr *> &NewMIs) const;
  202. // Replace the instruction with single LI if possible. \p DefMI must be LI or
  203. // LI8.
  204. bool simplifyToLI(MachineInstr &MI, MachineInstr &DefMI,
  205. unsigned OpNoForForwarding, MachineInstr **KilledDef) const;
  206. // If the inst is imm-form and its register operand is produced by a ADDI, put
  207. // the imm into the inst directly and remove the ADDI if possible.
  208. bool transformToNewImmFormFedByAdd(MachineInstr &MI, MachineInstr &DefMI,
  209. unsigned OpNoForForwarding) const;
  210. // If the inst is x-form and has imm-form and one of its operand is produced
  211. // by a LI, put the imm into the inst directly and remove the LI if possible.
  212. bool transformToImmFormFedByLI(MachineInstr &MI, const ImmInstrInfo &III,
  213. unsigned ConstantOpNo,
  214. MachineInstr &DefMI) const;
  215. // If the inst is x-form and has imm-form and one of its operand is produced
  216. // by an add-immediate, try to transform it when possible.
  217. bool transformToImmFormFedByAdd(MachineInstr &MI, const ImmInstrInfo &III,
  218. unsigned ConstantOpNo, MachineInstr &DefMI,
  219. bool KillDefMI) const;
  220. // Try to find that, if the instruction 'MI' contains any operand that
  221. // could be forwarded from some inst that feeds it. If yes, return the
  222. // Def of that operand. And OpNoForForwarding is the operand index in
  223. // the 'MI' for that 'Def'. If we see another use of this Def between
  224. // the Def and the MI, SeenIntermediateUse becomes 'true'.
  225. MachineInstr *getForwardingDefMI(MachineInstr &MI,
  226. unsigned &OpNoForForwarding,
  227. bool &SeenIntermediateUse) const;
  228. // Can the user MI have it's source at index \p OpNoForForwarding
  229. // forwarded from an add-immediate that feeds it?
  230. bool isUseMIElgibleForForwarding(MachineInstr &MI, const ImmInstrInfo &III,
  231. unsigned OpNoForForwarding) const;
  232. bool isDefMIElgibleForForwarding(MachineInstr &DefMI,
  233. const ImmInstrInfo &III,
  234. MachineOperand *&ImmMO,
  235. MachineOperand *&RegMO) const;
  236. bool isImmElgibleForForwarding(const MachineOperand &ImmMO,
  237. const MachineInstr &DefMI,
  238. const ImmInstrInfo &III,
  239. int64_t &Imm,
  240. int64_t BaseImm = 0) const;
  241. bool isRegElgibleForForwarding(const MachineOperand &RegMO,
  242. const MachineInstr &DefMI,
  243. const MachineInstr &MI, bool KillDefMI,
  244. bool &IsFwdFeederRegKilled,
  245. bool &SeenIntermediateUse) const;
  246. unsigned getSpillTarget() const;
  247. ArrayRef<unsigned> getStoreOpcodesForSpillArray() const;
  248. ArrayRef<unsigned> getLoadOpcodesForSpillArray() const;
  249. unsigned getSpillIndex(const TargetRegisterClass *RC) const;
  250. int16_t getFMAOpIdxInfo(unsigned Opcode) const;
  251. void reassociateFMA(MachineInstr &Root, MachineCombinerPattern Pattern,
  252. SmallVectorImpl<MachineInstr *> &InsInstrs,
  253. SmallVectorImpl<MachineInstr *> &DelInstrs,
  254. DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const;
  255. bool isLoadFromConstantPool(MachineInstr *I) const;
  256. Register
  257. generateLoadForNewConst(unsigned Idx, MachineInstr *MI, Type *Ty,
  258. SmallVectorImpl<MachineInstr *> &InsInstrs) const;
  259. const Constant *getConstantFromConstantPool(MachineInstr *I) const;
  260. virtual void anchor();
  261. protected:
  262. /// Commutes the operands in the given instruction.
  263. /// The commutable operands are specified by their indices OpIdx1 and OpIdx2.
  264. ///
  265. /// Do not call this method for a non-commutable instruction or for
  266. /// non-commutable pair of operand indices OpIdx1 and OpIdx2.
  267. /// Even though the instruction is commutable, the method may still
  268. /// fail to commute the operands, null pointer is returned in such cases.
  269. ///
  270. /// For example, we can commute rlwimi instructions, but only if the
  271. /// rotate amt is zero. We also have to munge the immediates a bit.
  272. MachineInstr *commuteInstructionImpl(MachineInstr &MI, bool NewMI,
  273. unsigned OpIdx1,
  274. unsigned OpIdx2) const override;
  275. public:
  276. explicit PPCInstrInfo(PPCSubtarget &STI);
  277. /// getRegisterInfo - TargetInstrInfo is a superset of MRegister info. As
  278. /// such, whenever a client has an instance of instruction info, it should
  279. /// always be able to get register info as well (through this method).
  280. ///
  281. const PPCRegisterInfo &getRegisterInfo() const { return RI; }
  282. bool isXFormMemOp(unsigned Opcode) const {
  283. return get(Opcode).TSFlags & PPCII::XFormMemOp;
  284. }
  285. bool isPrefixed(unsigned Opcode) const {
  286. return get(Opcode).TSFlags & PPCII::Prefixed;
  287. }
  288. bool isSExt32To64(unsigned Opcode) const {
  289. return get(Opcode).TSFlags & PPCII::SExt32To64;
  290. }
  291. bool isZExt32To64(unsigned Opcode) const {
  292. return get(Opcode).TSFlags & PPCII::ZExt32To64;
  293. }
  294. /// Check if Opcode corresponds to a call instruction that should be marked
  295. /// with the NOTOC relocation.
  296. bool isNoTOCCallInstr(unsigned Opcode) const {
  297. if (!get(Opcode).isCall())
  298. return false;
  299. switch (Opcode) {
  300. default:
  301. #ifndef NDEBUG
  302. llvm_unreachable("Unknown call opcode");
  303. #endif
  304. return false;
  305. case PPC::BL8_NOTOC:
  306. case PPC::BL8_NOTOC_TLS:
  307. case PPC::BL8_NOTOC_RM:
  308. return true;
  309. #ifndef NDEBUG
  310. case PPC::BL8:
  311. case PPC::BL:
  312. case PPC::BL8_TLS:
  313. case PPC::BL_TLS:
  314. case PPC::BLA8:
  315. case PPC::BLA:
  316. case PPC::BCCL:
  317. case PPC::BCCLA:
  318. case PPC::BCL:
  319. case PPC::BCLn:
  320. case PPC::BL8_NOP:
  321. case PPC::BL_NOP:
  322. case PPC::BL8_NOP_TLS:
  323. case PPC::BLA8_NOP:
  324. case PPC::BCTRL8:
  325. case PPC::BCTRL:
  326. case PPC::BCCCTRL8:
  327. case PPC::BCCCTRL:
  328. case PPC::BCCTRL8:
  329. case PPC::BCCTRL:
  330. case PPC::BCCTRL8n:
  331. case PPC::BCCTRLn:
  332. case PPC::BL8_RM:
  333. case PPC::BLA8_RM:
  334. case PPC::BL8_NOP_RM:
  335. case PPC::BLA8_NOP_RM:
  336. case PPC::BCTRL8_RM:
  337. case PPC::BCTRL8_LDinto_toc:
  338. case PPC::BCTRL8_LDinto_toc_RM:
  339. case PPC::BL8_TLS_:
  340. case PPC::TCRETURNdi8:
  341. case PPC::TCRETURNai8:
  342. case PPC::TCRETURNri8:
  343. case PPC::TAILBCTR8:
  344. case PPC::TAILB8:
  345. case PPC::TAILBA8:
  346. case PPC::BCLalways:
  347. case PPC::BLRL:
  348. case PPC::BCCLRL:
  349. case PPC::BCLRL:
  350. case PPC::BCLRLn:
  351. case PPC::BDZL:
  352. case PPC::BDNZL:
  353. case PPC::BDZLA:
  354. case PPC::BDNZLA:
  355. case PPC::BDZLp:
  356. case PPC::BDNZLp:
  357. case PPC::BDZLAp:
  358. case PPC::BDNZLAp:
  359. case PPC::BDZLm:
  360. case PPC::BDNZLm:
  361. case PPC::BDZLAm:
  362. case PPC::BDNZLAm:
  363. case PPC::BDZLRL:
  364. case PPC::BDNZLRL:
  365. case PPC::BDZLRLp:
  366. case PPC::BDNZLRLp:
  367. case PPC::BDZLRLm:
  368. case PPC::BDNZLRLm:
  369. case PPC::BL_RM:
  370. case PPC::BLA_RM:
  371. case PPC::BL_NOP_RM:
  372. case PPC::BCTRL_RM:
  373. case PPC::TCRETURNdi:
  374. case PPC::TCRETURNai:
  375. case PPC::TCRETURNri:
  376. case PPC::BCTRL_LWZinto_toc:
  377. case PPC::BCTRL_LWZinto_toc_RM:
  378. case PPC::TAILBCTR:
  379. case PPC::TAILB:
  380. case PPC::TAILBA:
  381. return false;
  382. #endif
  383. }
  384. }
  385. static bool isSameClassPhysRegCopy(unsigned Opcode) {
  386. unsigned CopyOpcodes[] = {PPC::OR, PPC::OR8, PPC::FMR,
  387. PPC::VOR, PPC::XXLOR, PPC::XXLORf,
  388. PPC::XSCPSGNDP, PPC::MCRF, PPC::CROR,
  389. PPC::EVOR, -1U};
  390. for (int i = 0; CopyOpcodes[i] != -1U; i++)
  391. if (Opcode == CopyOpcodes[i])
  392. return true;
  393. return false;
  394. }
  395. ScheduleHazardRecognizer *
  396. CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
  397. const ScheduleDAG *DAG) const override;
  398. ScheduleHazardRecognizer *
  399. CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
  400. const ScheduleDAG *DAG) const override;
  401. unsigned getInstrLatency(const InstrItineraryData *ItinData,
  402. const MachineInstr &MI,
  403. unsigned *PredCost = nullptr) const override;
  404. int getOperandLatency(const InstrItineraryData *ItinData,
  405. const MachineInstr &DefMI, unsigned DefIdx,
  406. const MachineInstr &UseMI,
  407. unsigned UseIdx) const override;
  408. int getOperandLatency(const InstrItineraryData *ItinData,
  409. SDNode *DefNode, unsigned DefIdx,
  410. SDNode *UseNode, unsigned UseIdx) const override {
  411. return PPCGenInstrInfo::getOperandLatency(ItinData, DefNode, DefIdx,
  412. UseNode, UseIdx);
  413. }
  414. bool hasLowDefLatency(const TargetSchedModel &SchedModel,
  415. const MachineInstr &DefMI,
  416. unsigned DefIdx) const override {
  417. // Machine LICM should hoist all instructions in low-register-pressure
  418. // situations; none are sufficiently free to justify leaving in a loop
  419. // body.
  420. return false;
  421. }
  422. bool useMachineCombiner() const override {
  423. return true;
  424. }
  425. /// When getMachineCombinerPatterns() finds patterns, this function generates
  426. /// the instructions that could replace the original code sequence
  427. void genAlternativeCodeSequence(
  428. MachineInstr &Root, MachineCombinerPattern Pattern,
  429. SmallVectorImpl<MachineInstr *> &InsInstrs,
  430. SmallVectorImpl<MachineInstr *> &DelInstrs,
  431. DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const override;
  432. /// Return true when there is potentially a faster code sequence for a fma
  433. /// chain ending in \p Root. All potential patterns are output in the \p
  434. /// P array.
  435. bool getFMAPatterns(MachineInstr &Root,
  436. SmallVectorImpl<MachineCombinerPattern> &P,
  437. bool DoRegPressureReduce) const;
  438. /// Return true when there is potentially a faster code sequence
  439. /// for an instruction chain ending in <Root>. All potential patterns are
  440. /// output in the <Pattern> array.
  441. bool getMachineCombinerPatterns(MachineInstr &Root,
  442. SmallVectorImpl<MachineCombinerPattern> &P,
  443. bool DoRegPressureReduce) const override;
  444. /// On PowerPC, we leverage machine combiner pass to reduce register pressure
  445. /// when the register pressure is high for one BB.
  446. /// Return true if register pressure for \p MBB is high and ABI is supported
  447. /// to reduce register pressure. Otherwise return false.
  448. bool shouldReduceRegisterPressure(
  449. const MachineBasicBlock *MBB,
  450. const RegisterClassInfo *RegClassInfo) const override;
  451. /// Fixup the placeholders we put in genAlternativeCodeSequence() for
  452. /// MachineCombiner.
  453. void
  454. finalizeInsInstrs(MachineInstr &Root, MachineCombinerPattern &P,
  455. SmallVectorImpl<MachineInstr *> &InsInstrs) const override;
  456. bool isAssociativeAndCommutative(const MachineInstr &Inst,
  457. bool Invert) const override;
  458. /// On PowerPC, we try to reassociate FMA chain which will increase
  459. /// instruction size. Set extension resource length limit to 1 for edge case.
  460. /// Resource Length is calculated by scaled resource usage in getCycles().
  461. /// Because of the division in getCycles(), it returns different cycles due to
  462. /// legacy scaled resource usage. So new resource length may be same with
  463. /// legacy or 1 bigger than legacy.
  464. /// We need to execlude the 1 bigger case even the resource length is not
  465. /// perserved for more FMA chain reassociations on PowerPC.
  466. int getExtendResourceLenLimit() const override { return 1; }
  467. void setSpecialOperandAttr(MachineInstr &OldMI1, MachineInstr &OldMI2,
  468. MachineInstr &NewMI1,
  469. MachineInstr &NewMI2) const override;
  470. // PowerPC specific version of setSpecialOperandAttr that copies Flags to MI
  471. // and clears nuw, nsw, and exact flags.
  472. void setSpecialOperandAttr(MachineInstr &MI, uint16_t Flags) const;
  473. bool isCoalescableExtInstr(const MachineInstr &MI,
  474. Register &SrcReg, Register &DstReg,
  475. unsigned &SubIdx) const override;
  476. unsigned isLoadFromStackSlot(const MachineInstr &MI,
  477. int &FrameIndex) const override;
  478. bool isReallyTriviallyReMaterializable(const MachineInstr &MI) const override;
  479. unsigned isStoreToStackSlot(const MachineInstr &MI,
  480. int &FrameIndex) const override;
  481. bool findCommutedOpIndices(const MachineInstr &MI, unsigned &SrcOpIdx1,
  482. unsigned &SrcOpIdx2) const override;
  483. void insertNoop(MachineBasicBlock &MBB,
  484. MachineBasicBlock::iterator MI) const override;
  485. // Branch analysis.
  486. bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
  487. MachineBasicBlock *&FBB,
  488. SmallVectorImpl<MachineOperand> &Cond,
  489. bool AllowModify) const override;
  490. unsigned removeBranch(MachineBasicBlock &MBB,
  491. int *BytesRemoved = nullptr) const override;
  492. unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
  493. MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
  494. const DebugLoc &DL,
  495. int *BytesAdded = nullptr) const override;
  496. // Select analysis.
  497. bool canInsertSelect(const MachineBasicBlock &, ArrayRef<MachineOperand> Cond,
  498. Register, Register, Register, int &, int &,
  499. int &) const override;
  500. void insertSelect(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
  501. const DebugLoc &DL, Register DstReg,
  502. ArrayRef<MachineOperand> Cond, Register TrueReg,
  503. Register FalseReg) const override;
  504. void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
  505. const DebugLoc &DL, MCRegister DestReg, MCRegister SrcReg,
  506. bool KillSrc) const override;
  507. void storeRegToStackSlot(MachineBasicBlock &MBB,
  508. MachineBasicBlock::iterator MBBI, Register SrcReg,
  509. bool isKill, int FrameIndex,
  510. const TargetRegisterClass *RC,
  511. const TargetRegisterInfo *TRI,
  512. Register VReg) const override;
  513. // Emits a register spill without updating the register class for vector
  514. // registers. This ensures that when we spill a vector register the
  515. // element order in the register is the same as it was in memory.
  516. void storeRegToStackSlotNoUpd(MachineBasicBlock &MBB,
  517. MachineBasicBlock::iterator MBBI,
  518. unsigned SrcReg, bool isKill, int FrameIndex,
  519. const TargetRegisterClass *RC,
  520. const TargetRegisterInfo *TRI) const;
  521. void loadRegFromStackSlot(MachineBasicBlock &MBB,
  522. MachineBasicBlock::iterator MBBI, Register DestReg,
  523. int FrameIndex, const TargetRegisterClass *RC,
  524. const TargetRegisterInfo *TRI,
  525. Register VReg) const override;
  526. // Emits a register reload without updating the register class for vector
  527. // registers. This ensures that when we reload a vector register the
  528. // element order in the register is the same as it was in memory.
  529. void loadRegFromStackSlotNoUpd(MachineBasicBlock &MBB,
  530. MachineBasicBlock::iterator MBBI,
  531. unsigned DestReg, int FrameIndex,
  532. const TargetRegisterClass *RC,
  533. const TargetRegisterInfo *TRI) const;
  534. unsigned getStoreOpcodeForSpill(const TargetRegisterClass *RC) const;
  535. unsigned getLoadOpcodeForSpill(const TargetRegisterClass *RC) const;
  536. bool
  537. reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
  538. bool FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, Register Reg,
  539. MachineRegisterInfo *MRI) const override;
  540. bool onlyFoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
  541. Register Reg) const;
  542. // If conversion by predication (only supported by some branch instructions).
  543. // All of the profitability checks always return true; it is always
  544. // profitable to use the predicated branches.
  545. bool isProfitableToIfCvt(MachineBasicBlock &MBB,
  546. unsigned NumCycles, unsigned ExtraPredCycles,
  547. BranchProbability Probability) const override {
  548. return true;
  549. }
  550. bool isProfitableToIfCvt(MachineBasicBlock &TMBB,
  551. unsigned NumT, unsigned ExtraT,
  552. MachineBasicBlock &FMBB,
  553. unsigned NumF, unsigned ExtraF,
  554. BranchProbability Probability) const override;
  555. bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
  556. BranchProbability Probability) const override {
  557. return true;
  558. }
  559. bool isProfitableToUnpredicate(MachineBasicBlock &TMBB,
  560. MachineBasicBlock &FMBB) const override {
  561. return false;
  562. }
  563. // Predication support.
  564. bool isPredicated(const MachineInstr &MI) const override;
  565. bool isSchedulingBoundary(const MachineInstr &MI,
  566. const MachineBasicBlock *MBB,
  567. const MachineFunction &MF) const override;
  568. bool PredicateInstruction(MachineInstr &MI,
  569. ArrayRef<MachineOperand> Pred) const override;
  570. bool SubsumesPredicate(ArrayRef<MachineOperand> Pred1,
  571. ArrayRef<MachineOperand> Pred2) const override;
  572. bool ClobbersPredicate(MachineInstr &MI, std::vector<MachineOperand> &Pred,
  573. bool SkipDead) const override;
  574. // Comparison optimization.
  575. bool analyzeCompare(const MachineInstr &MI, Register &SrcReg,
  576. Register &SrcReg2, int64_t &Mask,
  577. int64_t &Value) const override;
  578. bool optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
  579. Register SrcReg2, int64_t Mask, int64_t Value,
  580. const MachineRegisterInfo *MRI) const override;
  581. /// Return true if get the base operand, byte offset of an instruction and
  582. /// the memory width. Width is the size of memory that is being
  583. /// loaded/stored (e.g. 1, 2, 4, 8).
  584. bool getMemOperandWithOffsetWidth(const MachineInstr &LdSt,
  585. const MachineOperand *&BaseOp,
  586. int64_t &Offset, unsigned &Width,
  587. const TargetRegisterInfo *TRI) const;
  588. bool optimizeCmpPostRA(MachineInstr &MI) const;
  589. /// Get the base operand and byte offset of an instruction that reads/writes
  590. /// memory.
  591. bool getMemOperandsWithOffsetWidth(
  592. const MachineInstr &LdSt,
  593. SmallVectorImpl<const MachineOperand *> &BaseOps, int64_t &Offset,
  594. bool &OffsetIsScalable, unsigned &Width,
  595. const TargetRegisterInfo *TRI) const override;
  596. /// Returns true if the two given memory operations should be scheduled
  597. /// adjacent.
  598. bool shouldClusterMemOps(ArrayRef<const MachineOperand *> BaseOps1,
  599. ArrayRef<const MachineOperand *> BaseOps2,
  600. unsigned NumLoads, unsigned NumBytes) const override;
  601. /// Return true if two MIs access different memory addresses and false
  602. /// otherwise
  603. bool
  604. areMemAccessesTriviallyDisjoint(const MachineInstr &MIa,
  605. const MachineInstr &MIb) const override;
  606. /// GetInstSize - Return the number of bytes of code the specified
  607. /// instruction may be. This returns the maximum number of bytes.
  608. ///
  609. unsigned getInstSizeInBytes(const MachineInstr &MI) const override;
  610. MCInst getNop() const override;
  611. std::pair<unsigned, unsigned>
  612. decomposeMachineOperandsTargetFlags(unsigned TF) const override;
  613. ArrayRef<std::pair<unsigned, const char *>>
  614. getSerializableDirectMachineOperandTargetFlags() const override;
  615. ArrayRef<std::pair<unsigned, const char *>>
  616. getSerializableBitmaskMachineOperandTargetFlags() const override;
  617. // Expand VSX Memory Pseudo instruction to either a VSX or a FP instruction.
  618. bool expandVSXMemPseudo(MachineInstr &MI) const;
  619. // Lower pseudo instructions after register allocation.
  620. bool expandPostRAPseudo(MachineInstr &MI) const override;
  621. static bool isVFRegister(unsigned Reg) {
  622. return Reg >= PPC::VF0 && Reg <= PPC::VF31;
  623. }
  624. static bool isVRRegister(unsigned Reg) {
  625. return Reg >= PPC::V0 && Reg <= PPC::V31;
  626. }
  627. const TargetRegisterClass *updatedRC(const TargetRegisterClass *RC) const;
  628. static int getRecordFormOpcode(unsigned Opcode);
  629. bool isTOCSaveMI(const MachineInstr &MI) const;
  630. std::pair<bool, bool>
  631. isSignOrZeroExtended(const unsigned Reg, const unsigned BinOpDepth,
  632. const MachineRegisterInfo *MRI) const;
  633. // Return true if the register is sign-extended from 32 to 64 bits.
  634. bool isSignExtended(const unsigned Reg,
  635. const MachineRegisterInfo *MRI) const {
  636. return isSignOrZeroExtended(Reg, 0, MRI).first;
  637. }
  638. // Return true if the register is zero-extended from 32 to 64 bits.
  639. bool isZeroExtended(const unsigned Reg,
  640. const MachineRegisterInfo *MRI) const {
  641. return isSignOrZeroExtended(Reg, 0, MRI).second;
  642. }
  643. bool convertToImmediateForm(MachineInstr &MI,
  644. MachineInstr **KilledDef = nullptr) const;
  645. bool foldFrameOffset(MachineInstr &MI) const;
  646. bool combineRLWINM(MachineInstr &MI, MachineInstr **ToErase = nullptr) const;
  647. bool isADDIInstrEligibleForFolding(MachineInstr &ADDIMI, int64_t &Imm) const;
  648. bool isADDInstrEligibleForFolding(MachineInstr &ADDMI) const;
  649. bool isImmInstrEligibleForFolding(MachineInstr &MI, unsigned &BaseReg,
  650. unsigned &XFormOpcode,
  651. int64_t &OffsetOfImmInstr,
  652. ImmInstrInfo &III) const;
  653. bool isValidToBeChangedReg(MachineInstr *ADDMI, unsigned Index,
  654. MachineInstr *&ADDIMI, int64_t &OffsetAddi,
  655. int64_t OffsetImm) const;
  656. /// Fixup killed/dead flag for register \p RegNo between instructions [\p
  657. /// StartMI, \p EndMI]. Some pre-RA or post-RA transformations may violate
  658. /// register killed/dead flags semantics, this function can be called to fix
  659. /// up. Before calling this function,
  660. /// 1. Ensure that \p RegNo liveness is killed after instruction \p EndMI.
  661. /// 2. Ensure that there is no new definition between (\p StartMI, \p EndMI)
  662. /// and possible definition for \p RegNo is \p StartMI or \p EndMI. For
  663. /// pre-RA cases, definition may be \p StartMI through COPY, \p StartMI
  664. /// will be adjust to true definition.
  665. /// 3. We can do accurate fixup for the case when all instructions between
  666. /// [\p StartMI, \p EndMI] are in same basic block.
  667. /// 4. For the case when \p StartMI and \p EndMI are not in same basic block,
  668. /// we conservatively clear kill flag for all uses of \p RegNo for pre-RA
  669. /// and for post-RA, we give an assertion as without reaching definition
  670. /// analysis post-RA, \p StartMI and \p EndMI are hard to keep right.
  671. void fixupIsDeadOrKill(MachineInstr *StartMI, MachineInstr *EndMI,
  672. unsigned RegNo) const;
  673. void replaceInstrWithLI(MachineInstr &MI, const LoadImmediateInfo &LII) const;
  674. void replaceInstrOperandWithImm(MachineInstr &MI, unsigned OpNo,
  675. int64_t Imm) const;
  676. bool instrHasImmForm(unsigned Opc, bool IsVFReg, ImmInstrInfo &III,
  677. bool PostRA) const;
  678. // In PostRA phase, try to find instruction defines \p Reg before \p MI.
  679. // \p SeenIntermediate is set to true if uses between DefMI and \p MI exist.
  680. MachineInstr *getDefMIPostRA(unsigned Reg, MachineInstr &MI,
  681. bool &SeenIntermediateUse) const;
  682. // Materialize immediate after RA.
  683. void materializeImmPostRA(MachineBasicBlock &MBB,
  684. MachineBasicBlock::iterator MBBI,
  685. const DebugLoc &DL, Register Reg,
  686. int64_t Imm) const;
  687. /// getRegNumForOperand - some operands use different numbering schemes
  688. /// for the same registers. For example, a VSX instruction may have any of
  689. /// vs0-vs63 allocated whereas an Altivec instruction could only have
  690. /// vs32-vs63 allocated (numbered as v0-v31). This function returns the actual
  691. /// register number needed for the opcode/operand number combination.
  692. /// The operand number argument will be useful when we need to extend this
  693. /// to instructions that use both Altivec and VSX numbering (for different
  694. /// operands).
  695. static unsigned getRegNumForOperand(const MCInstrDesc &Desc, unsigned Reg,
  696. unsigned OpNo) {
  697. int16_t regClass = Desc.operands()[OpNo].RegClass;
  698. switch (regClass) {
  699. // We store F0-F31, VF0-VF31 in MCOperand and it should be F0-F31,
  700. // VSX32-VSX63 during encoding/disassembling
  701. case PPC::VSSRCRegClassID:
  702. case PPC::VSFRCRegClassID:
  703. if (isVFRegister(Reg))
  704. return PPC::VSX32 + (Reg - PPC::VF0);
  705. break;
  706. // We store VSL0-VSL31, V0-V31 in MCOperand and it should be VSL0-VSL31,
  707. // VSX32-VSX63 during encoding/disassembling
  708. case PPC::VSRCRegClassID:
  709. if (isVRRegister(Reg))
  710. return PPC::VSX32 + (Reg - PPC::V0);
  711. break;
  712. // Other RegClass doesn't need mapping
  713. default:
  714. break;
  715. }
  716. return Reg;
  717. }
  718. /// Check \p Opcode is BDNZ (Decrement CTR and branch if it is still nonzero).
  719. bool isBDNZ(unsigned Opcode) const;
  720. /// Find the hardware loop instruction used to set-up the specified loop.
  721. /// On PPC, we have two instructions used to set-up the hardware loop
  722. /// (MTCTRloop, MTCTR8loop) with corresponding endloop (BDNZ, BDNZ8)
  723. /// instructions to indicate the end of a loop.
  724. MachineInstr *
  725. findLoopInstr(MachineBasicBlock &PreHeader,
  726. SmallPtrSet<MachineBasicBlock *, 8> &Visited) const;
  727. /// Analyze loop L, which must be a single-basic-block loop, and if the
  728. /// conditions can be understood enough produce a PipelinerLoopInfo object.
  729. std::unique_ptr<TargetInstrInfo::PipelinerLoopInfo>
  730. analyzeLoopForPipelining(MachineBasicBlock *LoopBB) const override;
  731. };
  732. }
  733. #endif