PPCBranchCoalescing.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. //===-- CoalesceBranches.cpp - Coalesce blocks with the same condition ---===//
  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. /// \file
  10. /// Coalesce basic blocks guarded by the same branch condition into a single
  11. /// basic block.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "PPC.h"
  15. #include "llvm/ADT/BitVector.h"
  16. #include "llvm/ADT/Statistic.h"
  17. #include "llvm/CodeGen/MachineDominators.h"
  18. #include "llvm/CodeGen/MachineFunctionPass.h"
  19. #include "llvm/CodeGen/MachinePostDominators.h"
  20. #include "llvm/CodeGen/MachineRegisterInfo.h"
  21. #include "llvm/CodeGen/Passes.h"
  22. #include "llvm/CodeGen/TargetFrameLowering.h"
  23. #include "llvm/CodeGen/TargetInstrInfo.h"
  24. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  25. #include "llvm/InitializePasses.h"
  26. #include "llvm/Support/Debug.h"
  27. using namespace llvm;
  28. #define DEBUG_TYPE "ppc-branch-coalescing"
  29. STATISTIC(NumBlocksCoalesced, "Number of blocks coalesced");
  30. STATISTIC(NumPHINotMoved, "Number of PHI Nodes that cannot be merged");
  31. STATISTIC(NumBlocksNotCoalesced, "Number of blocks not coalesced");
  32. //===----------------------------------------------------------------------===//
  33. // PPCBranchCoalescing
  34. //===----------------------------------------------------------------------===//
  35. ///
  36. /// Improve scheduling by coalescing branches that depend on the same condition.
  37. /// This pass looks for blocks that are guarded by the same branch condition
  38. /// and attempts to merge the blocks together. Such opportunities arise from
  39. /// the expansion of select statements in the IR.
  40. ///
  41. /// This pass does not handle implicit operands on branch statements. In order
  42. /// to run on targets that use implicit operands, changes need to be made in the
  43. /// canCoalesceBranch and canMerge methods.
  44. ///
  45. /// Example: the following LLVM IR
  46. ///
  47. /// %test = icmp eq i32 %x 0
  48. /// %tmp1 = select i1 %test, double %a, double 2.000000e-03
  49. /// %tmp2 = select i1 %test, double %b, double 5.000000e-03
  50. ///
  51. /// expands to the following machine code:
  52. ///
  53. /// %bb.0: derived from LLVM BB %entry
  54. /// liveins: %f1 %f3 %x6
  55. /// <SNIP1>
  56. /// %0 = COPY %f1; F8RC:%0
  57. /// %5 = CMPLWI killed %4, 0; CRRC:%5 GPRC:%4
  58. /// %8 = LXSDX %zero8, killed %7, implicit %rm;
  59. /// mem:LD8[ConstantPool] F8RC:%8 G8RC:%7
  60. /// BCC 76, %5, <%bb.2>; CRRC:%5
  61. /// Successors according to CFG: %bb.1(?%) %bb.2(?%)
  62. ///
  63. /// %bb.1: derived from LLVM BB %entry
  64. /// Predecessors according to CFG: %bb.0
  65. /// Successors according to CFG: %bb.2(?%)
  66. ///
  67. /// %bb.2: derived from LLVM BB %entry
  68. /// Predecessors according to CFG: %bb.0 %bb.1
  69. /// %9 = PHI %8, <%bb.1>, %0, <%bb.0>;
  70. /// F8RC:%9,%8,%0
  71. /// <SNIP2>
  72. /// BCC 76, %5, <%bb.4>; CRRC:%5
  73. /// Successors according to CFG: %bb.3(?%) %bb.4(?%)
  74. ///
  75. /// %bb.3: derived from LLVM BB %entry
  76. /// Predecessors according to CFG: %bb.2
  77. /// Successors according to CFG: %bb.4(?%)
  78. ///
  79. /// %bb.4: derived from LLVM BB %entry
  80. /// Predecessors according to CFG: %bb.2 %bb.3
  81. /// %13 = PHI %12, <%bb.3>, %2, <%bb.2>;
  82. /// F8RC:%13,%12,%2
  83. /// <SNIP3>
  84. /// BLR8 implicit %lr8, implicit %rm, implicit %f1
  85. ///
  86. /// When this pattern is detected, branch coalescing will try to collapse
  87. /// it by moving code in %bb.2 to %bb.0 and/or %bb.4 and removing %bb.3.
  88. ///
  89. /// If all conditions are meet, IR should collapse to:
  90. ///
  91. /// %bb.0: derived from LLVM BB %entry
  92. /// liveins: %f1 %f3 %x6
  93. /// <SNIP1>
  94. /// %0 = COPY %f1; F8RC:%0
  95. /// %5 = CMPLWI killed %4, 0; CRRC:%5 GPRC:%4
  96. /// %8 = LXSDX %zero8, killed %7, implicit %rm;
  97. /// mem:LD8[ConstantPool] F8RC:%8 G8RC:%7
  98. /// <SNIP2>
  99. /// BCC 76, %5, <%bb.4>; CRRC:%5
  100. /// Successors according to CFG: %bb.1(0x2aaaaaaa / 0x80000000 = 33.33%)
  101. /// %bb.4(0x55555554 / 0x80000000 = 66.67%)
  102. ///
  103. /// %bb.1: derived from LLVM BB %entry
  104. /// Predecessors according to CFG: %bb.0
  105. /// Successors according to CFG: %bb.4(0x40000000 / 0x80000000 = 50.00%)
  106. ///
  107. /// %bb.4: derived from LLVM BB %entry
  108. /// Predecessors according to CFG: %bb.0 %bb.1
  109. /// %9 = PHI %8, <%bb.1>, %0, <%bb.0>;
  110. /// F8RC:%9,%8,%0
  111. /// %13 = PHI %12, <%bb.1>, %2, <%bb.0>;
  112. /// F8RC:%13,%12,%2
  113. /// <SNIP3>
  114. /// BLR8 implicit %lr8, implicit %rm, implicit %f1
  115. ///
  116. /// Branch Coalescing does not split blocks, it moves everything in the same
  117. /// direction ensuring it does not break use/definition semantics.
  118. ///
  119. /// PHI nodes and its corresponding use instructions are moved to its successor
  120. /// block if there are no uses within the successor block PHI nodes. PHI
  121. /// node ordering cannot be assumed.
  122. ///
  123. /// Non-PHI can be moved up to the predecessor basic block or down to the
  124. /// successor basic block following any PHI instructions. Whether it moves
  125. /// up or down depends on whether the register(s) defined in the instructions
  126. /// are used in current block or in any PHI instructions at the beginning of
  127. /// the successor block.
  128. namespace {
  129. class PPCBranchCoalescing : public MachineFunctionPass {
  130. struct CoalescingCandidateInfo {
  131. MachineBasicBlock *BranchBlock; // Block containing the branch
  132. MachineBasicBlock *BranchTargetBlock; // Block branched to
  133. MachineBasicBlock *FallThroughBlock; // Fall-through if branch not taken
  134. SmallVector<MachineOperand, 4> Cond;
  135. bool MustMoveDown;
  136. bool MustMoveUp;
  137. CoalescingCandidateInfo();
  138. void clear();
  139. };
  140. MachineDominatorTree *MDT;
  141. MachinePostDominatorTree *MPDT;
  142. const TargetInstrInfo *TII;
  143. MachineRegisterInfo *MRI;
  144. void initialize(MachineFunction &F);
  145. bool canCoalesceBranch(CoalescingCandidateInfo &Cand);
  146. bool identicalOperands(ArrayRef<MachineOperand> OperandList1,
  147. ArrayRef<MachineOperand> OperandList2) const;
  148. bool validateCandidates(CoalescingCandidateInfo &SourceRegion,
  149. CoalescingCandidateInfo &TargetRegion) const;
  150. public:
  151. static char ID;
  152. PPCBranchCoalescing() : MachineFunctionPass(ID) {
  153. initializePPCBranchCoalescingPass(*PassRegistry::getPassRegistry());
  154. }
  155. void getAnalysisUsage(AnalysisUsage &AU) const override {
  156. AU.addRequired<MachineDominatorTree>();
  157. AU.addRequired<MachinePostDominatorTree>();
  158. MachineFunctionPass::getAnalysisUsage(AU);
  159. }
  160. StringRef getPassName() const override { return "Branch Coalescing"; }
  161. bool mergeCandidates(CoalescingCandidateInfo &SourceRegion,
  162. CoalescingCandidateInfo &TargetRegion);
  163. bool canMoveToBeginning(const MachineInstr &MI,
  164. const MachineBasicBlock &MBB) const;
  165. bool canMoveToEnd(const MachineInstr &MI,
  166. const MachineBasicBlock &MBB) const;
  167. bool canMerge(CoalescingCandidateInfo &SourceRegion,
  168. CoalescingCandidateInfo &TargetRegion) const;
  169. void moveAndUpdatePHIs(MachineBasicBlock *SourceRegionMBB,
  170. MachineBasicBlock *TargetRegionMBB);
  171. bool runOnMachineFunction(MachineFunction &MF) override;
  172. };
  173. } // End anonymous namespace.
  174. char PPCBranchCoalescing::ID = 0;
  175. /// createPPCBranchCoalescingPass - returns an instance of the Branch Coalescing
  176. /// Pass
  177. FunctionPass *llvm::createPPCBranchCoalescingPass() {
  178. return new PPCBranchCoalescing();
  179. }
  180. INITIALIZE_PASS_BEGIN(PPCBranchCoalescing, DEBUG_TYPE,
  181. "Branch Coalescing", false, false)
  182. INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
  183. INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
  184. INITIALIZE_PASS_END(PPCBranchCoalescing, DEBUG_TYPE, "Branch Coalescing",
  185. false, false)
  186. PPCBranchCoalescing::CoalescingCandidateInfo::CoalescingCandidateInfo()
  187. : BranchBlock(nullptr), BranchTargetBlock(nullptr),
  188. FallThroughBlock(nullptr), MustMoveDown(false), MustMoveUp(false) {}
  189. void PPCBranchCoalescing::CoalescingCandidateInfo::clear() {
  190. BranchBlock = nullptr;
  191. BranchTargetBlock = nullptr;
  192. FallThroughBlock = nullptr;
  193. Cond.clear();
  194. MustMoveDown = false;
  195. MustMoveUp = false;
  196. }
  197. void PPCBranchCoalescing::initialize(MachineFunction &MF) {
  198. MDT = &getAnalysis<MachineDominatorTree>();
  199. MPDT = &getAnalysis<MachinePostDominatorTree>();
  200. TII = MF.getSubtarget().getInstrInfo();
  201. MRI = &MF.getRegInfo();
  202. }
  203. ///
  204. /// Analyze the branch statement to determine if it can be coalesced. This
  205. /// method analyses the branch statement for the given candidate to determine
  206. /// if it can be coalesced. If the branch can be coalesced, then the
  207. /// BranchTargetBlock and the FallThroughBlock are recorded in the specified
  208. /// Candidate.
  209. ///
  210. ///\param[in,out] Cand The coalescing candidate to analyze
  211. ///\return true if and only if the branch can be coalesced, false otherwise
  212. ///
  213. bool PPCBranchCoalescing::canCoalesceBranch(CoalescingCandidateInfo &Cand) {
  214. LLVM_DEBUG(dbgs() << "Determine if branch block "
  215. << Cand.BranchBlock->getNumber() << " can be coalesced:");
  216. MachineBasicBlock *FalseMBB = nullptr;
  217. if (TII->analyzeBranch(*Cand.BranchBlock, Cand.BranchTargetBlock, FalseMBB,
  218. Cand.Cond)) {
  219. LLVM_DEBUG(dbgs() << "TII unable to Analyze Branch - skip\n");
  220. return false;
  221. }
  222. for (auto &I : Cand.BranchBlock->terminators()) {
  223. LLVM_DEBUG(dbgs() << "Looking at terminator : " << I << "\n");
  224. if (!I.isBranch())
  225. continue;
  226. // The analyzeBranch method does not include any implicit operands.
  227. // This is not an issue on PPC but must be handled on other targets.
  228. // For this pass to be made target-independent, the analyzeBranch API
  229. // need to be updated to support implicit operands and there would
  230. // need to be a way to verify that any implicit operands would not be
  231. // clobbered by merging blocks. This would include identifying the
  232. // implicit operands as well as the basic block they are defined in.
  233. // This could be done by changing the analyzeBranch API to have it also
  234. // record and return the implicit operands and the blocks where they are
  235. // defined. Alternatively, the BranchCoalescing code would need to be
  236. // extended to identify the implicit operands. The analysis in canMerge
  237. // must then be extended to prove that none of the implicit operands are
  238. // changed in the blocks that are combined during coalescing.
  239. if (I.getNumOperands() != I.getNumExplicitOperands()) {
  240. LLVM_DEBUG(dbgs() << "Terminator contains implicit operands - skip : "
  241. << I << "\n");
  242. return false;
  243. }
  244. }
  245. if (Cand.BranchBlock->isEHPad() || Cand.BranchBlock->hasEHPadSuccessor()) {
  246. LLVM_DEBUG(dbgs() << "EH Pad - skip\n");
  247. return false;
  248. }
  249. if (Cand.BranchBlock->mayHaveInlineAsmBr()) {
  250. LLVM_DEBUG(dbgs() << "Inline Asm Br - skip\n");
  251. return false;
  252. }
  253. // For now only consider triangles (i.e, BranchTargetBlock is set,
  254. // FalseMBB is null, and BranchTargetBlock is a successor to BranchBlock)
  255. if (!Cand.BranchTargetBlock || FalseMBB ||
  256. !Cand.BranchBlock->isSuccessor(Cand.BranchTargetBlock)) {
  257. LLVM_DEBUG(dbgs() << "Does not form a triangle - skip\n");
  258. return false;
  259. }
  260. // Ensure there are only two successors
  261. if (Cand.BranchBlock->succ_size() != 2) {
  262. LLVM_DEBUG(dbgs() << "Does not have 2 successors - skip\n");
  263. return false;
  264. }
  265. // The block must be able to fall through.
  266. assert(Cand.BranchBlock->canFallThrough() &&
  267. "Expecting the block to fall through!");
  268. // We have already ensured there are exactly two successors to
  269. // BranchBlock and that BranchTargetBlock is a successor to BranchBlock.
  270. // Ensure the single fall though block is empty.
  271. MachineBasicBlock *Succ =
  272. (*Cand.BranchBlock->succ_begin() == Cand.BranchTargetBlock)
  273. ? *Cand.BranchBlock->succ_rbegin()
  274. : *Cand.BranchBlock->succ_begin();
  275. assert(Succ && "Expecting a valid fall-through block\n");
  276. if (!Succ->empty()) {
  277. LLVM_DEBUG(dbgs() << "Fall-through block contains code -- skip\n");
  278. return false;
  279. }
  280. if (!Succ->isSuccessor(Cand.BranchTargetBlock)) {
  281. LLVM_DEBUG(
  282. dbgs()
  283. << "Successor of fall through block is not branch taken block\n");
  284. return false;
  285. }
  286. Cand.FallThroughBlock = Succ;
  287. LLVM_DEBUG(dbgs() << "Valid Candidate\n");
  288. return true;
  289. }
  290. ///
  291. /// Determine if the two operand lists are identical
  292. ///
  293. /// \param[in] OpList1 operand list
  294. /// \param[in] OpList2 operand list
  295. /// \return true if and only if the operands lists are identical
  296. ///
  297. bool PPCBranchCoalescing::identicalOperands(
  298. ArrayRef<MachineOperand> OpList1, ArrayRef<MachineOperand> OpList2) const {
  299. if (OpList1.size() != OpList2.size()) {
  300. LLVM_DEBUG(dbgs() << "Operand list is different size\n");
  301. return false;
  302. }
  303. for (unsigned i = 0; i < OpList1.size(); ++i) {
  304. const MachineOperand &Op1 = OpList1[i];
  305. const MachineOperand &Op2 = OpList2[i];
  306. LLVM_DEBUG(dbgs() << "Op1: " << Op1 << "\n"
  307. << "Op2: " << Op2 << "\n");
  308. if (Op1.isIdenticalTo(Op2)) {
  309. // filter out instructions with physical-register uses
  310. if (Op1.isReg() &&
  311. Register::isPhysicalRegister(Op1.getReg())
  312. // If the physical register is constant then we can assume the value
  313. // has not changed between uses.
  314. && !(Op1.isUse() && MRI->isConstantPhysReg(Op1.getReg()))) {
  315. LLVM_DEBUG(dbgs() << "The operands are not provably identical.\n");
  316. return false;
  317. }
  318. LLVM_DEBUG(dbgs() << "Op1 and Op2 are identical!\n");
  319. continue;
  320. }
  321. // If the operands are not identical, but are registers, check to see if the
  322. // definition of the register produces the same value. If they produce the
  323. // same value, consider them to be identical.
  324. if (Op1.isReg() && Op2.isReg() &&
  325. Register::isVirtualRegister(Op1.getReg()) &&
  326. Register::isVirtualRegister(Op2.getReg())) {
  327. MachineInstr *Op1Def = MRI->getVRegDef(Op1.getReg());
  328. MachineInstr *Op2Def = MRI->getVRegDef(Op2.getReg());
  329. if (TII->produceSameValue(*Op1Def, *Op2Def, MRI)) {
  330. LLVM_DEBUG(dbgs() << "Op1Def: " << *Op1Def << " and " << *Op2Def
  331. << " produce the same value!\n");
  332. } else {
  333. LLVM_DEBUG(dbgs() << "Operands produce different values\n");
  334. return false;
  335. }
  336. } else {
  337. LLVM_DEBUG(dbgs() << "The operands are not provably identical.\n");
  338. return false;
  339. }
  340. }
  341. return true;
  342. }
  343. ///
  344. /// Moves ALL PHI instructions in SourceMBB to beginning of TargetMBB
  345. /// and update them to refer to the new block. PHI node ordering
  346. /// cannot be assumed so it does not matter where the PHI instructions
  347. /// are moved to in TargetMBB.
  348. ///
  349. /// \param[in] SourceMBB block to move PHI instructions from
  350. /// \param[in] TargetMBB block to move PHI instructions to
  351. ///
  352. void PPCBranchCoalescing::moveAndUpdatePHIs(MachineBasicBlock *SourceMBB,
  353. MachineBasicBlock *TargetMBB) {
  354. MachineBasicBlock::iterator MI = SourceMBB->begin();
  355. MachineBasicBlock::iterator ME = SourceMBB->getFirstNonPHI();
  356. if (MI == ME) {
  357. LLVM_DEBUG(dbgs() << "SourceMBB contains no PHI instructions.\n");
  358. return;
  359. }
  360. // Update all PHI instructions in SourceMBB and move to top of TargetMBB
  361. for (MachineBasicBlock::iterator Iter = MI; Iter != ME; Iter++) {
  362. MachineInstr &PHIInst = *Iter;
  363. for (unsigned i = 2, e = PHIInst.getNumOperands() + 1; i != e; i += 2) {
  364. MachineOperand &MO = PHIInst.getOperand(i);
  365. if (MO.getMBB() == SourceMBB)
  366. MO.setMBB(TargetMBB);
  367. }
  368. }
  369. TargetMBB->splice(TargetMBB->begin(), SourceMBB, MI, ME);
  370. }
  371. ///
  372. /// This function checks if MI can be moved to the beginning of the TargetMBB
  373. /// following PHI instructions. A MI instruction can be moved to beginning of
  374. /// the TargetMBB if there are no uses of it within the TargetMBB PHI nodes.
  375. ///
  376. /// \param[in] MI the machine instruction to move.
  377. /// \param[in] TargetMBB the machine basic block to move to
  378. /// \return true if it is safe to move MI to beginning of TargetMBB,
  379. /// false otherwise.
  380. ///
  381. bool PPCBranchCoalescing::canMoveToBeginning(const MachineInstr &MI,
  382. const MachineBasicBlock &TargetMBB
  383. ) const {
  384. LLVM_DEBUG(dbgs() << "Checking if " << MI << " can move to beginning of "
  385. << TargetMBB.getNumber() << "\n");
  386. for (auto &Def : MI.defs()) { // Looking at Def
  387. for (auto &Use : MRI->use_instructions(Def.getReg())) {
  388. if (Use.isPHI() && Use.getParent() == &TargetMBB) {
  389. LLVM_DEBUG(dbgs() << " *** used in a PHI -- cannot move ***\n");
  390. return false;
  391. }
  392. }
  393. }
  394. LLVM_DEBUG(dbgs() << " Safe to move to the beginning.\n");
  395. return true;
  396. }
  397. ///
  398. /// This function checks if MI can be moved to the end of the TargetMBB,
  399. /// immediately before the first terminator. A MI instruction can be moved
  400. /// to then end of the TargetMBB if no PHI node defines what MI uses within
  401. /// it's own MBB.
  402. ///
  403. /// \param[in] MI the machine instruction to move.
  404. /// \param[in] TargetMBB the machine basic block to move to
  405. /// \return true if it is safe to move MI to end of TargetMBB,
  406. /// false otherwise.
  407. ///
  408. bool PPCBranchCoalescing::canMoveToEnd(const MachineInstr &MI,
  409. const MachineBasicBlock &TargetMBB
  410. ) const {
  411. LLVM_DEBUG(dbgs() << "Checking if " << MI << " can move to end of "
  412. << TargetMBB.getNumber() << "\n");
  413. for (auto &Use : MI.uses()) {
  414. if (Use.isReg() && Register::isVirtualRegister(Use.getReg())) {
  415. MachineInstr *DefInst = MRI->getVRegDef(Use.getReg());
  416. if (DefInst->isPHI() && DefInst->getParent() == MI.getParent()) {
  417. LLVM_DEBUG(dbgs() << " *** Cannot move this instruction ***\n");
  418. return false;
  419. } else {
  420. LLVM_DEBUG(
  421. dbgs() << " *** def is in another block -- safe to move!\n");
  422. }
  423. }
  424. }
  425. LLVM_DEBUG(dbgs() << " Safe to move to the end.\n");
  426. return true;
  427. }
  428. ///
  429. /// This method checks to ensure the two coalescing candidates follows the
  430. /// expected pattern required for coalescing.
  431. ///
  432. /// \param[in] SourceRegion The candidate to move statements from
  433. /// \param[in] TargetRegion The candidate to move statements to
  434. /// \return true if all instructions in SourceRegion.BranchBlock can be merged
  435. /// into a block in TargetRegion; false otherwise.
  436. ///
  437. bool PPCBranchCoalescing::validateCandidates(
  438. CoalescingCandidateInfo &SourceRegion,
  439. CoalescingCandidateInfo &TargetRegion) const {
  440. if (TargetRegion.BranchTargetBlock != SourceRegion.BranchBlock)
  441. llvm_unreachable("Expecting SourceRegion to immediately follow TargetRegion");
  442. else if (!MDT->dominates(TargetRegion.BranchBlock, SourceRegion.BranchBlock))
  443. llvm_unreachable("Expecting TargetRegion to dominate SourceRegion");
  444. else if (!MPDT->dominates(SourceRegion.BranchBlock, TargetRegion.BranchBlock))
  445. llvm_unreachable("Expecting SourceRegion to post-dominate TargetRegion");
  446. else if (!TargetRegion.FallThroughBlock->empty() ||
  447. !SourceRegion.FallThroughBlock->empty())
  448. llvm_unreachable("Expecting fall-through blocks to be empty");
  449. return true;
  450. }
  451. ///
  452. /// This method determines whether the two coalescing candidates can be merged.
  453. /// In order to be merged, all instructions must be able to
  454. /// 1. Move to the beginning of the SourceRegion.BranchTargetBlock;
  455. /// 2. Move to the end of the TargetRegion.BranchBlock.
  456. /// Merging involves moving the instructions in the
  457. /// TargetRegion.BranchTargetBlock (also SourceRegion.BranchBlock).
  458. ///
  459. /// This function first try to move instructions from the
  460. /// TargetRegion.BranchTargetBlock down, to the beginning of the
  461. /// SourceRegion.BranchTargetBlock. This is not possible if any register defined
  462. /// in TargetRegion.BranchTargetBlock is used in a PHI node in the
  463. /// SourceRegion.BranchTargetBlock. In this case, check whether the statement
  464. /// can be moved up, to the end of the TargetRegion.BranchBlock (immediately
  465. /// before the branch statement). If it cannot move, then these blocks cannot
  466. /// be merged.
  467. ///
  468. /// Note that there is no analysis for moving instructions past the fall-through
  469. /// blocks because they are confirmed to be empty. An assert is thrown if they
  470. /// are not.
  471. ///
  472. /// \param[in] SourceRegion The candidate to move statements from
  473. /// \param[in] TargetRegion The candidate to move statements to
  474. /// \return true if all instructions in SourceRegion.BranchBlock can be merged
  475. /// into a block in TargetRegion, false otherwise.
  476. ///
  477. bool PPCBranchCoalescing::canMerge(CoalescingCandidateInfo &SourceRegion,
  478. CoalescingCandidateInfo &TargetRegion) const {
  479. if (!validateCandidates(SourceRegion, TargetRegion))
  480. return false;
  481. // Walk through PHI nodes first and see if they force the merge into the
  482. // SourceRegion.BranchTargetBlock.
  483. for (MachineBasicBlock::iterator
  484. I = SourceRegion.BranchBlock->instr_begin(),
  485. E = SourceRegion.BranchBlock->getFirstNonPHI();
  486. I != E; ++I) {
  487. for (auto &Def : I->defs())
  488. for (auto &Use : MRI->use_instructions(Def.getReg())) {
  489. if (Use.isPHI() && Use.getParent() == SourceRegion.BranchTargetBlock) {
  490. LLVM_DEBUG(dbgs()
  491. << "PHI " << *I
  492. << " defines register used in another "
  493. "PHI within branch target block -- can't merge\n");
  494. NumPHINotMoved++;
  495. return false;
  496. }
  497. if (Use.getParent() == SourceRegion.BranchBlock) {
  498. LLVM_DEBUG(dbgs() << "PHI " << *I
  499. << " defines register used in this "
  500. "block -- all must move down\n");
  501. SourceRegion.MustMoveDown = true;
  502. }
  503. }
  504. }
  505. // Walk through the MI to see if they should be merged into
  506. // TargetRegion.BranchBlock (up) or SourceRegion.BranchTargetBlock (down)
  507. for (MachineBasicBlock::iterator
  508. I = SourceRegion.BranchBlock->getFirstNonPHI(),
  509. E = SourceRegion.BranchBlock->end();
  510. I != E; ++I) {
  511. if (!canMoveToBeginning(*I, *SourceRegion.BranchTargetBlock)) {
  512. LLVM_DEBUG(dbgs() << "Instruction " << *I
  513. << " cannot move down - must move up!\n");
  514. SourceRegion.MustMoveUp = true;
  515. }
  516. if (!canMoveToEnd(*I, *TargetRegion.BranchBlock)) {
  517. LLVM_DEBUG(dbgs() << "Instruction " << *I
  518. << " cannot move up - must move down!\n");
  519. SourceRegion.MustMoveDown = true;
  520. }
  521. }
  522. return (SourceRegion.MustMoveUp && SourceRegion.MustMoveDown) ? false : true;
  523. }
  524. /// Merge the instructions from SourceRegion.BranchBlock,
  525. /// SourceRegion.BranchTargetBlock, and SourceRegion.FallThroughBlock into
  526. /// TargetRegion.BranchBlock, TargetRegion.BranchTargetBlock and
  527. /// TargetRegion.FallThroughBlock respectively.
  528. ///
  529. /// The successors for blocks in TargetRegion will be updated to use the
  530. /// successors from blocks in SourceRegion. Finally, the blocks in SourceRegion
  531. /// will be removed from the function.
  532. ///
  533. /// A region consists of a BranchBlock, a FallThroughBlock, and a
  534. /// BranchTargetBlock. Branch coalesce works on patterns where the
  535. /// TargetRegion's BranchTargetBlock must also be the SourceRegions's
  536. /// BranchBlock.
  537. ///
  538. /// Before mergeCandidates:
  539. ///
  540. /// +---------------------------+
  541. /// | TargetRegion.BranchBlock |
  542. /// +---------------------------+
  543. /// / |
  544. /// / +--------------------------------+
  545. /// | | TargetRegion.FallThroughBlock |
  546. /// \ +--------------------------------+
  547. /// \ |
  548. /// +----------------------------------+
  549. /// | TargetRegion.BranchTargetBlock |
  550. /// | SourceRegion.BranchBlock |
  551. /// +----------------------------------+
  552. /// / |
  553. /// / +--------------------------------+
  554. /// | | SourceRegion.FallThroughBlock |
  555. /// \ +--------------------------------+
  556. /// \ |
  557. /// +----------------------------------+
  558. /// | SourceRegion.BranchTargetBlock |
  559. /// +----------------------------------+
  560. ///
  561. /// After mergeCandidates:
  562. ///
  563. /// +-----------------------------+
  564. /// | TargetRegion.BranchBlock |
  565. /// | SourceRegion.BranchBlock |
  566. /// +-----------------------------+
  567. /// / |
  568. /// / +---------------------------------+
  569. /// | | TargetRegion.FallThroughBlock |
  570. /// | | SourceRegion.FallThroughBlock |
  571. /// \ +---------------------------------+
  572. /// \ |
  573. /// +----------------------------------+
  574. /// | SourceRegion.BranchTargetBlock |
  575. /// +----------------------------------+
  576. ///
  577. /// \param[in] SourceRegion The candidate to move blocks from
  578. /// \param[in] TargetRegion The candidate to move blocks to
  579. ///
  580. bool PPCBranchCoalescing::mergeCandidates(CoalescingCandidateInfo &SourceRegion,
  581. CoalescingCandidateInfo &TargetRegion) {
  582. if (SourceRegion.MustMoveUp && SourceRegion.MustMoveDown) {
  583. llvm_unreachable("Cannot have both MustMoveDown and MustMoveUp set!");
  584. return false;
  585. }
  586. if (!validateCandidates(SourceRegion, TargetRegion))
  587. return false;
  588. // Start the merging process by first handling the BranchBlock.
  589. // Move any PHIs in SourceRegion.BranchBlock down to the branch-taken block
  590. moveAndUpdatePHIs(SourceRegion.BranchBlock, SourceRegion.BranchTargetBlock);
  591. // Move remaining instructions in SourceRegion.BranchBlock into
  592. // TargetRegion.BranchBlock
  593. MachineBasicBlock::iterator firstInstr =
  594. SourceRegion.BranchBlock->getFirstNonPHI();
  595. MachineBasicBlock::iterator lastInstr =
  596. SourceRegion.BranchBlock->getFirstTerminator();
  597. MachineBasicBlock *Source = SourceRegion.MustMoveDown
  598. ? SourceRegion.BranchTargetBlock
  599. : TargetRegion.BranchBlock;
  600. MachineBasicBlock::iterator Target =
  601. SourceRegion.MustMoveDown
  602. ? SourceRegion.BranchTargetBlock->getFirstNonPHI()
  603. : TargetRegion.BranchBlock->getFirstTerminator();
  604. Source->splice(Target, SourceRegion.BranchBlock, firstInstr, lastInstr);
  605. // Once PHI and instructions have been moved we need to clean up the
  606. // control flow.
  607. // Remove SourceRegion.FallThroughBlock before transferring successors of
  608. // SourceRegion.BranchBlock to TargetRegion.BranchBlock.
  609. SourceRegion.BranchBlock->removeSuccessor(SourceRegion.FallThroughBlock);
  610. TargetRegion.BranchBlock->transferSuccessorsAndUpdatePHIs(
  611. SourceRegion.BranchBlock);
  612. // Update branch in TargetRegion.BranchBlock to jump to
  613. // SourceRegion.BranchTargetBlock
  614. // In this case, TargetRegion.BranchTargetBlock == SourceRegion.BranchBlock.
  615. TargetRegion.BranchBlock->ReplaceUsesOfBlockWith(
  616. SourceRegion.BranchBlock, SourceRegion.BranchTargetBlock);
  617. // Remove the branch statement(s) in SourceRegion.BranchBlock
  618. MachineBasicBlock::iterator I =
  619. SourceRegion.BranchBlock->terminators().begin();
  620. while (I != SourceRegion.BranchBlock->terminators().end()) {
  621. MachineInstr &CurrInst = *I;
  622. ++I;
  623. if (CurrInst.isBranch())
  624. CurrInst.eraseFromParent();
  625. }
  626. // Fall-through block should be empty since this is part of the condition
  627. // to coalesce the branches.
  628. assert(TargetRegion.FallThroughBlock->empty() &&
  629. "FallThroughBlocks should be empty!");
  630. // Transfer successor information and move PHIs down to the
  631. // branch-taken block.
  632. TargetRegion.FallThroughBlock->transferSuccessorsAndUpdatePHIs(
  633. SourceRegion.FallThroughBlock);
  634. TargetRegion.FallThroughBlock->removeSuccessor(SourceRegion.BranchBlock);
  635. // Remove the blocks from the function.
  636. assert(SourceRegion.BranchBlock->empty() &&
  637. "Expecting branch block to be empty!");
  638. SourceRegion.BranchBlock->eraseFromParent();
  639. assert(SourceRegion.FallThroughBlock->empty() &&
  640. "Expecting fall-through block to be empty!\n");
  641. SourceRegion.FallThroughBlock->eraseFromParent();
  642. NumBlocksCoalesced++;
  643. return true;
  644. }
  645. bool PPCBranchCoalescing::runOnMachineFunction(MachineFunction &MF) {
  646. if (skipFunction(MF.getFunction()) || MF.empty())
  647. return false;
  648. bool didSomething = false;
  649. LLVM_DEBUG(dbgs() << "******** Branch Coalescing ********\n");
  650. initialize(MF);
  651. LLVM_DEBUG(dbgs() << "Function: "; MF.dump(); dbgs() << "\n");
  652. CoalescingCandidateInfo Cand1, Cand2;
  653. // Walk over blocks and find candidates to merge
  654. // Continue trying to merge with the first candidate found, as long as merging
  655. // is successfull.
  656. for (MachineBasicBlock &MBB : MF) {
  657. bool MergedCandidates = false;
  658. do {
  659. MergedCandidates = false;
  660. Cand1.clear();
  661. Cand2.clear();
  662. Cand1.BranchBlock = &MBB;
  663. // If unable to coalesce the branch, then continue to next block
  664. if (!canCoalesceBranch(Cand1))
  665. break;
  666. Cand2.BranchBlock = Cand1.BranchTargetBlock;
  667. if (!canCoalesceBranch(Cand2))
  668. break;
  669. // The branch-taken block of the second candidate should post-dominate the
  670. // first candidate.
  671. assert(MPDT->dominates(Cand2.BranchTargetBlock, Cand1.BranchBlock) &&
  672. "Branch-taken block should post-dominate first candidate");
  673. if (!identicalOperands(Cand1.Cond, Cand2.Cond)) {
  674. LLVM_DEBUG(dbgs() << "Blocks " << Cand1.BranchBlock->getNumber()
  675. << " and " << Cand2.BranchBlock->getNumber()
  676. << " have different branches\n");
  677. break;
  678. }
  679. if (!canMerge(Cand2, Cand1)) {
  680. LLVM_DEBUG(dbgs() << "Cannot merge blocks "
  681. << Cand1.BranchBlock->getNumber() << " and "
  682. << Cand2.BranchBlock->getNumber() << "\n");
  683. NumBlocksNotCoalesced++;
  684. continue;
  685. }
  686. LLVM_DEBUG(dbgs() << "Merging blocks " << Cand1.BranchBlock->getNumber()
  687. << " and " << Cand1.BranchTargetBlock->getNumber()
  688. << "\n");
  689. MergedCandidates = mergeCandidates(Cand2, Cand1);
  690. if (MergedCandidates)
  691. didSomething = true;
  692. LLVM_DEBUG(dbgs() << "Function after merging: "; MF.dump();
  693. dbgs() << "\n");
  694. } while (MergedCandidates);
  695. }
  696. #ifndef NDEBUG
  697. // Verify MF is still valid after branch coalescing
  698. if (didSomething)
  699. MF.verify(nullptr, "Error in code produced by branch coalescing");
  700. #endif // NDEBUG
  701. LLVM_DEBUG(dbgs() << "Finished Branch Coalescing\n");
  702. return didSomething;
  703. }