PPCReduceCRLogicals.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. //===---- PPCReduceCRLogicals.cpp - Reduce CR Bit Logical operations ------===//
  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 pass aims to reduce the number of logical operations on bits in the CR
  10. // register. These instructions have a fairly high latency and only a single
  11. // pipeline at their disposal in modern PPC cores. Furthermore, they have a
  12. // tendency to occur in fairly small blocks where there's little opportunity
  13. // to hide the latency between the CR logical operation and its user.
  14. //
  15. //===---------------------------------------------------------------------===//
  16. #include "PPC.h"
  17. #include "PPCInstrInfo.h"
  18. #include "PPCTargetMachine.h"
  19. #include "llvm/ADT/Statistic.h"
  20. #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
  21. #include "llvm/CodeGen/MachineDominators.h"
  22. #include "llvm/CodeGen/MachineFunctionPass.h"
  23. #include "llvm/CodeGen/MachineInstrBuilder.h"
  24. #include "llvm/CodeGen/MachineRegisterInfo.h"
  25. #include "llvm/Config/llvm-config.h"
  26. #include "llvm/InitializePasses.h"
  27. #include "llvm/Support/Debug.h"
  28. using namespace llvm;
  29. #define DEBUG_TYPE "ppc-reduce-cr-ops"
  30. STATISTIC(NumContainedSingleUseBinOps,
  31. "Number of single-use binary CR logical ops contained in a block");
  32. STATISTIC(NumToSplitBlocks,
  33. "Number of binary CR logical ops that can be used to split blocks");
  34. STATISTIC(TotalCRLogicals, "Number of CR logical ops.");
  35. STATISTIC(TotalNullaryCRLogicals,
  36. "Number of nullary CR logical ops (CRSET/CRUNSET).");
  37. STATISTIC(TotalUnaryCRLogicals, "Number of unary CR logical ops.");
  38. STATISTIC(TotalBinaryCRLogicals, "Number of CR logical ops.");
  39. STATISTIC(NumBlocksSplitOnBinaryCROp,
  40. "Number of blocks split on CR binary logical ops.");
  41. STATISTIC(NumNotSplitIdenticalOperands,
  42. "Number of blocks not split due to operands being identical.");
  43. STATISTIC(NumNotSplitChainCopies,
  44. "Number of blocks not split due to operands being chained copies.");
  45. STATISTIC(NumNotSplitWrongOpcode,
  46. "Number of blocks not split due to the wrong opcode.");
  47. /// Given a basic block \p Successor that potentially contains PHIs, this
  48. /// function will look for any incoming values in the PHIs that are supposed to
  49. /// be coming from \p OrigMBB but whose definition is actually in \p NewMBB.
  50. /// Any such PHIs will be updated to reflect reality.
  51. static void updatePHIs(MachineBasicBlock *Successor, MachineBasicBlock *OrigMBB,
  52. MachineBasicBlock *NewMBB, MachineRegisterInfo *MRI) {
  53. for (auto &MI : Successor->instrs()) {
  54. if (!MI.isPHI())
  55. continue;
  56. // This is a really ugly-looking loop, but it was pillaged directly from
  57. // MachineBasicBlock::transferSuccessorsAndUpdatePHIs().
  58. for (unsigned i = 2, e = MI.getNumOperands() + 1; i != e; i += 2) {
  59. MachineOperand &MO = MI.getOperand(i);
  60. if (MO.getMBB() == OrigMBB) {
  61. // Check if the instruction is actually defined in NewMBB.
  62. if (MI.getOperand(i - 1).isReg()) {
  63. MachineInstr *DefMI = MRI->getVRegDef(MI.getOperand(i - 1).getReg());
  64. if (DefMI->getParent() == NewMBB ||
  65. !OrigMBB->isSuccessor(Successor)) {
  66. MO.setMBB(NewMBB);
  67. break;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. /// Given a basic block \p Successor that potentially contains PHIs, this
  75. /// function will look for PHIs that have an incoming value from \p OrigMBB
  76. /// and will add the same incoming value from \p NewMBB.
  77. /// NOTE: This should only be used if \p NewMBB is an immediate dominator of
  78. /// \p OrigMBB.
  79. static void addIncomingValuesToPHIs(MachineBasicBlock *Successor,
  80. MachineBasicBlock *OrigMBB,
  81. MachineBasicBlock *NewMBB,
  82. MachineRegisterInfo *MRI) {
  83. assert(OrigMBB->isSuccessor(NewMBB) &&
  84. "NewMBB must be a successor of OrigMBB");
  85. for (auto &MI : Successor->instrs()) {
  86. if (!MI.isPHI())
  87. continue;
  88. // This is a really ugly-looking loop, but it was pillaged directly from
  89. // MachineBasicBlock::transferSuccessorsAndUpdatePHIs().
  90. for (unsigned i = 2, e = MI.getNumOperands() + 1; i != e; i += 2) {
  91. MachineOperand &MO = MI.getOperand(i);
  92. if (MO.getMBB() == OrigMBB) {
  93. MachineInstrBuilder MIB(*MI.getParent()->getParent(), &MI);
  94. MIB.addReg(MI.getOperand(i - 1).getReg()).addMBB(NewMBB);
  95. break;
  96. }
  97. }
  98. }
  99. }
  100. struct BlockSplitInfo {
  101. MachineInstr *OrigBranch;
  102. MachineInstr *SplitBefore;
  103. MachineInstr *SplitCond;
  104. bool InvertNewBranch;
  105. bool InvertOrigBranch;
  106. bool BranchToFallThrough;
  107. const MachineBranchProbabilityInfo *MBPI;
  108. MachineInstr *MIToDelete;
  109. MachineInstr *NewCond;
  110. bool allInstrsInSameMBB() {
  111. if (!OrigBranch || !SplitBefore || !SplitCond)
  112. return false;
  113. MachineBasicBlock *MBB = OrigBranch->getParent();
  114. if (SplitBefore->getParent() != MBB || SplitCond->getParent() != MBB)
  115. return false;
  116. if (MIToDelete && MIToDelete->getParent() != MBB)
  117. return false;
  118. if (NewCond && NewCond->getParent() != MBB)
  119. return false;
  120. return true;
  121. }
  122. };
  123. /// Splits a MachineBasicBlock to branch before \p SplitBefore. The original
  124. /// branch is \p OrigBranch. The target of the new branch can either be the same
  125. /// as the target of the original branch or the fallthrough successor of the
  126. /// original block as determined by \p BranchToFallThrough. The branch
  127. /// conditions will be inverted according to \p InvertNewBranch and
  128. /// \p InvertOrigBranch. If an instruction that previously fed the branch is to
  129. /// be deleted, it is provided in \p MIToDelete and \p NewCond will be used as
  130. /// the branch condition. The branch probabilities will be set if the
  131. /// MachineBranchProbabilityInfo isn't null.
  132. static bool splitMBB(BlockSplitInfo &BSI) {
  133. assert(BSI.allInstrsInSameMBB() &&
  134. "All instructions must be in the same block.");
  135. MachineBasicBlock *ThisMBB = BSI.OrigBranch->getParent();
  136. MachineFunction *MF = ThisMBB->getParent();
  137. MachineRegisterInfo *MRI = &MF->getRegInfo();
  138. assert(MRI->isSSA() && "Can only do this while the function is in SSA form.");
  139. if (ThisMBB->succ_size() != 2) {
  140. LLVM_DEBUG(
  141. dbgs() << "Don't know how to handle blocks that don't have exactly"
  142. << " two successors.\n");
  143. return false;
  144. }
  145. const PPCInstrInfo *TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo();
  146. unsigned OrigBROpcode = BSI.OrigBranch->getOpcode();
  147. unsigned InvertedOpcode =
  148. OrigBROpcode == PPC::BC
  149. ? PPC::BCn
  150. : OrigBROpcode == PPC::BCn
  151. ? PPC::BC
  152. : OrigBROpcode == PPC::BCLR ? PPC::BCLRn : PPC::BCLR;
  153. unsigned NewBROpcode = BSI.InvertNewBranch ? InvertedOpcode : OrigBROpcode;
  154. MachineBasicBlock *OrigTarget = BSI.OrigBranch->getOperand(1).getMBB();
  155. MachineBasicBlock *OrigFallThrough = OrigTarget == *ThisMBB->succ_begin()
  156. ? *ThisMBB->succ_rbegin()
  157. : *ThisMBB->succ_begin();
  158. MachineBasicBlock *NewBRTarget =
  159. BSI.BranchToFallThrough ? OrigFallThrough : OrigTarget;
  160. // It's impossible to know the precise branch probability after the split.
  161. // But it still needs to be reasonable, the whole probability to original
  162. // targets should not be changed.
  163. // After split NewBRTarget will get two incoming edges. Assume P0 is the
  164. // original branch probability to NewBRTarget, P1 and P2 are new branch
  165. // probabilies to NewBRTarget after split. If the two edge frequencies are
  166. // same, then
  167. // F * P1 = F * P0 / 2 ==> P1 = P0 / 2
  168. // F * (1 - P1) * P2 = F * P1 ==> P2 = P1 / (1 - P1)
  169. BranchProbability ProbToNewTarget, ProbFallThrough; // Prob for new Br.
  170. BranchProbability ProbOrigTarget, ProbOrigFallThrough; // Prob for orig Br.
  171. ProbToNewTarget = ProbFallThrough = BranchProbability::getUnknown();
  172. ProbOrigTarget = ProbOrigFallThrough = BranchProbability::getUnknown();
  173. if (BSI.MBPI) {
  174. if (BSI.BranchToFallThrough) {
  175. ProbToNewTarget = BSI.MBPI->getEdgeProbability(ThisMBB, OrigFallThrough) / 2;
  176. ProbFallThrough = ProbToNewTarget.getCompl();
  177. ProbOrigFallThrough = ProbToNewTarget / ProbToNewTarget.getCompl();
  178. ProbOrigTarget = ProbOrigFallThrough.getCompl();
  179. } else {
  180. ProbToNewTarget = BSI.MBPI->getEdgeProbability(ThisMBB, OrigTarget) / 2;
  181. ProbFallThrough = ProbToNewTarget.getCompl();
  182. ProbOrigTarget = ProbToNewTarget / ProbToNewTarget.getCompl();
  183. ProbOrigFallThrough = ProbOrigTarget.getCompl();
  184. }
  185. }
  186. // Create a new basic block.
  187. MachineBasicBlock::iterator InsertPoint = BSI.SplitBefore;
  188. const BasicBlock *LLVM_BB = ThisMBB->getBasicBlock();
  189. MachineFunction::iterator It = ThisMBB->getIterator();
  190. MachineBasicBlock *NewMBB = MF->CreateMachineBasicBlock(LLVM_BB);
  191. MF->insert(++It, NewMBB);
  192. // Move everything after SplitBefore into the new block.
  193. NewMBB->splice(NewMBB->end(), ThisMBB, InsertPoint, ThisMBB->end());
  194. NewMBB->transferSuccessors(ThisMBB);
  195. if (!ProbOrigTarget.isUnknown()) {
  196. auto MBBI = find(NewMBB->successors(), OrigTarget);
  197. NewMBB->setSuccProbability(MBBI, ProbOrigTarget);
  198. MBBI = find(NewMBB->successors(), OrigFallThrough);
  199. NewMBB->setSuccProbability(MBBI, ProbOrigFallThrough);
  200. }
  201. // Add the two successors to ThisMBB.
  202. ThisMBB->addSuccessor(NewBRTarget, ProbToNewTarget);
  203. ThisMBB->addSuccessor(NewMBB, ProbFallThrough);
  204. // Add the branches to ThisMBB.
  205. BuildMI(*ThisMBB, ThisMBB->end(), BSI.SplitBefore->getDebugLoc(),
  206. TII->get(NewBROpcode))
  207. .addReg(BSI.SplitCond->getOperand(0).getReg())
  208. .addMBB(NewBRTarget);
  209. BuildMI(*ThisMBB, ThisMBB->end(), BSI.SplitBefore->getDebugLoc(),
  210. TII->get(PPC::B))
  211. .addMBB(NewMBB);
  212. if (BSI.MIToDelete)
  213. BSI.MIToDelete->eraseFromParent();
  214. // Change the condition on the original branch and invert it if requested.
  215. auto FirstTerminator = NewMBB->getFirstTerminator();
  216. if (BSI.NewCond) {
  217. assert(FirstTerminator->getOperand(0).isReg() &&
  218. "Can't update condition of unconditional branch.");
  219. FirstTerminator->getOperand(0).setReg(BSI.NewCond->getOperand(0).getReg());
  220. }
  221. if (BSI.InvertOrigBranch)
  222. FirstTerminator->setDesc(TII->get(InvertedOpcode));
  223. // If any of the PHIs in the successors of NewMBB reference values that
  224. // now come from NewMBB, they need to be updated.
  225. for (auto *Succ : NewMBB->successors()) {
  226. updatePHIs(Succ, ThisMBB, NewMBB, MRI);
  227. }
  228. addIncomingValuesToPHIs(NewBRTarget, ThisMBB, NewMBB, MRI);
  229. LLVM_DEBUG(dbgs() << "After splitting, ThisMBB:\n"; ThisMBB->dump());
  230. LLVM_DEBUG(dbgs() << "NewMBB:\n"; NewMBB->dump());
  231. LLVM_DEBUG(dbgs() << "New branch-to block:\n"; NewBRTarget->dump());
  232. return true;
  233. }
  234. static bool isBinary(MachineInstr &MI) {
  235. return MI.getNumOperands() == 3;
  236. }
  237. static bool isNullary(MachineInstr &MI) {
  238. return MI.getNumOperands() == 1;
  239. }
  240. /// Given a CR logical operation \p CROp, branch opcode \p BROp as well as
  241. /// a flag to indicate if the first operand of \p CROp is used as the
  242. /// SplitBefore operand, determines whether either of the branches are to be
  243. /// inverted as well as whether the new target should be the original
  244. /// fall-through block.
  245. static void
  246. computeBranchTargetAndInversion(unsigned CROp, unsigned BROp, bool UsingDef1,
  247. bool &InvertNewBranch, bool &InvertOrigBranch,
  248. bool &TargetIsFallThrough) {
  249. // The conditions under which each of the output operands should be [un]set
  250. // can certainly be written much more concisely with just 3 if statements or
  251. // ternary expressions. However, this provides a much clearer overview to the
  252. // reader as to what is set for each <CROp, BROp, OpUsed> combination.
  253. if (BROp == PPC::BC || BROp == PPC::BCLR) {
  254. // Regular branches.
  255. switch (CROp) {
  256. default:
  257. llvm_unreachable("Don't know how to handle this CR logical.");
  258. case PPC::CROR:
  259. InvertNewBranch = false;
  260. InvertOrigBranch = false;
  261. TargetIsFallThrough = false;
  262. return;
  263. case PPC::CRAND:
  264. InvertNewBranch = true;
  265. InvertOrigBranch = false;
  266. TargetIsFallThrough = true;
  267. return;
  268. case PPC::CRNAND:
  269. InvertNewBranch = true;
  270. InvertOrigBranch = true;
  271. TargetIsFallThrough = false;
  272. return;
  273. case PPC::CRNOR:
  274. InvertNewBranch = false;
  275. InvertOrigBranch = true;
  276. TargetIsFallThrough = true;
  277. return;
  278. case PPC::CRORC:
  279. InvertNewBranch = UsingDef1;
  280. InvertOrigBranch = !UsingDef1;
  281. TargetIsFallThrough = false;
  282. return;
  283. case PPC::CRANDC:
  284. InvertNewBranch = !UsingDef1;
  285. InvertOrigBranch = !UsingDef1;
  286. TargetIsFallThrough = true;
  287. return;
  288. }
  289. } else if (BROp == PPC::BCn || BROp == PPC::BCLRn) {
  290. // Negated branches.
  291. switch (CROp) {
  292. default:
  293. llvm_unreachable("Don't know how to handle this CR logical.");
  294. case PPC::CROR:
  295. InvertNewBranch = true;
  296. InvertOrigBranch = false;
  297. TargetIsFallThrough = true;
  298. return;
  299. case PPC::CRAND:
  300. InvertNewBranch = false;
  301. InvertOrigBranch = false;
  302. TargetIsFallThrough = false;
  303. return;
  304. case PPC::CRNAND:
  305. InvertNewBranch = false;
  306. InvertOrigBranch = true;
  307. TargetIsFallThrough = true;
  308. return;
  309. case PPC::CRNOR:
  310. InvertNewBranch = true;
  311. InvertOrigBranch = true;
  312. TargetIsFallThrough = false;
  313. return;
  314. case PPC::CRORC:
  315. InvertNewBranch = !UsingDef1;
  316. InvertOrigBranch = !UsingDef1;
  317. TargetIsFallThrough = true;
  318. return;
  319. case PPC::CRANDC:
  320. InvertNewBranch = UsingDef1;
  321. InvertOrigBranch = !UsingDef1;
  322. TargetIsFallThrough = false;
  323. return;
  324. }
  325. } else
  326. llvm_unreachable("Don't know how to handle this branch.");
  327. }
  328. namespace {
  329. class PPCReduceCRLogicals : public MachineFunctionPass {
  330. public:
  331. static char ID;
  332. struct CRLogicalOpInfo {
  333. MachineInstr *MI;
  334. // FIXME: If chains of copies are to be handled, this should be a vector.
  335. std::pair<MachineInstr*, MachineInstr*> CopyDefs;
  336. std::pair<MachineInstr*, MachineInstr*> TrueDefs;
  337. unsigned IsBinary : 1;
  338. unsigned IsNullary : 1;
  339. unsigned ContainedInBlock : 1;
  340. unsigned FeedsISEL : 1;
  341. unsigned FeedsBR : 1;
  342. unsigned FeedsLogical : 1;
  343. unsigned SingleUse : 1;
  344. unsigned DefsSingleUse : 1;
  345. unsigned SubregDef1;
  346. unsigned SubregDef2;
  347. CRLogicalOpInfo() : MI(nullptr), IsBinary(0), IsNullary(0),
  348. ContainedInBlock(0), FeedsISEL(0), FeedsBR(0),
  349. FeedsLogical(0), SingleUse(0), DefsSingleUse(1),
  350. SubregDef1(0), SubregDef2(0) { }
  351. void dump();
  352. };
  353. private:
  354. const PPCInstrInfo *TII = nullptr;
  355. MachineFunction *MF = nullptr;
  356. MachineRegisterInfo *MRI = nullptr;
  357. const MachineBranchProbabilityInfo *MBPI = nullptr;
  358. // A vector to contain all the CR logical operations
  359. SmallVector<CRLogicalOpInfo, 16> AllCRLogicalOps;
  360. void initialize(MachineFunction &MFParm);
  361. void collectCRLogicals();
  362. bool handleCROp(unsigned Idx);
  363. bool splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI);
  364. static bool isCRLogical(MachineInstr &MI) {
  365. unsigned Opc = MI.getOpcode();
  366. return Opc == PPC::CRAND || Opc == PPC::CRNAND || Opc == PPC::CROR ||
  367. Opc == PPC::CRXOR || Opc == PPC::CRNOR || Opc == PPC::CREQV ||
  368. Opc == PPC::CRANDC || Opc == PPC::CRORC || Opc == PPC::CRSET ||
  369. Opc == PPC::CRUNSET || Opc == PPC::CR6SET || Opc == PPC::CR6UNSET;
  370. }
  371. bool simplifyCode() {
  372. bool Changed = false;
  373. // Not using a range-based for loop here as the vector may grow while being
  374. // operated on.
  375. for (unsigned i = 0; i < AllCRLogicalOps.size(); i++)
  376. Changed |= handleCROp(i);
  377. return Changed;
  378. }
  379. public:
  380. PPCReduceCRLogicals() : MachineFunctionPass(ID) {
  381. initializePPCReduceCRLogicalsPass(*PassRegistry::getPassRegistry());
  382. }
  383. MachineInstr *lookThroughCRCopy(unsigned Reg, unsigned &Subreg,
  384. MachineInstr *&CpDef);
  385. bool runOnMachineFunction(MachineFunction &MF) override {
  386. if (skipFunction(MF.getFunction()))
  387. return false;
  388. // If the subtarget doesn't use CR bits, there's nothing to do.
  389. const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
  390. if (!STI.useCRBits())
  391. return false;
  392. initialize(MF);
  393. collectCRLogicals();
  394. return simplifyCode();
  395. }
  396. CRLogicalOpInfo createCRLogicalOpInfo(MachineInstr &MI);
  397. void getAnalysisUsage(AnalysisUsage &AU) const override {
  398. AU.addRequired<MachineBranchProbabilityInfo>();
  399. AU.addRequired<MachineDominatorTree>();
  400. MachineFunctionPass::getAnalysisUsage(AU);
  401. }
  402. };
  403. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  404. LLVM_DUMP_METHOD void PPCReduceCRLogicals::CRLogicalOpInfo::dump() {
  405. dbgs() << "CRLogicalOpMI: ";
  406. MI->dump();
  407. dbgs() << "IsBinary: " << IsBinary << ", FeedsISEL: " << FeedsISEL;
  408. dbgs() << ", FeedsBR: " << FeedsBR << ", FeedsLogical: ";
  409. dbgs() << FeedsLogical << ", SingleUse: " << SingleUse;
  410. dbgs() << ", DefsSingleUse: " << DefsSingleUse;
  411. dbgs() << ", SubregDef1: " << SubregDef1 << ", SubregDef2: ";
  412. dbgs() << SubregDef2 << ", ContainedInBlock: " << ContainedInBlock;
  413. if (!IsNullary) {
  414. dbgs() << "\nDefs:\n";
  415. TrueDefs.first->dump();
  416. }
  417. if (IsBinary)
  418. TrueDefs.second->dump();
  419. dbgs() << "\n";
  420. if (CopyDefs.first) {
  421. dbgs() << "CopyDef1: ";
  422. CopyDefs.first->dump();
  423. }
  424. if (CopyDefs.second) {
  425. dbgs() << "CopyDef2: ";
  426. CopyDefs.second->dump();
  427. }
  428. }
  429. #endif
  430. PPCReduceCRLogicals::CRLogicalOpInfo
  431. PPCReduceCRLogicals::createCRLogicalOpInfo(MachineInstr &MIParam) {
  432. CRLogicalOpInfo Ret;
  433. Ret.MI = &MIParam;
  434. // Get the defs
  435. if (isNullary(MIParam)) {
  436. Ret.IsNullary = 1;
  437. Ret.TrueDefs = std::make_pair(nullptr, nullptr);
  438. Ret.CopyDefs = std::make_pair(nullptr, nullptr);
  439. } else {
  440. MachineInstr *Def1 = lookThroughCRCopy(MIParam.getOperand(1).getReg(),
  441. Ret.SubregDef1, Ret.CopyDefs.first);
  442. assert(Def1 && "Must be able to find a definition of operand 1.");
  443. Ret.DefsSingleUse &=
  444. MRI->hasOneNonDBGUse(Def1->getOperand(0).getReg());
  445. Ret.DefsSingleUse &=
  446. MRI->hasOneNonDBGUse(Ret.CopyDefs.first->getOperand(0).getReg());
  447. if (isBinary(MIParam)) {
  448. Ret.IsBinary = 1;
  449. MachineInstr *Def2 = lookThroughCRCopy(MIParam.getOperand(2).getReg(),
  450. Ret.SubregDef2,
  451. Ret.CopyDefs.second);
  452. assert(Def2 && "Must be able to find a definition of operand 2.");
  453. Ret.DefsSingleUse &=
  454. MRI->hasOneNonDBGUse(Def2->getOperand(0).getReg());
  455. Ret.DefsSingleUse &=
  456. MRI->hasOneNonDBGUse(Ret.CopyDefs.second->getOperand(0).getReg());
  457. Ret.TrueDefs = std::make_pair(Def1, Def2);
  458. } else {
  459. Ret.TrueDefs = std::make_pair(Def1, nullptr);
  460. Ret.CopyDefs.second = nullptr;
  461. }
  462. }
  463. Ret.ContainedInBlock = 1;
  464. // Get the uses
  465. for (MachineInstr &UseMI :
  466. MRI->use_nodbg_instructions(MIParam.getOperand(0).getReg())) {
  467. unsigned Opc = UseMI.getOpcode();
  468. if (Opc == PPC::ISEL || Opc == PPC::ISEL8)
  469. Ret.FeedsISEL = 1;
  470. if (Opc == PPC::BC || Opc == PPC::BCn || Opc == PPC::BCLR ||
  471. Opc == PPC::BCLRn)
  472. Ret.FeedsBR = 1;
  473. Ret.FeedsLogical = isCRLogical(UseMI);
  474. if (UseMI.getParent() != MIParam.getParent())
  475. Ret.ContainedInBlock = 0;
  476. }
  477. Ret.SingleUse = MRI->hasOneNonDBGUse(MIParam.getOperand(0).getReg()) ? 1 : 0;
  478. // We now know whether all the uses of the CR logical are in the same block.
  479. if (!Ret.IsNullary) {
  480. Ret.ContainedInBlock &=
  481. (MIParam.getParent() == Ret.TrueDefs.first->getParent());
  482. if (Ret.IsBinary)
  483. Ret.ContainedInBlock &=
  484. (MIParam.getParent() == Ret.TrueDefs.second->getParent());
  485. }
  486. LLVM_DEBUG(Ret.dump());
  487. if (Ret.IsBinary && Ret.ContainedInBlock && Ret.SingleUse) {
  488. NumContainedSingleUseBinOps++;
  489. if (Ret.FeedsBR && Ret.DefsSingleUse)
  490. NumToSplitBlocks++;
  491. }
  492. return Ret;
  493. }
  494. /// Looks through a COPY instruction to the actual definition of the CR-bit
  495. /// register and returns the instruction that defines it.
  496. /// FIXME: This currently handles what is by-far the most common case:
  497. /// an instruction that defines a CR field followed by a single copy of a bit
  498. /// from that field into a virtual register. If chains of copies need to be
  499. /// handled, this should have a loop until a non-copy instruction is found.
  500. MachineInstr *PPCReduceCRLogicals::lookThroughCRCopy(unsigned Reg,
  501. unsigned &Subreg,
  502. MachineInstr *&CpDef) {
  503. Subreg = -1;
  504. if (!Register::isVirtualRegister(Reg))
  505. return nullptr;
  506. MachineInstr *Copy = MRI->getVRegDef(Reg);
  507. CpDef = Copy;
  508. if (!Copy->isCopy())
  509. return Copy;
  510. Register CopySrc = Copy->getOperand(1).getReg();
  511. Subreg = Copy->getOperand(1).getSubReg();
  512. if (!Register::isVirtualRegister(CopySrc)) {
  513. const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
  514. // Set the Subreg
  515. if (CopySrc == PPC::CR0EQ || CopySrc == PPC::CR6EQ)
  516. Subreg = PPC::sub_eq;
  517. if (CopySrc == PPC::CR0LT || CopySrc == PPC::CR6LT)
  518. Subreg = PPC::sub_lt;
  519. if (CopySrc == PPC::CR0GT || CopySrc == PPC::CR6GT)
  520. Subreg = PPC::sub_gt;
  521. if (CopySrc == PPC::CR0UN || CopySrc == PPC::CR6UN)
  522. Subreg = PPC::sub_un;
  523. // Loop backwards and return the first MI that modifies the physical CR Reg.
  524. MachineBasicBlock::iterator Me = Copy, B = Copy->getParent()->begin();
  525. while (Me != B)
  526. if ((--Me)->modifiesRegister(CopySrc, TRI))
  527. return &*Me;
  528. return nullptr;
  529. }
  530. return MRI->getVRegDef(CopySrc);
  531. }
  532. void PPCReduceCRLogicals::initialize(MachineFunction &MFParam) {
  533. MF = &MFParam;
  534. MRI = &MF->getRegInfo();
  535. TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo();
  536. MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
  537. AllCRLogicalOps.clear();
  538. }
  539. /// Contains all the implemented transformations on CR logical operations.
  540. /// For example, a binary CR logical can be used to split a block on its inputs,
  541. /// a unary CR logical might be used to change the condition code on a
  542. /// comparison feeding it. A nullary CR logical might simply be removable
  543. /// if the user of the bit it [un]sets can be transformed.
  544. bool PPCReduceCRLogicals::handleCROp(unsigned Idx) {
  545. // We can definitely split a block on the inputs to a binary CR operation
  546. // whose defs and (single) use are within the same block.
  547. bool Changed = false;
  548. CRLogicalOpInfo CRI = AllCRLogicalOps[Idx];
  549. if (CRI.IsBinary && CRI.ContainedInBlock && CRI.SingleUse && CRI.FeedsBR &&
  550. CRI.DefsSingleUse) {
  551. Changed = splitBlockOnBinaryCROp(CRI);
  552. if (Changed)
  553. NumBlocksSplitOnBinaryCROp++;
  554. }
  555. return Changed;
  556. }
  557. /// Splits a block that contains a CR-logical operation that feeds a branch
  558. /// and whose operands are produced within the block.
  559. /// Example:
  560. /// %vr5<def> = CMPDI %vr2, 0; CRRC:%vr5 G8RC:%vr2
  561. /// %vr6<def> = COPY %vr5:sub_eq; CRBITRC:%vr6 CRRC:%vr5
  562. /// %vr7<def> = CMPDI %vr3, 0; CRRC:%vr7 G8RC:%vr3
  563. /// %vr8<def> = COPY %vr7:sub_eq; CRBITRC:%vr8 CRRC:%vr7
  564. /// %vr9<def> = CROR %vr6<kill>, %vr8<kill>; CRBITRC:%vr9,%vr6,%vr8
  565. /// BC %vr9<kill>, <BB#2>; CRBITRC:%vr9
  566. /// Becomes:
  567. /// %vr5<def> = CMPDI %vr2, 0; CRRC:%vr5 G8RC:%vr2
  568. /// %vr6<def> = COPY %vr5:sub_eq; CRBITRC:%vr6 CRRC:%vr5
  569. /// BC %vr6<kill>, <BB#2>; CRBITRC:%vr6
  570. ///
  571. /// %vr7<def> = CMPDI %vr3, 0; CRRC:%vr7 G8RC:%vr3
  572. /// %vr8<def> = COPY %vr7:sub_eq; CRBITRC:%vr8 CRRC:%vr7
  573. /// BC %vr9<kill>, <BB#2>; CRBITRC:%vr9
  574. bool PPCReduceCRLogicals::splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI) {
  575. if (CRI.CopyDefs.first == CRI.CopyDefs.second) {
  576. LLVM_DEBUG(dbgs() << "Unable to split as the two operands are the same\n");
  577. NumNotSplitIdenticalOperands++;
  578. return false;
  579. }
  580. if (CRI.TrueDefs.first->isCopy() || CRI.TrueDefs.second->isCopy() ||
  581. CRI.TrueDefs.first->isPHI() || CRI.TrueDefs.second->isPHI()) {
  582. LLVM_DEBUG(
  583. dbgs() << "Unable to split because one of the operands is a PHI or "
  584. "chain of copies.\n");
  585. NumNotSplitChainCopies++;
  586. return false;
  587. }
  588. // Note: keep in sync with computeBranchTargetAndInversion().
  589. if (CRI.MI->getOpcode() != PPC::CROR &&
  590. CRI.MI->getOpcode() != PPC::CRAND &&
  591. CRI.MI->getOpcode() != PPC::CRNOR &&
  592. CRI.MI->getOpcode() != PPC::CRNAND &&
  593. CRI.MI->getOpcode() != PPC::CRORC &&
  594. CRI.MI->getOpcode() != PPC::CRANDC) {
  595. LLVM_DEBUG(dbgs() << "Unable to split blocks on this opcode.\n");
  596. NumNotSplitWrongOpcode++;
  597. return false;
  598. }
  599. LLVM_DEBUG(dbgs() << "Splitting the following CR op:\n"; CRI.dump());
  600. MachineBasicBlock::iterator Def1It = CRI.TrueDefs.first;
  601. MachineBasicBlock::iterator Def2It = CRI.TrueDefs.second;
  602. bool UsingDef1 = false;
  603. MachineInstr *SplitBefore = &*Def2It;
  604. for (auto E = CRI.MI->getParent()->end(); Def2It != E; ++Def2It) {
  605. if (Def1It == Def2It) { // Def2 comes before Def1.
  606. SplitBefore = &*Def1It;
  607. UsingDef1 = true;
  608. break;
  609. }
  610. }
  611. LLVM_DEBUG(dbgs() << "We will split the following block:\n";);
  612. LLVM_DEBUG(CRI.MI->getParent()->dump());
  613. LLVM_DEBUG(dbgs() << "Before instruction:\n"; SplitBefore->dump());
  614. // Get the branch instruction.
  615. MachineInstr *Branch =
  616. MRI->use_nodbg_begin(CRI.MI->getOperand(0).getReg())->getParent();
  617. // We want the new block to have no code in it other than the definition
  618. // of the input to the CR logical and the CR logical itself. So we move
  619. // those to the bottom of the block (just before the branch). Then we
  620. // will split before the CR logical.
  621. MachineBasicBlock *MBB = SplitBefore->getParent();
  622. auto FirstTerminator = MBB->getFirstTerminator();
  623. MachineBasicBlock::iterator FirstInstrToMove =
  624. UsingDef1 ? CRI.TrueDefs.first : CRI.TrueDefs.second;
  625. MachineBasicBlock::iterator SecondInstrToMove =
  626. UsingDef1 ? CRI.CopyDefs.first : CRI.CopyDefs.second;
  627. // The instructions that need to be moved are not guaranteed to be
  628. // contiguous. Move them individually.
  629. // FIXME: If one of the operands is a chain of (single use) copies, they
  630. // can all be moved and we can still split.
  631. MBB->splice(FirstTerminator, MBB, FirstInstrToMove);
  632. if (FirstInstrToMove != SecondInstrToMove)
  633. MBB->splice(FirstTerminator, MBB, SecondInstrToMove);
  634. MBB->splice(FirstTerminator, MBB, CRI.MI);
  635. unsigned Opc = CRI.MI->getOpcode();
  636. bool InvertOrigBranch, InvertNewBranch, TargetIsFallThrough;
  637. computeBranchTargetAndInversion(Opc, Branch->getOpcode(), UsingDef1,
  638. InvertNewBranch, InvertOrigBranch,
  639. TargetIsFallThrough);
  640. MachineInstr *SplitCond =
  641. UsingDef1 ? CRI.CopyDefs.second : CRI.CopyDefs.first;
  642. LLVM_DEBUG(dbgs() << "We will " << (InvertNewBranch ? "invert" : "copy"));
  643. LLVM_DEBUG(dbgs() << " the original branch and the target is the "
  644. << (TargetIsFallThrough ? "fallthrough block\n"
  645. : "orig. target block\n"));
  646. LLVM_DEBUG(dbgs() << "Original branch instruction: "; Branch->dump());
  647. BlockSplitInfo BSI { Branch, SplitBefore, SplitCond, InvertNewBranch,
  648. InvertOrigBranch, TargetIsFallThrough, MBPI, CRI.MI,
  649. UsingDef1 ? CRI.CopyDefs.first : CRI.CopyDefs.second };
  650. bool Changed = splitMBB(BSI);
  651. // If we've split on a CR logical that is fed by a CR logical,
  652. // recompute the source CR logical as it may be usable for splitting.
  653. if (Changed) {
  654. bool Input1CRlogical =
  655. CRI.TrueDefs.first && isCRLogical(*CRI.TrueDefs.first);
  656. bool Input2CRlogical =
  657. CRI.TrueDefs.second && isCRLogical(*CRI.TrueDefs.second);
  658. if (Input1CRlogical)
  659. AllCRLogicalOps.push_back(createCRLogicalOpInfo(*CRI.TrueDefs.first));
  660. if (Input2CRlogical)
  661. AllCRLogicalOps.push_back(createCRLogicalOpInfo(*CRI.TrueDefs.second));
  662. }
  663. return Changed;
  664. }
  665. void PPCReduceCRLogicals::collectCRLogicals() {
  666. for (MachineBasicBlock &MBB : *MF) {
  667. for (MachineInstr &MI : MBB) {
  668. if (isCRLogical(MI)) {
  669. AllCRLogicalOps.push_back(createCRLogicalOpInfo(MI));
  670. TotalCRLogicals++;
  671. if (AllCRLogicalOps.back().IsNullary)
  672. TotalNullaryCRLogicals++;
  673. else if (AllCRLogicalOps.back().IsBinary)
  674. TotalBinaryCRLogicals++;
  675. else
  676. TotalUnaryCRLogicals++;
  677. }
  678. }
  679. }
  680. }
  681. } // end anonymous namespace
  682. INITIALIZE_PASS_BEGIN(PPCReduceCRLogicals, DEBUG_TYPE,
  683. "PowerPC Reduce CR logical Operation", false, false)
  684. INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
  685. INITIALIZE_PASS_END(PPCReduceCRLogicals, DEBUG_TYPE,
  686. "PowerPC Reduce CR logical Operation", false, false)
  687. char PPCReduceCRLogicals::ID = 0;
  688. FunctionPass*
  689. llvm::createPPCReduceCRLogicalsPass() { return new PPCReduceCRLogicals(); }