Chris Lattner | 25e48dd | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 1 | //===-- BranchFolding.cpp - Fold machine code branch instructions ---------===// |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 25e48dd | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 25e48dd | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass forwards branches to unconditional branches to make them branch |
| 11 | // directly to the target block. This pass often results in dead MBB's, which |
| 12 | // it then removes. |
| 13 | // |
| 14 | // Note that this pass must be run after register allocation, it cannot handle |
Jingyue Wu | 3a04dc6 | 2015-07-29 18:59:09 +0000 | [diff] [blame] | 15 | // SSA form. It also must handle virtual registers for targets that emit virtual |
| 16 | // ISA (e.g. NVPTX). |
Chris Lattner | 25e48dd | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 20 | #include "BranchFolding.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
| 22 | #include "llvm/ADT/SmallSet.h" |
| 23 | #include "llvm/ADT/Statistic.h" |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/Analysis.h" |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
| 26 | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
Chris Lattner | 25e48dd | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chris Lattner | 56c9d25 | 2006-10-17 17:13:52 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Chad Rosier | 3b67c8d | 2015-03-10 16:22:52 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineMemOperand.h" |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Jakob Stoklund Olesen | d1664a1 | 2012-03-27 17:06:09 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/Passes.h" |
Matthias Braun | 31d19d4 | 2016-05-10 03:21:59 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/TargetPassConfig.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Function.h" |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 36 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 56ec81f | 2006-11-18 21:56:39 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ErrorHandling.h" |
Bill Wendling | c3f05e8 | 2009-08-22 20:03:00 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 40 | #include "llvm/Target/TargetInstrInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetRegisterInfo.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetSubtargetInfo.h" |
Jeff Cohen | 7d6f3db | 2006-11-05 19:31:28 +0000 | [diff] [blame] | 43 | #include <algorithm> |
Chris Lattner | 25e48dd | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 46 | #define DEBUG_TYPE "branchfolding" |
| 47 | |
Chris Lattner | aee775a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 48 | STATISTIC(NumDeadBlocks, "Number of dead blocks removed"); |
| 49 | STATISTIC(NumBranchOpts, "Number of branches optimized"); |
| 50 | STATISTIC(NumTailMerge , "Number of block tails merged"); |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 51 | STATISTIC(NumHoist , "Number of times common instructions are hoisted"); |
Hans Wennborg | 75e25f6 | 2016-09-07 17:52:14 +0000 | [diff] [blame] | 52 | STATISTIC(NumTailCalls, "Number of tail calls optimized"); |
Bob Wilson | 8984e6e | 2009-11-18 19:29:37 +0000 | [diff] [blame] | 53 | |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 54 | static cl::opt<cl::boolOrDefault> FlagEnableTailMerge("enable-tail-merge", |
Dale Johannesen | 82810c8 | 2007-05-22 17:14:46 +0000 | [diff] [blame] | 55 | cl::init(cl::BOU_UNSET), cl::Hidden); |
Bob Wilson | 8984e6e | 2009-11-18 19:29:37 +0000 | [diff] [blame] | 56 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 57 | // Throttle for huge numbers of predecessors (compile speed problems) |
| 58 | static cl::opt<unsigned> |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 59 | TailMergeThreshold("tail-merge-threshold", |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 60 | cl::desc("Max number of predecessors to consider tail merging"), |
Dale Johannesen | 1d7e42c | 2008-10-27 02:10:21 +0000 | [diff] [blame] | 61 | cl::init(150), cl::Hidden); |
Dale Johannesen | 86798e5 | 2007-06-08 01:08:52 +0000 | [diff] [blame] | 62 | |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 63 | // Heuristic for tail merging (and, inversely, tail duplication). |
| 64 | // TODO: This should be replaced with a target query. |
| 65 | static cl::opt<unsigned> |
Bob Wilson | 699f5b9 | 2009-11-16 17:56:13 +0000 | [diff] [blame] | 66 | TailMergeSize("tail-merge-size", |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 67 | cl::desc("Min number of instructions to consider tail merging"), |
| 68 | cl::init(3), cl::Hidden); |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 69 | |
Dan Gohman | a9b40a6e | 2009-11-12 01:59:26 +0000 | [diff] [blame] | 70 | namespace { |
| 71 | /// BranchFolderPass - Wrap branch folder in a machine function pass. |
Andrew Trick | 58648e4 | 2012-02-08 21:22:48 +0000 | [diff] [blame] | 72 | class BranchFolderPass : public MachineFunctionPass { |
Dan Gohman | a9b40a6e | 2009-11-12 01:59:26 +0000 | [diff] [blame] | 73 | public: |
| 74 | static char ID; |
Andrew Trick | 58648e4 | 2012-02-08 21:22:48 +0000 | [diff] [blame] | 75 | explicit BranchFolderPass(): MachineFunctionPass(ID) {} |
Dan Gohman | a9b40a6e | 2009-11-12 01:59:26 +0000 | [diff] [blame] | 76 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 77 | bool runOnMachineFunction(MachineFunction &MF) override; |
Andrew Trick | 58648e4 | 2012-02-08 21:22:48 +0000 | [diff] [blame] | 78 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 79 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 80 | AU.addRequired<MachineBlockFrequencyInfo>(); |
| 81 | AU.addRequired<MachineBranchProbabilityInfo>(); |
Andrew Trick | 58648e4 | 2012-02-08 21:22:48 +0000 | [diff] [blame] | 82 | AU.addRequired<TargetPassConfig>(); |
| 83 | MachineFunctionPass::getAnalysisUsage(AU); |
| 84 | } |
Dan Gohman | a9b40a6e | 2009-11-12 01:59:26 +0000 | [diff] [blame] | 85 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 86 | } |
Dan Gohman | a9b40a6e | 2009-11-12 01:59:26 +0000 | [diff] [blame] | 87 | |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 88 | char BranchFolderPass::ID = 0; |
Andrew Trick | 58648e4 | 2012-02-08 21:22:48 +0000 | [diff] [blame] | 89 | char &llvm::BranchFolderPassID = BranchFolderPass::ID; |
Chris Lattner | 25e48dd | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 90 | |
Andrew Trick | 58648e4 | 2012-02-08 21:22:48 +0000 | [diff] [blame] | 91 | INITIALIZE_PASS(BranchFolderPass, "branch-folder", |
| 92 | "Control Flow Optimizer", false, false) |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 93 | |
| 94 | bool BranchFolderPass::runOnMachineFunction(MachineFunction &MF) { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 95 | if (skipFunction(*MF.getFunction())) |
Paul Robinson | 7c99ec5 | 2014-03-31 17:43:35 +0000 | [diff] [blame] | 96 | return false; |
| 97 | |
Andrew Trick | 58648e4 | 2012-02-08 21:22:48 +0000 | [diff] [blame] | 98 | TargetPassConfig *PassConfig = &getAnalysis<TargetPassConfig>(); |
Vincent Lejeune | 92b0a64 | 2013-12-07 01:49:19 +0000 | [diff] [blame] | 99 | // TailMerge can create jump into if branches that make CFG irreducible for |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 100 | // HW that requires structurized CFG. |
Vincent Lejeune | 92b0a64 | 2013-12-07 01:49:19 +0000 | [diff] [blame] | 101 | bool EnableTailMerge = !MF.getTarget().requiresStructuredCFG() && |
David Majnemer | 4600c06 | 2015-10-01 21:04:13 +0000 | [diff] [blame] | 102 | PassConfig->getEnableTailMerge(); |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 103 | BranchFolder::MBFIWrapper MBBFreqInfo( |
| 104 | getAnalysis<MachineBlockFrequencyInfo>()); |
| 105 | BranchFolder Folder(EnableTailMerge, /*CommonHoist=*/true, MBBFreqInfo, |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 106 | getAnalysis<MachineBranchProbabilityInfo>()); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 107 | return Folder.OptimizeFunction(MF, MF.getSubtarget().getInstrInfo(), |
| 108 | MF.getSubtarget().getRegisterInfo(), |
| 109 | getAnalysisIfAvailable<MachineModuleInfo>()); |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 112 | BranchFolder::BranchFolder(bool defaultEnableTailMerge, bool CommonHoist, |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 113 | MBFIWrapper &FreqInfo, |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 114 | const MachineBranchProbabilityInfo &ProbInfo, |
| 115 | unsigned MinTailLength) |
| 116 | : EnableHoistCommonCode(CommonHoist), MinCommonTailLength(MinTailLength), |
| 117 | MBBFreqInfo(FreqInfo), MBPI(ProbInfo) { |
| 118 | if (MinCommonTailLength == 0) |
| 119 | MinCommonTailLength = TailMergeSize; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 120 | switch (FlagEnableTailMerge) { |
| 121 | case cl::BOU_UNSET: EnableTailMerge = defaultEnableTailMerge; break; |
| 122 | case cl::BOU_TRUE: EnableTailMerge = true; break; |
| 123 | case cl::BOU_FALSE: EnableTailMerge = false; break; |
| 124 | } |
Evan Cheng | 1f6e5eb | 2009-09-03 23:54:22 +0000 | [diff] [blame] | 125 | } |
Chris Lattner | 25e48dd | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 56c9d25 | 2006-10-17 17:13:52 +0000 | [diff] [blame] | 127 | /// RemoveDeadBlock - Remove the specified dead machine basic block from the |
| 128 | /// function, updating the CFG. |
Chris Lattner | 73da320 | 2006-10-17 23:17:27 +0000 | [diff] [blame] | 129 | void BranchFolder::RemoveDeadBlock(MachineBasicBlock *MBB) { |
Jim Laskey | 9df1a1d | 2007-02-22 16:39:03 +0000 | [diff] [blame] | 130 | assert(MBB->pred_empty() && "MBB must be dead!"); |
David Greene | d60abbf | 2009-12-24 00:34:21 +0000 | [diff] [blame] | 131 | DEBUG(dbgs() << "\nRemoving MBB: " << *MBB); |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 56c9d25 | 2006-10-17 17:13:52 +0000 | [diff] [blame] | 133 | MachineFunction *MF = MBB->getParent(); |
| 134 | // drop all successors. |
| 135 | while (!MBB->succ_empty()) |
| 136 | MBB->removeSuccessor(MBB->succ_end()-1); |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 137 | |
Rafael Espindola | 3aeaf9e | 2011-06-14 15:31:54 +0000 | [diff] [blame] | 138 | // Avoid matching if this pointer gets reused. |
| 139 | TriedMerging.erase(MBB); |
| 140 | |
Chris Lattner | 56c9d25 | 2006-10-17 17:13:52 +0000 | [diff] [blame] | 141 | // Remove the block. |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 142 | MF->erase(MBB); |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 143 | FuncletMembership.erase(MBB); |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 144 | if (MLI) |
| 145 | MLI->removeBlock(MBB); |
Chris Lattner | 56c9d25 | 2006-10-17 17:13:52 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 148 | /// OptimizeFunction - Perhaps branch folding, tail merging and other |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 149 | /// CFG optimizations on the given function. Block placement changes the layout |
| 150 | /// and may create new tail merging opportunities. |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 151 | bool BranchFolder::OptimizeFunction(MachineFunction &MF, |
| 152 | const TargetInstrInfo *tii, |
| 153 | const TargetRegisterInfo *tri, |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 154 | MachineModuleInfo *mmi, |
| 155 | MachineLoopInfo *mli, bool AfterPlacement) { |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 156 | if (!tii) return false; |
Chris Lattner | 3218e0e | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 157 | |
Rafael Espindola | 3aeaf9e | 2011-06-14 15:31:54 +0000 | [diff] [blame] | 158 | TriedMerging.clear(); |
| 159 | |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 160 | AfterBlockPlacement = AfterPlacement; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 161 | TII = tii; |
| 162 | TRI = tri; |
| 163 | MMI = mmi; |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 164 | MLI = mli; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 165 | |
Jakob Stoklund Olesen | d1664a1 | 2012-03-27 17:06:09 +0000 | [diff] [blame] | 166 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Matthias Braun | aeab09f | 2016-07-12 18:44:33 +0000 | [diff] [blame] | 167 | UpdateLiveIns = MRI.tracksLiveness() && TRI->trackLivenessAfterRegAlloc(MF); |
| 168 | if (!UpdateLiveIns) |
Jakob Stoklund Olesen | d1664a1 | 2012-03-27 17:06:09 +0000 | [diff] [blame] | 169 | MRI.invalidateLiveness(); |
Evan Cheng | 9d33984 | 2008-04-10 02:32:10 +0000 | [diff] [blame] | 170 | |
Dale Johannesen | 420a85d | 2007-05-15 21:19:17 +0000 | [diff] [blame] | 171 | // Fix CFG. The later algorithms expect it to be right. |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 172 | bool MadeChange = false; |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 173 | for (MachineBasicBlock &MBB : MF) { |
| 174 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 175 | SmallVector<MachineOperand, 4> Cond; |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 176 | if (!TII->analyzeBranch(MBB, TBB, FBB, Cond, true)) |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 177 | MadeChange |= MBB.CorrectExtraCFGEdges(TBB, FBB, !Cond.empty()); |
Dale Johannesen | 420a85d | 2007-05-15 21:19:17 +0000 | [diff] [blame] | 178 | } |
| 179 | |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 180 | // Recalculate funclet membership. |
| 181 | FuncletMembership = getFuncletMembership(MF); |
| 182 | |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 183 | bool MadeChangeThisIteration = true; |
| 184 | while (MadeChangeThisIteration) { |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 185 | MadeChangeThisIteration = TailMergeBlocks(MF); |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 186 | // No need to clean up if tail merging does not change anything after the |
| 187 | // block placement. |
| 188 | if (!AfterBlockPlacement || MadeChangeThisIteration) |
| 189 | MadeChangeThisIteration |= OptimizeBranches(MF); |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 190 | if (EnableHoistCommonCode) |
| 191 | MadeChangeThisIteration |= HoistCommonCode(MF); |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 192 | MadeChange |= MadeChangeThisIteration; |
Chris Lattner | 25e48dd | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Bob Wilson | bc5af98 | 2010-03-19 19:05:41 +0000 | [diff] [blame] | 195 | // See if any jump tables have become dead as the code generator |
Chris Lattner | c07657f | 2006-10-28 18:34:47 +0000 | [diff] [blame] | 196 | // did its thing. |
| 197 | MachineJumpTableInfo *JTI = MF.getJumpTableInfo(); |
Matthias Braun | aeab09f | 2016-07-12 18:44:33 +0000 | [diff] [blame] | 198 | if (!JTI) |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 199 | return MadeChange; |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 200 | |
Bob Wilson | bc5af98 | 2010-03-19 19:05:41 +0000 | [diff] [blame] | 201 | // Walk the function to find jump tables that are live. |
| 202 | BitVector JTIsLive(JTI->getJumpTables().size()); |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 203 | for (const MachineBasicBlock &BB : MF) { |
| 204 | for (const MachineInstr &I : BB) |
| 205 | for (const MachineOperand &Op : I.operands()) { |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 206 | if (!Op.isJTI()) continue; |
Chris Lattner | c07657f | 2006-10-28 18:34:47 +0000 | [diff] [blame] | 207 | |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 208 | // Remember that this JT is live. |
Bob Wilson | bc5af98 | 2010-03-19 19:05:41 +0000 | [diff] [blame] | 209 | JTIsLive.set(Op.getIndex()); |
Chris Lattner | c07657f | 2006-10-28 18:34:47 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 212 | |
Bob Wilson | bc5af98 | 2010-03-19 19:05:41 +0000 | [diff] [blame] | 213 | // Finally, remove dead jump tables. This happens when the |
| 214 | // indirect jump was unreachable (and thus deleted). |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 215 | for (unsigned i = 0, e = JTIsLive.size(); i != e; ++i) |
| 216 | if (!JTIsLive.test(i)) { |
| 217 | JTI->RemoveJumpTable(i); |
| 218 | MadeChange = true; |
| 219 | } |
| 220 | |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 221 | return MadeChange; |
Chris Lattner | 25e48dd | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 224 | //===----------------------------------------------------------------------===// |
| 225 | // Tail Merging of Blocks |
| 226 | //===----------------------------------------------------------------------===// |
| 227 | |
NAKAMURA Takumi | 3746abb | 2015-06-20 06:21:48 +0000 | [diff] [blame] | 228 | /// HashMachineInstr - Compute a hash value for MI and its operands. |
Duncan P. N. Exon Smith | d3a7467 | 2016-02-27 19:48:01 +0000 | [diff] [blame] | 229 | static unsigned HashMachineInstr(const MachineInstr &MI) { |
| 230 | unsigned Hash = MI.getOpcode(); |
| 231 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 232 | const MachineOperand &Op = MI.getOperand(i); |
NAKAMURA Takumi | 3746abb | 2015-06-20 06:21:48 +0000 | [diff] [blame] | 233 | |
Benjamin Kramer | 8c57cfd | 2015-06-23 14:47:36 +0000 | [diff] [blame] | 234 | // Merge in bits from the operand if easy. We can't use MachineOperand's |
| 235 | // hash_code here because it's not deterministic and we sort by hash value |
| 236 | // later. |
NAKAMURA Takumi | 3746abb | 2015-06-20 06:21:48 +0000 | [diff] [blame] | 237 | unsigned OperandHash = 0; |
| 238 | switch (Op.getType()) { |
NAKAMURA Takumi | 34d3376 | 2015-06-20 06:22:04 +0000 | [diff] [blame] | 239 | case MachineOperand::MO_Register: |
| 240 | OperandHash = Op.getReg(); |
| 241 | break; |
| 242 | case MachineOperand::MO_Immediate: |
| 243 | OperandHash = Op.getImm(); |
| 244 | break; |
NAKAMURA Takumi | 3746abb | 2015-06-20 06:21:48 +0000 | [diff] [blame] | 245 | case MachineOperand::MO_MachineBasicBlock: |
| 246 | OperandHash = Op.getMBB()->getNumber(); |
| 247 | break; |
| 248 | case MachineOperand::MO_FrameIndex: |
| 249 | case MachineOperand::MO_ConstantPoolIndex: |
| 250 | case MachineOperand::MO_JumpTableIndex: |
| 251 | OperandHash = Op.getIndex(); |
| 252 | break; |
| 253 | case MachineOperand::MO_GlobalAddress: |
| 254 | case MachineOperand::MO_ExternalSymbol: |
| 255 | // Global address / external symbol are too hard, don't bother, but do |
| 256 | // pull in the offset. |
| 257 | OperandHash = Op.getOffset(); |
| 258 | break; |
NAKAMURA Takumi | 34d3376 | 2015-06-20 06:22:04 +0000 | [diff] [blame] | 259 | default: |
| 260 | break; |
NAKAMURA Takumi | 3746abb | 2015-06-20 06:21:48 +0000 | [diff] [blame] | 261 | } |
| 262 | |
NAKAMURA Takumi | 34d3376 | 2015-06-20 06:22:04 +0000 | [diff] [blame] | 263 | Hash += ((OperandHash << 3) | Op.getType()) << (i & 31); |
NAKAMURA Takumi | 3746abb | 2015-06-20 06:21:48 +0000 | [diff] [blame] | 264 | } |
| 265 | return Hash; |
| 266 | } |
| 267 | |
Dan Gohman | 2ad68de | 2010-05-03 14:35:47 +0000 | [diff] [blame] | 268 | /// HashEndOfMBB - Hash the last instruction in the MBB. |
Duncan P. N. Exon Smith | d3a7467 | 2016-02-27 19:48:01 +0000 | [diff] [blame] | 269 | static unsigned HashEndOfMBB(const MachineBasicBlock &MBB) { |
| 270 | MachineBasicBlock::const_iterator I = MBB.getLastNonDebugInstr(); |
| 271 | if (I == MBB.end()) |
Benjamin Kramer | 6b56896 | 2015-06-23 14:47:29 +0000 | [diff] [blame] | 272 | return 0; |
NAKAMURA Takumi | 3746abb | 2015-06-20 06:21:48 +0000 | [diff] [blame] | 273 | |
Duncan P. N. Exon Smith | d3a7467 | 2016-02-27 19:48:01 +0000 | [diff] [blame] | 274 | return HashMachineInstr(*I); |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /// ComputeCommonTailLength - Given two machine basic blocks, compute the number |
| 278 | /// of instructions they actually have in common together at their end. Return |
| 279 | /// iterators for the first shared instruction in each block. |
| 280 | static unsigned ComputeCommonTailLength(MachineBasicBlock *MBB1, |
| 281 | MachineBasicBlock *MBB2, |
| 282 | MachineBasicBlock::iterator &I1, |
| 283 | MachineBasicBlock::iterator &I2) { |
| 284 | I1 = MBB1->end(); |
| 285 | I2 = MBB2->end(); |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 287 | unsigned TailLen = 0; |
| 288 | while (I1 != MBB1->begin() && I2 != MBB2->begin()) { |
| 289 | --I1; --I2; |
Dale Johannesen | 0eebdbb | 2010-03-08 05:38:13 +0000 | [diff] [blame] | 290 | // Skip debugging pseudos; necessary to avoid changing the code. |
| 291 | while (I1->isDebugValue()) { |
Dale Johannesen | 5ebe0c1 | 2010-03-10 05:45:47 +0000 | [diff] [blame] | 292 | if (I1==MBB1->begin()) { |
| 293 | while (I2->isDebugValue()) { |
| 294 | if (I2==MBB2->begin()) |
| 295 | // I1==DBG at begin; I2==DBG at begin |
| 296 | return TailLen; |
| 297 | --I2; |
| 298 | } |
| 299 | ++I2; |
| 300 | // I1==DBG at begin; I2==non-DBG, or first of DBGs not at begin |
Dale Johannesen | 0eebdbb | 2010-03-08 05:38:13 +0000 | [diff] [blame] | 301 | return TailLen; |
Dale Johannesen | 5ebe0c1 | 2010-03-10 05:45:47 +0000 | [diff] [blame] | 302 | } |
Dale Johannesen | 0eebdbb | 2010-03-08 05:38:13 +0000 | [diff] [blame] | 303 | --I1; |
| 304 | } |
Dale Johannesen | 5ebe0c1 | 2010-03-10 05:45:47 +0000 | [diff] [blame] | 305 | // I1==first (untested) non-DBG preceding known match |
Dale Johannesen | 0eebdbb | 2010-03-08 05:38:13 +0000 | [diff] [blame] | 306 | while (I2->isDebugValue()) { |
Dale Johannesen | 5ebe0c1 | 2010-03-10 05:45:47 +0000 | [diff] [blame] | 307 | if (I2==MBB2->begin()) { |
| 308 | ++I1; |
| 309 | // I1==non-DBG, or first of DBGs not at begin; I2==DBG at begin |
Dale Johannesen | 0eebdbb | 2010-03-08 05:38:13 +0000 | [diff] [blame] | 310 | return TailLen; |
Dale Johannesen | 5ebe0c1 | 2010-03-10 05:45:47 +0000 | [diff] [blame] | 311 | } |
Dale Johannesen | 0eebdbb | 2010-03-08 05:38:13 +0000 | [diff] [blame] | 312 | --I2; |
| 313 | } |
Dale Johannesen | 5ebe0c1 | 2010-03-10 05:45:47 +0000 | [diff] [blame] | 314 | // I1, I2==first (untested) non-DBGs preceding known match |
Duncan P. N. Exon Smith | fd8cc23 | 2016-02-27 20:01:33 +0000 | [diff] [blame] | 315 | if (!I1->isIdenticalTo(*I2) || |
Bill Wendling | f73340e | 2007-10-25 19:49:32 +0000 | [diff] [blame] | 316 | // FIXME: This check is dubious. It's used to get around a problem where |
Bill Wendling | 5f7ed00 | 2007-10-25 18:23:45 +0000 | [diff] [blame] | 317 | // people incorrectly expect inline asm directives to remain in the same |
| 318 | // relative order. This is untenable because normal compiler |
| 319 | // optimizations (like this one) may reorder and/or merge these |
| 320 | // directives. |
Chris Lattner | b06015a | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 321 | I1->isInlineAsm()) { |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 322 | ++I1; ++I2; |
| 323 | break; |
| 324 | } |
| 325 | ++TailLen; |
| 326 | } |
Dale Johannesen | 5ebe0c1 | 2010-03-10 05:45:47 +0000 | [diff] [blame] | 327 | // Back past possible debugging pseudos at beginning of block. This matters |
| 328 | // when one block differs from the other only by whether debugging pseudos |
Junmo Park | 7cc13f2 | 2015-12-04 02:06:59 +0000 | [diff] [blame] | 329 | // are present at the beginning. (This way, the various checks later for |
Dale Johannesen | 5ebe0c1 | 2010-03-10 05:45:47 +0000 | [diff] [blame] | 330 | // I1==MBB1->begin() work as expected.) |
| 331 | if (I1 == MBB1->begin() && I2 != MBB2->begin()) { |
| 332 | --I2; |
| 333 | while (I2->isDebugValue()) { |
Craig Topper | 2f6031c | 2012-10-07 20:31:05 +0000 | [diff] [blame] | 334 | if (I2 == MBB2->begin()) |
Dale Johannesen | 5ebe0c1 | 2010-03-10 05:45:47 +0000 | [diff] [blame] | 335 | return TailLen; |
Dale Johannesen | 5ebe0c1 | 2010-03-10 05:45:47 +0000 | [diff] [blame] | 336 | --I2; |
| 337 | } |
| 338 | ++I2; |
| 339 | } |
| 340 | if (I2 == MBB2->begin() && I1 != MBB1->begin()) { |
| 341 | --I1; |
| 342 | while (I1->isDebugValue()) { |
| 343 | if (I1 == MBB1->begin()) |
| 344 | return TailLen; |
| 345 | --I1; |
| 346 | } |
| 347 | ++I1; |
| 348 | } |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 349 | return TailLen; |
| 350 | } |
| 351 | |
Matthias Braun | aeab09f | 2016-07-12 18:44:33 +0000 | [diff] [blame] | 352 | void BranchFolder::computeLiveIns(MachineBasicBlock &MBB) { |
| 353 | if (!UpdateLiveIns) |
| 354 | return; |
| 355 | |
Matthias Braun | 0c989a8 | 2016-12-08 00:15:51 +0000 | [diff] [blame] | 356 | LiveRegs.init(*TRI); |
Matthias Braun | aeab09f | 2016-07-12 18:44:33 +0000 | [diff] [blame] | 357 | LiveRegs.addLiveOutsNoPristines(MBB); |
| 358 | for (MachineInstr &MI : make_range(MBB.rbegin(), MBB.rend())) |
| 359 | LiveRegs.stepBackward(MI); |
| 360 | |
| 361 | for (unsigned Reg : LiveRegs) { |
| 362 | // Skip the register if we are about to add one of its super registers. |
| 363 | bool ContainsSuperReg = false; |
| 364 | for (MCSuperRegIterator SReg(Reg, TRI); SReg.isValid(); ++SReg) { |
| 365 | if (LiveRegs.contains(*SReg)) { |
| 366 | ContainsSuperReg = true; |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | if (ContainsSuperReg) |
| 371 | continue; |
| 372 | MBB.addLiveIn(Reg); |
Evan Cheng | 6cc8d49 | 2012-01-07 03:35:48 +0000 | [diff] [blame] | 373 | } |
Eli Friedman | bf00736 | 2011-07-06 23:41:48 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 376 | /// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything |
Evan Cheng | 2d51c7c | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 377 | /// after it, replacing it with an unconditional branch to NewDest. |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 378 | void BranchFolder::ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst, |
| 379 | MachineBasicBlock *NewDest) { |
Evan Cheng | 2d51c7c | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 380 | TII->ReplaceTailWithBranchTo(OldInst, NewDest); |
Eli Friedman | bf00736 | 2011-07-06 23:41:48 +0000 | [diff] [blame] | 381 | |
Matthias Braun | aeab09f | 2016-07-12 18:44:33 +0000 | [diff] [blame] | 382 | computeLiveIns(*NewDest); |
Eli Friedman | bf00736 | 2011-07-06 23:41:48 +0000 | [diff] [blame] | 383 | |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 384 | ++NumTailMerge; |
| 385 | } |
| 386 | |
Chris Lattner | f505a5a | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 387 | /// SplitMBBAt - Given a machine basic block and an iterator into it, split the |
| 388 | /// MBB so that the part before the iterator falls into the part starting at the |
| 389 | /// iterator. This returns the new MBB. |
| 390 | MachineBasicBlock *BranchFolder::SplitMBBAt(MachineBasicBlock &CurMBB, |
Andrew Trick | 97a1d7c | 2013-06-24 01:55:01 +0000 | [diff] [blame] | 391 | MachineBasicBlock::iterator BBI1, |
| 392 | const BasicBlock *BB) { |
Evan Cheng | 37bb617 | 2010-06-22 01:18:16 +0000 | [diff] [blame] | 393 | if (!TII->isLegalToSplitMBBAt(CurMBB, BBI1)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 394 | return nullptr; |
Evan Cheng | 37bb617 | 2010-06-22 01:18:16 +0000 | [diff] [blame] | 395 | |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 396 | MachineFunction &MF = *CurMBB.getParent(); |
| 397 | |
Chris Lattner | f505a5a | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 398 | // Create the fall-through block. |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 399 | MachineFunction::iterator MBBI = CurMBB.getIterator(); |
Andrew Trick | 97a1d7c | 2013-06-24 01:55:01 +0000 | [diff] [blame] | 400 | MachineBasicBlock *NewMBB =MF.CreateMachineBasicBlock(BB); |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 401 | CurMBB.getParent()->insert(++MBBI, NewMBB); |
Chris Lattner | f505a5a | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 402 | |
| 403 | // Move all the successors of this block to the specified block. |
Dan Gohman | 6f88069 | 2008-06-19 17:22:29 +0000 | [diff] [blame] | 404 | NewMBB->transferSuccessors(&CurMBB); |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 405 | |
Chris Lattner | f505a5a | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 406 | // Add an edge from CurMBB to NewMBB for the fall-through. |
| 407 | CurMBB.addSuccessor(NewMBB); |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 408 | |
Chris Lattner | f505a5a | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 409 | // Splice the code over. |
| 410 | NewMBB->splice(NewMBB->end(), &CurMBB, BBI1, CurMBB.end()); |
Dale Johannesen | d05a1a2 | 2007-03-20 21:35:06 +0000 | [diff] [blame] | 411 | |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 412 | // NewMBB belongs to the same loop as CurMBB. |
| 413 | if (MLI) |
| 414 | if (MachineLoop *ML = MLI->getLoopFor(&CurMBB)) |
| 415 | ML->addBasicBlockToLoop(NewMBB, MLI->getBase()); |
| 416 | |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 417 | // NewMBB inherits CurMBB's block frequency. |
| 418 | MBBFreqInfo.setBlockFreq(NewMBB, MBBFreqInfo.getBlockFreq(&CurMBB)); |
| 419 | |
Matthias Braun | aeab09f | 2016-07-12 18:44:33 +0000 | [diff] [blame] | 420 | computeLiveIns(*NewMBB); |
Dale Johannesen | d05a1a2 | 2007-03-20 21:35:06 +0000 | [diff] [blame] | 421 | |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 422 | // Add the new block to the funclet. |
| 423 | const auto &FuncletI = FuncletMembership.find(&CurMBB); |
Stephan Bergmann | 480de22 | 2016-03-31 15:42:01 +0000 | [diff] [blame] | 424 | if (FuncletI != FuncletMembership.end()) { |
| 425 | auto n = FuncletI->second; |
| 426 | FuncletMembership[NewMBB] = n; |
| 427 | } |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 428 | |
Chris Lattner | f505a5a | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 429 | return NewMBB; |
| 430 | } |
| 431 | |
Chris Lattner | 7cee6dd | 2006-11-01 19:36:29 +0000 | [diff] [blame] | 432 | /// EstimateRuntime - Make a rough estimate for how long it will take to run |
| 433 | /// the specified code. |
| 434 | static unsigned EstimateRuntime(MachineBasicBlock::iterator I, |
Chris Lattner | a98c679 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 435 | MachineBasicBlock::iterator E) { |
Chris Lattner | 7cee6dd | 2006-11-01 19:36:29 +0000 | [diff] [blame] | 436 | unsigned Time = 0; |
| 437 | for (; I != E; ++I) { |
Dale Johannesen | 2061c84 | 2010-03-05 00:02:59 +0000 | [diff] [blame] | 438 | if (I->isDebugValue()) |
| 439 | continue; |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 440 | if (I->isCall()) |
Chris Lattner | 7cee6dd | 2006-11-01 19:36:29 +0000 | [diff] [blame] | 441 | Time += 10; |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 442 | else if (I->mayLoad() || I->mayStore()) |
Chris Lattner | 7cee6dd | 2006-11-01 19:36:29 +0000 | [diff] [blame] | 443 | Time += 2; |
| 444 | else |
| 445 | ++Time; |
| 446 | } |
| 447 | return Time; |
| 448 | } |
| 449 | |
Dale Johannesen | 6e16d09 | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 450 | // CurMBB needs to add an unconditional branch to SuccMBB (we removed these |
| 451 | // branches temporarily for tail merging). In the case where CurMBB ends |
| 452 | // with a conditional branch to the next block, optimize by reversing the |
| 453 | // test and conditionally branching to SuccMBB instead. |
Bob Wilson | 3794ec2 | 2009-11-16 18:08:46 +0000 | [diff] [blame] | 454 | static void FixTail(MachineBasicBlock *CurMBB, MachineBasicBlock *SuccBB, |
Dale Johannesen | 6e16d09 | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 455 | const TargetInstrInfo *TII) { |
| 456 | MachineFunction *MF = CurMBB->getParent(); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 457 | MachineFunction::iterator I = std::next(MachineFunction::iterator(CurMBB)); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 458 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 459 | SmallVector<MachineOperand, 4> Cond; |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 460 | DebugLoc dl; // FIXME: this is nowhere |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 461 | if (I != MF->end() && !TII->analyzeBranch(*CurMBB, TBB, FBB, Cond, true)) { |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 462 | MachineBasicBlock *NextBB = &*I; |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 463 | if (TBB == NextBB && !Cond.empty() && !FBB) { |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 464 | if (!TII->reverseBranchCondition(Cond)) { |
| 465 | TII->removeBranch(*CurMBB); |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 466 | TII->insertBranch(*CurMBB, SuccBB, nullptr, Cond, dl); |
Dale Johannesen | 6e16d09 | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 467 | return; |
| 468 | } |
| 469 | } |
| 470 | } |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 471 | TII->insertBranch(*CurMBB, SuccBB, nullptr, |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 472 | SmallVector<MachineOperand, 0>(), dl); |
Dale Johannesen | 6e16d09 | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 475 | bool |
| 476 | BranchFolder::MergePotentialsElt::operator<(const MergePotentialsElt &o) const { |
| 477 | if (getHash() < o.getHash()) |
| 478 | return true; |
Craig Topper | 2f6031c | 2012-10-07 20:31:05 +0000 | [diff] [blame] | 479 | if (getHash() > o.getHash()) |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 480 | return false; |
Craig Topper | 2f6031c | 2012-10-07 20:31:05 +0000 | [diff] [blame] | 481 | if (getBlock()->getNumber() < o.getBlock()->getNumber()) |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 482 | return true; |
Craig Topper | 2f6031c | 2012-10-07 20:31:05 +0000 | [diff] [blame] | 483 | if (getBlock()->getNumber() > o.getBlock()->getNumber()) |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 484 | return false; |
Craig Topper | 2f6031c | 2012-10-07 20:31:05 +0000 | [diff] [blame] | 485 | // _GLIBCXX_DEBUG checks strict weak ordering, which involves comparing |
| 486 | // an object with itself. |
Duncan Sands | d5ea194 | 2007-07-11 08:47:55 +0000 | [diff] [blame] | 487 | #ifndef _GLIBCXX_DEBUG |
Craig Topper | 2f6031c | 2012-10-07 20:31:05 +0000 | [diff] [blame] | 488 | llvm_unreachable("Predecessor appears twice"); |
David Blaikie | 46a9f01 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 489 | #else |
Craig Topper | 2f6031c | 2012-10-07 20:31:05 +0000 | [diff] [blame] | 490 | return false; |
David Blaikie | 46a9f01 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 491 | #endif |
Dale Johannesen | a69ebdb | 2007-05-29 23:47:50 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 494 | BlockFrequency |
| 495 | BranchFolder::MBFIWrapper::getBlockFreq(const MachineBasicBlock *MBB) const { |
| 496 | auto I = MergedBBFreq.find(MBB); |
| 497 | |
| 498 | if (I != MergedBBFreq.end()) |
| 499 | return I->second; |
| 500 | |
| 501 | return MBFI.getBlockFreq(MBB); |
| 502 | } |
| 503 | |
| 504 | void BranchFolder::MBFIWrapper::setBlockFreq(const MachineBasicBlock *MBB, |
| 505 | BlockFrequency F) { |
| 506 | MergedBBFreq[MBB] = F; |
| 507 | } |
| 508 | |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 509 | raw_ostream & |
| 510 | BranchFolder::MBFIWrapper::printBlockFreq(raw_ostream &OS, |
| 511 | const MachineBasicBlock *MBB) const { |
| 512 | return MBFI.printBlockFreq(OS, getBlockFreq(MBB)); |
| 513 | } |
| 514 | |
| 515 | raw_ostream & |
| 516 | BranchFolder::MBFIWrapper::printBlockFreq(raw_ostream &OS, |
| 517 | const BlockFrequency Freq) const { |
| 518 | return MBFI.printBlockFreq(OS, Freq); |
| 519 | } |
| 520 | |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 521 | /// CountTerminators - Count the number of terminators in the given |
| 522 | /// block and set I to the position of the first non-terminator, if there |
| 523 | /// is one, or MBB->end() otherwise. |
| 524 | static unsigned CountTerminators(MachineBasicBlock *MBB, |
| 525 | MachineBasicBlock::iterator &I) { |
| 526 | I = MBB->end(); |
| 527 | unsigned NumTerms = 0; |
| 528 | for (;;) { |
| 529 | if (I == MBB->begin()) { |
| 530 | I = MBB->end(); |
| 531 | break; |
| 532 | } |
| 533 | --I; |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 534 | if (!I->isTerminator()) break; |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 535 | ++NumTerms; |
| 536 | } |
| 537 | return NumTerms; |
| 538 | } |
| 539 | |
Bob Wilson | 94f8f87 | 2009-10-29 18:40:06 +0000 | [diff] [blame] | 540 | /// ProfitableToMerge - Check if two machine basic blocks have a common tail |
| 541 | /// and decide if it would be profitable to merge those tails. Return the |
| 542 | /// length of the common tail and iterators to the first common instruction |
| 543 | /// in each block. |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 544 | /// MBB1, MBB2 The blocks to check |
| 545 | /// MinCommonTailLength Minimum size of tail block to be merged. |
| 546 | /// CommonTailLen Out parameter to record the size of the shared tail between |
| 547 | /// MBB1 and MBB2 |
| 548 | /// I1, I2 Iterator references that will be changed to point to the first |
| 549 | /// instruction in the common tail shared by MBB1,MBB2 |
| 550 | /// SuccBB A common successor of MBB1, MBB2 which are in a canonical form |
| 551 | /// relative to SuccBB |
| 552 | /// PredBB The layout predecessor of SuccBB, if any. |
| 553 | /// FuncletMembership map from block to funclet #. |
| 554 | /// AfterPlacement True if we are merging blocks after layout. Stricter |
| 555 | /// thresholds apply to prevent undoing tail-duplication. |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 556 | static bool |
| 557 | ProfitableToMerge(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2, |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 558 | unsigned MinCommonTailLength, unsigned &CommonTailLen, |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 559 | MachineBasicBlock::iterator &I1, |
| 560 | MachineBasicBlock::iterator &I2, MachineBasicBlock *SuccBB, |
| 561 | MachineBasicBlock *PredBB, |
Kyle Butt | 71b1ca1 | 2016-08-10 18:36:18 +0000 | [diff] [blame] | 562 | DenseMap<const MachineBasicBlock *, int> &FuncletMembership, |
| 563 | bool AfterPlacement) { |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 564 | // It is never profitable to tail-merge blocks from two different funclets. |
| 565 | if (!FuncletMembership.empty()) { |
| 566 | auto Funclet1 = FuncletMembership.find(MBB1); |
| 567 | assert(Funclet1 != FuncletMembership.end()); |
| 568 | auto Funclet2 = FuncletMembership.find(MBB2); |
| 569 | assert(Funclet2 != FuncletMembership.end()); |
| 570 | if (Funclet1->second != Funclet2->second) |
| 571 | return false; |
| 572 | } |
| 573 | |
Bob Wilson | 94f8f87 | 2009-10-29 18:40:06 +0000 | [diff] [blame] | 574 | CommonTailLen = ComputeCommonTailLength(MBB1, MBB2, I1, I2); |
Bob Wilson | 94f8f87 | 2009-10-29 18:40:06 +0000 | [diff] [blame] | 575 | if (CommonTailLen == 0) |
| 576 | return false; |
Evan Cheng | b8ed462 | 2011-02-21 23:39:48 +0000 | [diff] [blame] | 577 | DEBUG(dbgs() << "Common tail length of BB#" << MBB1->getNumber() |
| 578 | << " and BB#" << MBB2->getNumber() << " is " << CommonTailLen |
| 579 | << '\n'); |
Bob Wilson | 94f8f87 | 2009-10-29 18:40:06 +0000 | [diff] [blame] | 580 | |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 581 | // It's almost always profitable to merge any number of non-terminator |
| 582 | // instructions with the block that falls through into the common successor. |
Kyle Butt | 71b1ca1 | 2016-08-10 18:36:18 +0000 | [diff] [blame] | 583 | // This is true only for a single successor. For multiple successors, we are |
| 584 | // trading a conditional branch for an unconditional one. |
| 585 | // TODO: Re-visit successor size for non-layout tail merging. |
| 586 | if ((MBB1 == PredBB || MBB2 == PredBB) && |
| 587 | (!AfterPlacement || MBB1->succ_size() == 1)) { |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 588 | MachineBasicBlock::iterator I; |
| 589 | unsigned NumTerms = CountTerminators(MBB1 == PredBB ? MBB2 : MBB1, I); |
| 590 | if (CommonTailLen > NumTerms) |
| 591 | return true; |
| 592 | } |
| 593 | |
Dan Gohman | 09478e9 | 2009-11-12 00:39:10 +0000 | [diff] [blame] | 594 | // If one of the blocks can be completely merged and happens to be in |
| 595 | // a position where the other could fall through into it, merge any number |
| 596 | // of instructions, because it can be done without a branch. |
| 597 | // TODO: If the blocks are not adjacent, move one of them so that they are? |
| 598 | if (MBB1->isLayoutSuccessor(MBB2) && I2 == MBB2->begin()) |
| 599 | return true; |
| 600 | if (MBB2->isLayoutSuccessor(MBB1) && I1 == MBB1->begin()) |
| 601 | return true; |
| 602 | |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 603 | // If both blocks have an unconditional branch temporarily stripped out, |
Dan Gohman | 225fa59c | 2009-11-13 21:02:15 +0000 | [diff] [blame] | 604 | // count that as an additional common instruction for the following |
Kyle Butt | 71b1ca1 | 2016-08-10 18:36:18 +0000 | [diff] [blame] | 605 | // heuristics. This heuristic is only accurate for single-succ blocks, so to |
| 606 | // make sure that during layout merging and duplicating don't crash, we check |
| 607 | // for that when merging during layout. |
Dan Gohman | 225fa59c | 2009-11-13 21:02:15 +0000 | [diff] [blame] | 608 | unsigned EffectiveTailLen = CommonTailLen; |
Bob Wilson | 699f5b9 | 2009-11-16 17:56:13 +0000 | [diff] [blame] | 609 | if (SuccBB && MBB1 != PredBB && MBB2 != PredBB && |
Kyle Butt | 71b1ca1 | 2016-08-10 18:36:18 +0000 | [diff] [blame] | 610 | (MBB1->succ_size() == 1 || !AfterPlacement) && |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 611 | !MBB1->back().isBarrier() && |
| 612 | !MBB2->back().isBarrier()) |
Dan Gohman | 225fa59c | 2009-11-13 21:02:15 +0000 | [diff] [blame] | 613 | ++EffectiveTailLen; |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 614 | |
| 615 | // Check if the common tail is long enough to be worthwhile. |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 616 | if (EffectiveTailLen >= MinCommonTailLength) |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 617 | return true; |
| 618 | |
Dan Gohman | 225fa59c | 2009-11-13 21:02:15 +0000 | [diff] [blame] | 619 | // If we are optimizing for code size, 2 instructions in common is enough if |
| 620 | // we don't have to split a block. At worst we will be introducing 1 new |
| 621 | // branch instruction, which is likely to be smaller than the 2 |
| 622 | // instructions that would be deleted in the merge. |
Evan Cheng | b8ed462 | 2011-02-21 23:39:48 +0000 | [diff] [blame] | 623 | MachineFunction *MF = MBB1->getParent(); |
Rafael Espindola | 84921b9 | 2015-10-24 23:11:13 +0000 | [diff] [blame] | 624 | return EffectiveTailLen >= 2 && MF->getFunction()->optForSize() && |
| 625 | (I1 == MBB1->begin() || I2 == MBB2->begin()); |
Bob Wilson | 94f8f87 | 2009-10-29 18:40:06 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 628 | /// ComputeSameTails - Look through all the blocks in MergePotentials that have |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 629 | /// hash CurHash (guaranteed to match the last element). Build the vector |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 630 | /// SameTails of all those that have the (same) largest number of instructions |
| 631 | /// in common of any pair of these blocks. SameTails entries contain an |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 632 | /// iterator into MergePotentials (from which the MachineBasicBlock can be |
| 633 | /// found) and a MachineBasicBlock::iterator into that MBB indicating the |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 634 | /// instruction where the matching code sequence begins. |
| 635 | /// Order of elements in SameTails is the reverse of the order in which |
| 636 | /// those blocks appear in MergePotentials (where they are not necessarily |
| 637 | /// consecutive). |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 638 | unsigned BranchFolder::ComputeSameTails(unsigned CurHash, |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 639 | unsigned MinCommonTailLength, |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 640 | MachineBasicBlock *SuccBB, |
| 641 | MachineBasicBlock *PredBB) { |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 642 | unsigned maxCommonTailLength = 0U; |
| 643 | SameTails.clear(); |
| 644 | MachineBasicBlock::iterator TrialBBI1, TrialBBI2; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 645 | MPIterator HighestMPIter = std::prev(MergePotentials.end()); |
| 646 | for (MPIterator CurMPIter = std::prev(MergePotentials.end()), |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 647 | B = MergePotentials.begin(); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 648 | CurMPIter != B && CurMPIter->getHash() == CurHash; --CurMPIter) { |
| 649 | for (MPIterator I = std::prev(CurMPIter); I->getHash() == CurHash; --I) { |
Bob Wilson | 94f8f87 | 2009-10-29 18:40:06 +0000 | [diff] [blame] | 650 | unsigned CommonTailLen; |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 651 | if (ProfitableToMerge(CurMPIter->getBlock(), I->getBlock(), |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 652 | MinCommonTailLength, |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 653 | CommonTailLen, TrialBBI1, TrialBBI2, |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 654 | SuccBB, PredBB, |
Kyle Butt | 71b1ca1 | 2016-08-10 18:36:18 +0000 | [diff] [blame] | 655 | FuncletMembership, |
| 656 | AfterBlockPlacement)) { |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 657 | if (CommonTailLen > maxCommonTailLength) { |
| 658 | SameTails.clear(); |
| 659 | maxCommonTailLength = CommonTailLen; |
| 660 | HighestMPIter = CurMPIter; |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 661 | SameTails.push_back(SameTailElt(CurMPIter, TrialBBI1)); |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 662 | } |
| 663 | if (HighestMPIter == CurMPIter && |
| 664 | CommonTailLen == maxCommonTailLength) |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 665 | SameTails.push_back(SameTailElt(I, TrialBBI2)); |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 666 | } |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 667 | if (I == B) |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 668 | break; |
| 669 | } |
| 670 | } |
| 671 | return maxCommonTailLength; |
| 672 | } |
| 673 | |
| 674 | /// RemoveBlocksWithHash - Remove all blocks with hash CurHash from |
| 675 | /// MergePotentials, restoring branches at ends of blocks as appropriate. |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 676 | void BranchFolder::RemoveBlocksWithHash(unsigned CurHash, |
Bob Wilson | 3794ec2 | 2009-11-16 18:08:46 +0000 | [diff] [blame] | 677 | MachineBasicBlock *SuccBB, |
| 678 | MachineBasicBlock *PredBB) { |
Dale Johannesen | b28a17c | 2008-05-23 17:19:02 +0000 | [diff] [blame] | 679 | MPIterator CurMPIter, B; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 680 | for (CurMPIter = std::prev(MergePotentials.end()), |
| 681 | B = MergePotentials.begin(); |
| 682 | CurMPIter->getHash() == CurHash; --CurMPIter) { |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 683 | // Put the unconditional branch back, if we need one. |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 684 | MachineBasicBlock *CurMBB = CurMPIter->getBlock(); |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 685 | if (SuccBB && CurMBB != PredBB) |
| 686 | FixTail(CurMBB, SuccBB, TII); |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 687 | if (CurMPIter == B) |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 688 | break; |
| 689 | } |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 690 | if (CurMPIter->getHash() != CurHash) |
Dale Johannesen | b28a17c | 2008-05-23 17:19:02 +0000 | [diff] [blame] | 691 | CurMPIter++; |
| 692 | MergePotentials.erase(CurMPIter, MergePotentials.end()); |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 695 | /// CreateCommonTailOnlyBlock - None of the blocks to be tail-merged consist |
| 696 | /// only of the common tail. Create a block that does by splitting one. |
Evan Cheng | 37bb617 | 2010-06-22 01:18:16 +0000 | [diff] [blame] | 697 | bool BranchFolder::CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB, |
Andrew Trick | 97a1d7c | 2013-06-24 01:55:01 +0000 | [diff] [blame] | 698 | MachineBasicBlock *SuccBB, |
Evan Cheng | 37bb617 | 2010-06-22 01:18:16 +0000 | [diff] [blame] | 699 | unsigned maxCommonTailLength, |
| 700 | unsigned &commonTailIndex) { |
| 701 | commonTailIndex = 0; |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 702 | unsigned TimeEstimate = ~0U; |
Dan Gohman | b3bf49f | 2009-11-12 01:51:28 +0000 | [diff] [blame] | 703 | for (unsigned i = 0, e = SameTails.size(); i != e; ++i) { |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 704 | // Use PredBB if possible; that doesn't require a new branch. |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 705 | if (SameTails[i].getBlock() == PredBB) { |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 706 | commonTailIndex = i; |
| 707 | break; |
| 708 | } |
| 709 | // Otherwise, make a (fairly bogus) choice based on estimate of |
| 710 | // how long it will take the various blocks to execute. |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 711 | unsigned t = EstimateRuntime(SameTails[i].getBlock()->begin(), |
| 712 | SameTails[i].getTailStartPos()); |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 713 | if (t <= TimeEstimate) { |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 714 | TimeEstimate = t; |
| 715 | commonTailIndex = i; |
| 716 | } |
| 717 | } |
| 718 | |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 719 | MachineBasicBlock::iterator BBI = |
| 720 | SameTails[commonTailIndex].getTailStartPos(); |
| 721 | MachineBasicBlock *MBB = SameTails[commonTailIndex].getBlock(); |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 722 | |
David Greene | d60abbf | 2009-12-24 00:34:21 +0000 | [diff] [blame] | 723 | DEBUG(dbgs() << "\nSplitting BB#" << MBB->getNumber() << ", size " |
Bill Wendling | c3f05e8 | 2009-08-22 20:03:00 +0000 | [diff] [blame] | 724 | << maxCommonTailLength); |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 725 | |
Andrew Trick | 97a1d7c | 2013-06-24 01:55:01 +0000 | [diff] [blame] | 726 | // If the split block unconditionally falls-thru to SuccBB, it will be |
| 727 | // merged. In control flow terms it should then take SuccBB's name. e.g. If |
| 728 | // SuccBB is an inner loop, the common tail is still part of the inner loop. |
| 729 | const BasicBlock *BB = (SuccBB && MBB->succ_size() == 1) ? |
| 730 | SuccBB->getBasicBlock() : MBB->getBasicBlock(); |
| 731 | MachineBasicBlock *newMBB = SplitMBBAt(*MBB, BBI, BB); |
Evan Cheng | 37bb617 | 2010-06-22 01:18:16 +0000 | [diff] [blame] | 732 | if (!newMBB) { |
| 733 | DEBUG(dbgs() << "... failed!"); |
| 734 | return false; |
| 735 | } |
| 736 | |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 737 | SameTails[commonTailIndex].setBlock(newMBB); |
| 738 | SameTails[commonTailIndex].setTailStartPos(newMBB->begin()); |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 739 | |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 740 | // If we split PredBB, newMBB is the new predecessor. |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 741 | if (PredBB == MBB) |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 742 | PredBB = newMBB; |
| 743 | |
Evan Cheng | 37bb617 | 2010-06-22 01:18:16 +0000 | [diff] [blame] | 744 | return true; |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Chad Rosier | 3b67c8d | 2015-03-10 16:22:52 +0000 | [diff] [blame] | 747 | static void |
Matthias Braun | 4040c0f | 2016-09-20 01:14:42 +0000 | [diff] [blame] | 748 | mergeOperations(MachineBasicBlock::iterator MBBIStartPos, |
| 749 | MachineBasicBlock &MBBCommon) { |
Chad Rosier | 3b67c8d | 2015-03-10 16:22:52 +0000 | [diff] [blame] | 750 | MachineBasicBlock *MBB = MBBIStartPos->getParent(); |
| 751 | // Note CommonTailLen does not necessarily matches the size of |
| 752 | // the common BB nor all its instructions because of debug |
| 753 | // instructions differences. |
| 754 | unsigned CommonTailLen = 0; |
| 755 | for (auto E = MBB->end(); MBBIStartPos != E; ++MBBIStartPos) |
| 756 | ++CommonTailLen; |
| 757 | |
| 758 | MachineBasicBlock::reverse_iterator MBBI = MBB->rbegin(); |
Chad Rosier | 99fb8d1 | 2015-03-10 20:29:59 +0000 | [diff] [blame] | 759 | MachineBasicBlock::reverse_iterator MBBIE = MBB->rend(); |
Chad Rosier | 3b67c8d | 2015-03-10 16:22:52 +0000 | [diff] [blame] | 760 | MachineBasicBlock::reverse_iterator MBBICommon = MBBCommon.rbegin(); |
| 761 | MachineBasicBlock::reverse_iterator MBBIECommon = MBBCommon.rend(); |
| 762 | |
| 763 | while (CommonTailLen--) { |
Chad Rosier | 99fb8d1 | 2015-03-10 20:29:59 +0000 | [diff] [blame] | 764 | assert(MBBI != MBBIE && "Reached BB end within common tail length!"); |
| 765 | (void)MBBIE; |
Chad Rosier | 3b67c8d | 2015-03-10 16:22:52 +0000 | [diff] [blame] | 766 | |
| 767 | if (MBBI->isDebugValue()) { |
| 768 | ++MBBI; |
| 769 | continue; |
| 770 | } |
| 771 | |
| 772 | while ((MBBICommon != MBBIECommon) && MBBICommon->isDebugValue()) |
| 773 | ++MBBICommon; |
| 774 | |
| 775 | assert(MBBICommon != MBBIECommon && |
| 776 | "Reached BB end within common tail length!"); |
Duncan P. N. Exon Smith | fd8cc23 | 2016-02-27 20:01:33 +0000 | [diff] [blame] | 777 | assert(MBBICommon->isIdenticalTo(*MBBI) && "Expected matching MIIs!"); |
Chad Rosier | 3b67c8d | 2015-03-10 16:22:52 +0000 | [diff] [blame] | 778 | |
Matthias Braun | 4040c0f | 2016-09-20 01:14:42 +0000 | [diff] [blame] | 779 | // Merge MMOs from memory operations in the common block. |
Chad Rosier | 3b67c8d | 2015-03-10 16:22:52 +0000 | [diff] [blame] | 780 | if (MBBICommon->mayLoad() || MBBICommon->mayStore()) |
Junmo Park | 7ceec0b | 2016-01-11 07:15:38 +0000 | [diff] [blame] | 781 | MBBICommon->setMemRefs(MBBICommon->mergeMemRefsWith(*MBBI)); |
Matthias Braun | 4040c0f | 2016-09-20 01:14:42 +0000 | [diff] [blame] | 782 | // Drop undef flags if they aren't present in all merged instructions. |
| 783 | for (unsigned I = 0, E = MBBICommon->getNumOperands(); I != E; ++I) { |
| 784 | MachineOperand &MO = MBBICommon->getOperand(I); |
| 785 | if (MO.isReg() && MO.isUndef()) { |
| 786 | const MachineOperand &OtherMO = MBBI->getOperand(I); |
| 787 | if (!OtherMO.isUndef()) |
| 788 | MO.setIsUndef(false); |
| 789 | } |
| 790 | } |
Chad Rosier | 3b67c8d | 2015-03-10 16:22:52 +0000 | [diff] [blame] | 791 | |
| 792 | ++MBBI; |
| 793 | ++MBBICommon; |
| 794 | } |
| 795 | } |
| 796 | |
Kyle Butt | 83a2579 | 2016-07-11 21:37:03 +0000 | [diff] [blame] | 797 | // See if any of the blocks in MergePotentials (which all have SuccBB as a |
| 798 | // successor, or all have no successor if it is null) can be tail-merged. |
| 799 | // If there is a successor, any blocks in MergePotentials that are not |
| 800 | // tail-merged and are not immediately before Succ must have an unconditional |
| 801 | // branch to Succ added (but the predecessor/successor lists need no |
| 802 | // adjustment). The lone predecessor of Succ that falls through into Succ, |
Dale Johannesen | 9a25b3a | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 803 | // if any, is given in PredBB. |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 804 | // MinCommonTailLength - Except for the special cases below, tail-merge if |
| 805 | // there are at least this many instructions in common. |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 806 | bool BranchFolder::TryTailMergeBlocks(MachineBasicBlock *SuccBB, |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 807 | MachineBasicBlock *PredBB, |
| 808 | unsigned MinCommonTailLength) { |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 809 | bool MadeChange = false; |
| 810 | |
David Greene | d60abbf | 2009-12-24 00:34:21 +0000 | [diff] [blame] | 811 | DEBUG(dbgs() << "\nTryTailMergeBlocks: "; |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 812 | for (unsigned i = 0, e = MergePotentials.size(); i != e; ++i) |
David Greene | d60abbf | 2009-12-24 00:34:21 +0000 | [diff] [blame] | 813 | dbgs() << "BB#" << MergePotentials[i].getBlock()->getNumber() |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 814 | << (i == e-1 ? "" : ", "); |
David Greene | d60abbf | 2009-12-24 00:34:21 +0000 | [diff] [blame] | 815 | dbgs() << "\n"; |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 816 | if (SuccBB) { |
David Greene | d60abbf | 2009-12-24 00:34:21 +0000 | [diff] [blame] | 817 | dbgs() << " with successor BB#" << SuccBB->getNumber() << '\n'; |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 818 | if (PredBB) |
David Greene | d60abbf | 2009-12-24 00:34:21 +0000 | [diff] [blame] | 819 | dbgs() << " which has fall-through from BB#" |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 820 | << PredBB->getNumber() << "\n"; |
| 821 | } |
David Greene | d60abbf | 2009-12-24 00:34:21 +0000 | [diff] [blame] | 822 | dbgs() << "Looking for common tails of at least " |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 823 | << MinCommonTailLength << " instruction" |
| 824 | << (MinCommonTailLength == 1 ? "" : "s") << '\n'; |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 825 | ); |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 826 | |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 827 | // Sort by hash value so that blocks with identical end sequences sort |
| 828 | // together. |
Benjamin Kramer | 848c9fa | 2015-03-13 21:17:02 +0000 | [diff] [blame] | 829 | array_pod_sort(MergePotentials.begin(), MergePotentials.end()); |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 830 | |
| 831 | // Walk through equivalence sets looking for actual exact matches. |
| 832 | while (MergePotentials.size() > 1) { |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 833 | unsigned CurHash = MergePotentials.back().getHash(); |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 834 | |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 835 | // Build SameTails, identifying the set of blocks with this hash code |
| 836 | // and with the maximum number of instructions in common. |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 837 | unsigned maxCommonTailLength = ComputeSameTails(CurHash, |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 838 | MinCommonTailLength, |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 839 | SuccBB, PredBB); |
Dale Johannesen | f4a77d2 | 2007-05-23 21:07:20 +0000 | [diff] [blame] | 840 | |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 841 | // If we didn't find any pair that has at least MinCommonTailLength |
Dale Johannesen | cff7df2 | 2008-05-09 21:24:35 +0000 | [diff] [blame] | 842 | // instructions in common, remove all blocks with this hash code and retry. |
| 843 | if (SameTails.empty()) { |
Dale Johannesen | 66da8b5 | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 844 | RemoveBlocksWithHash(CurHash, SuccBB, PredBB); |
Dale Johannesen | f4a77d2 | 2007-05-23 21:07:20 +0000 | [diff] [blame] | 845 | continue; |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 846 | } |
Chris Lattner | f505a5a | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 847 | |
Dale Johannesen | cff7df2 | 2008-05-09 21:24:35 +0000 | [diff] [blame] | 848 | // If one of the blocks is the entire common tail (and not the entry |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 849 | // block, which we can't jump to), we can treat all blocks with this same |
| 850 | // tail at once. Use PredBB if that is one of the possibilities, as that |
| 851 | // will not introduce any extra branches. |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 852 | MachineBasicBlock *EntryBB = |
| 853 | &MergePotentials.front().getBlock()->getParent()->front(); |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 854 | unsigned commonTailIndex = SameTails.size(); |
Dan Gohman | 09478e9 | 2009-11-12 00:39:10 +0000 | [diff] [blame] | 855 | // If there are two blocks, check to see if one can be made to fall through |
| 856 | // into the other. |
| 857 | if (SameTails.size() == 2 && |
| 858 | SameTails[0].getBlock()->isLayoutSuccessor(SameTails[1].getBlock()) && |
| 859 | SameTails[1].tailIsWholeBlock()) |
| 860 | commonTailIndex = 1; |
| 861 | else if (SameTails.size() == 2 && |
| 862 | SameTails[1].getBlock()->isLayoutSuccessor( |
| 863 | SameTails[0].getBlock()) && |
| 864 | SameTails[0].tailIsWholeBlock()) |
| 865 | commonTailIndex = 0; |
| 866 | else { |
| 867 | // Otherwise just pick one, favoring the fall-through predecessor if |
| 868 | // there is one. |
| 869 | for (unsigned i = 0, e = SameTails.size(); i != e; ++i) { |
| 870 | MachineBasicBlock *MBB = SameTails[i].getBlock(); |
| 871 | if (MBB == EntryBB && SameTails[i].tailIsWholeBlock()) |
| 872 | continue; |
| 873 | if (MBB == PredBB) { |
| 874 | commonTailIndex = i; |
| 875 | break; |
| 876 | } |
| 877 | if (SameTails[i].tailIsWholeBlock()) |
| 878 | commonTailIndex = i; |
Dale Johannesen | cff7df2 | 2008-05-09 21:24:35 +0000 | [diff] [blame] | 879 | } |
Dale Johannesen | cff7df2 | 2008-05-09 21:24:35 +0000 | [diff] [blame] | 880 | } |
Dale Johannesen | 3c0a137 | 2007-06-01 23:02:45 +0000 | [diff] [blame] | 881 | |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 882 | if (commonTailIndex == SameTails.size() || |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 883 | (SameTails[commonTailIndex].getBlock() == PredBB && |
| 884 | !SameTails[commonTailIndex].tailIsWholeBlock())) { |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 885 | // None of the blocks consist entirely of the common tail. |
| 886 | // Split a block so that one does. |
Andrew Trick | 97a1d7c | 2013-06-24 01:55:01 +0000 | [diff] [blame] | 887 | if (!CreateCommonTailOnlyBlock(PredBB, SuccBB, |
Evan Cheng | 37bb617 | 2010-06-22 01:18:16 +0000 | [diff] [blame] | 888 | maxCommonTailLength, commonTailIndex)) { |
| 889 | RemoveBlocksWithHash(CurHash, SuccBB, PredBB); |
| 890 | continue; |
| 891 | } |
Chris Lattner | f505a5a | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 892 | } |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 893 | |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 894 | MachineBasicBlock *MBB = SameTails[commonTailIndex].getBlock(); |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 895 | |
Marianne Mailhot-Sarrasin | eddc5b1 | 2016-03-10 21:54:25 +0000 | [diff] [blame] | 896 | // Recompute common tail MBB's edge weights and block frequency. |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 897 | setCommonTailEdgeWeights(*MBB); |
| 898 | |
Robert Lougher | 660f2f9 | 2016-10-26 17:01:47 +0000 | [diff] [blame] | 899 | // Remove the original debug location from the common tail. |
| 900 | for (auto &MI : *MBB) |
| 901 | if (!MI.isDebugValue()) |
| 902 | MI.setDebugLoc(DebugLoc()); |
| 903 | |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 904 | // MBB is common tail. Adjust all other BB's to jump to this one. |
| 905 | // Traversal must be forwards so erases work. |
David Greene | d60abbf | 2009-12-24 00:34:21 +0000 | [diff] [blame] | 906 | DEBUG(dbgs() << "\nUsing common tail in BB#" << MBB->getNumber() |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 907 | << " for "); |
| 908 | for (unsigned int i=0, e = SameTails.size(); i != e; ++i) { |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 909 | if (commonTailIndex == i) |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 910 | continue; |
David Greene | d60abbf | 2009-12-24 00:34:21 +0000 | [diff] [blame] | 911 | DEBUG(dbgs() << "BB#" << SameTails[i].getBlock()->getNumber() |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 912 | << (i == e-1 ? "" : ", ")); |
Matthias Braun | 4040c0f | 2016-09-20 01:14:42 +0000 | [diff] [blame] | 913 | // Merge operations (MMOs, undef flags) |
| 914 | mergeOperations(SameTails[i].getTailStartPos(), *MBB); |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 915 | // Hack the end off BB i, making it jump to BB commonTailIndex instead. |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 916 | ReplaceTailWithBranchTo(SameTails[i].getTailStartPos(), MBB); |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 917 | // BB i is no longer a predecessor of SuccBB; remove it from the worklist. |
Dan Gohman | 02b1554 | 2009-11-11 21:57:02 +0000 | [diff] [blame] | 918 | MergePotentials.erase(SameTails[i].getMPIter()); |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 919 | } |
David Greene | d60abbf | 2009-12-24 00:34:21 +0000 | [diff] [blame] | 920 | DEBUG(dbgs() << "\n"); |
Dale Johannesen | c4c4d8e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 921 | // We leave commonTailIndex in the worklist in case there are other blocks |
| 922 | // that match it with a smaller number of instructions. |
Chris Lattner | f505a5a | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 923 | MadeChange = true; |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 924 | } |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 925 | return MadeChange; |
| 926 | } |
| 927 | |
Dale Johannesen | 9a25b3a | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 928 | bool BranchFolder::TailMergeBlocks(MachineFunction &MF) { |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 929 | bool MadeChange = false; |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 930 | if (!EnableTailMerge) return MadeChange; |
Dale Johannesen | 6e16d09 | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 931 | |
Dale Johannesen | 9a25b3a | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 932 | // First find blocks with no successors. |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 933 | // Block placement does not create new tail merging opportunities for these |
| 934 | // blocks. |
| 935 | if (!AfterBlockPlacement) { |
| 936 | MergePotentials.clear(); |
| 937 | for (MachineBasicBlock &MBB : MF) { |
| 938 | if (MergePotentials.size() == TailMergeThreshold) |
| 939 | break; |
| 940 | if (!TriedMerging.count(&MBB) && MBB.succ_empty()) |
| 941 | MergePotentials.push_back(MergePotentialsElt(HashEndOfMBB(MBB), &MBB)); |
| 942 | } |
| 943 | |
| 944 | // If this is a large problem, avoid visiting the same basic blocks |
| 945 | // multiple times. |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 946 | if (MergePotentials.size() == TailMergeThreshold) |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 947 | for (unsigned i = 0, e = MergePotentials.size(); i != e; ++i) |
| 948 | TriedMerging.insert(MergePotentials[i].getBlock()); |
| 949 | |
| 950 | // See if we can do any tail merging on those. |
| 951 | if (MergePotentials.size() >= 2) |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 952 | MadeChange |= TryTailMergeBlocks(nullptr, nullptr, MinCommonTailLength); |
Dale Johannesen | 9a25b3a | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 953 | } |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 954 | |
Dale Johannesen | 6e16d09 | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 955 | // Look at blocks (IBB) with multiple predecessors (PBB). |
| 956 | // We change each predecessor to a canonical form, by |
| 957 | // (1) temporarily removing any unconditional branch from the predecessor |
| 958 | // to IBB, and |
| 959 | // (2) alter conditional branches so they branch to the other block |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 960 | // not IBB; this may require adding back an unconditional branch to IBB |
Dale Johannesen | 6e16d09 | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 961 | // later, where there wasn't one coming in. E.g. |
| 962 | // Bcc IBB |
| 963 | // fallthrough to QBB |
| 964 | // here becomes |
| 965 | // Bncc QBB |
| 966 | // with a conceptual B to IBB after that, which never actually exists. |
| 967 | // With those changes, we see whether the predecessors' tails match, |
| 968 | // and merge them if so. We change things out of canonical form and |
| 969 | // back to the way they were later in the process. (OptimizeBranches |
| 970 | // would undo some of this, but we can't use it, because we'd get into |
| 971 | // a compile-time infinite loop repeatedly doing and undoing the same |
| 972 | // transformations.) |
Dale Johannesen | 9a25b3a | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 973 | |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 974 | for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end(); |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 975 | I != E; ++I) { |
Bill Wendling | e351e8c | 2012-05-23 22:12:50 +0000 | [diff] [blame] | 976 | if (I->pred_size() < 2) continue; |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 977 | SmallPtrSet<MachineBasicBlock *, 8> UniquePreds; |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 978 | MachineBasicBlock *IBB = &*I; |
| 979 | MachineBasicBlock *PredBB = &*std::prev(I); |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 980 | MergePotentials.clear(); |
Haicheng Wu | 7c4535d | 2016-08-12 23:13:38 +0000 | [diff] [blame] | 981 | MachineLoop *ML; |
| 982 | |
| 983 | // Bail if merging after placement and IBB is the loop header because |
| 984 | // -- If merging predecessors that belong to the same loop as IBB, the |
| 985 | // common tail of merged predecessors may become the loop top if block |
| 986 | // placement is called again and the predecessors may branch to this common |
| 987 | // tail and require more branches. This can be relaxed if |
| 988 | // MachineBlockPlacement::findBestLoopTop is more flexible. |
| 989 | // --If merging predecessors that do not belong to the same loop as IBB, the |
| 990 | // loop info of IBB's loop and the other loops may be affected. Calling the |
| 991 | // block placement again may make big change to the layout and eliminate the |
| 992 | // reason to do tail merging here. |
| 993 | if (AfterBlockPlacement && MLI) { |
| 994 | ML = MLI->getLoopFor(IBB); |
| 995 | if (ML && IBB == ML->getHeader()) |
| 996 | continue; |
| 997 | } |
| 998 | |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 999 | for (MachineBasicBlock *PBB : I->predecessors()) { |
| 1000 | if (MergePotentials.size() == TailMergeThreshold) |
| 1001 | break; |
| 1002 | |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 1003 | if (TriedMerging.count(PBB)) |
| 1004 | continue; |
| 1005 | |
| 1006 | // Skip blocks that loop to themselves, can't tail merge these. |
| 1007 | if (PBB == IBB) |
| 1008 | continue; |
| 1009 | |
| 1010 | // Visit each predecessor only once. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1011 | if (!UniquePreds.insert(PBB).second) |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 1012 | continue; |
| 1013 | |
| 1014 | // Skip blocks which may jump to a landing pad. Can't tail merge these. |
Reid Kleckner | ed17079 | 2015-09-17 17:19:40 +0000 | [diff] [blame] | 1015 | if (PBB->hasEHPadSuccessor()) |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 1016 | continue; |
| 1017 | |
Haicheng Wu | 7c4535d | 2016-08-12 23:13:38 +0000 | [diff] [blame] | 1018 | // After block placement, only consider predecessors that belong to the |
| 1019 | // same loop as IBB. The reason is the same as above when skipping loop |
| 1020 | // header. |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 1021 | if (AfterBlockPlacement && MLI) |
Haicheng Wu | 7c4535d | 2016-08-12 23:13:38 +0000 | [diff] [blame] | 1022 | if (ML != MLI->getLoopFor(PBB)) |
| 1023 | continue; |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 1024 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1025 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 1026 | SmallVector<MachineOperand, 4> Cond; |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1027 | if (!TII->analyzeBranch(*PBB, TBB, FBB, Cond, true)) { |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 1028 | // Failing case: IBB is the target of a cbr, and we cannot reverse the |
| 1029 | // branch. |
| 1030 | SmallVector<MachineOperand, 4> NewCond(Cond); |
| 1031 | if (!Cond.empty() && TBB == IBB) { |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1032 | if (TII->reverseBranchCondition(NewCond)) |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 1033 | continue; |
| 1034 | // This is the QBB case described above |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 1035 | if (!FBB) { |
| 1036 | auto Next = ++PBB->getIterator(); |
| 1037 | if (Next != MF.end()) |
| 1038 | FBB = &*Next; |
| 1039 | } |
Dale Johannesen | 9a25b3a | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 1040 | } |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 1041 | |
| 1042 | // Failing case: the only way IBB can be reached from PBB is via |
| 1043 | // exception handling. Happens for landing pads. Would be nice to have |
| 1044 | // a bit in the edge so we didn't have to do all this. |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 1045 | if (IBB->isEHPad()) { |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 1046 | MachineFunction::iterator IP = ++PBB->getIterator(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1047 | MachineBasicBlock *PredNextBB = nullptr; |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 1048 | if (IP != MF.end()) |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 1049 | PredNextBB = &*IP; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1050 | if (!TBB) { |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 1051 | if (IBB != PredNextBB) // fallthrough |
| 1052 | continue; |
| 1053 | } else if (FBB) { |
| 1054 | if (TBB != IBB && FBB != IBB) // cbr then ubr |
| 1055 | continue; |
| 1056 | } else if (Cond.empty()) { |
| 1057 | if (TBB != IBB) // ubr |
| 1058 | continue; |
| 1059 | } else { |
| 1060 | if (TBB != IBB && IBB != PredNextBB) // cbr |
| 1061 | continue; |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | // Remove the unconditional branch at the end, if any. |
| 1066 | if (TBB && (Cond.empty() || FBB)) { |
| 1067 | DebugLoc dl; // FIXME: this is nowhere |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1068 | TII->removeBranch(*PBB); |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 1069 | if (!Cond.empty()) |
| 1070 | // reinsert conditional branch only, for now |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 1071 | TII->insertBranch(*PBB, (TBB == IBB) ? FBB : TBB, nullptr, |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1072 | NewCond, dl); |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Duncan P. N. Exon Smith | d3a7467 | 2016-02-27 19:48:01 +0000 | [diff] [blame] | 1075 | MergePotentials.push_back(MergePotentialsElt(HashEndOfMBB(*PBB), PBB)); |
Dale Johannesen | 9a25b3a | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 1076 | } |
Dale Johannesen | 9a25b3a | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 1077 | } |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 1078 | |
| 1079 | // If this is a large problem, avoid visiting the same basic blocks multiple |
| 1080 | // times. |
| 1081 | if (MergePotentials.size() == TailMergeThreshold) |
| 1082 | for (unsigned i = 0, e = MergePotentials.size(); i != e; ++i) |
| 1083 | TriedMerging.insert(MergePotentials[i].getBlock()); |
| 1084 | |
| 1085 | if (MergePotentials.size() >= 2) |
Kyle Butt | 64e4281 | 2016-08-18 18:57:29 +0000 | [diff] [blame] | 1086 | MadeChange |= TryTailMergeBlocks(IBB, PredBB, MinCommonTailLength); |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 1087 | |
| 1088 | // Reinsert an unconditional branch if needed. The 1 below can occur as a |
| 1089 | // result of removing blocks in TryTailMergeBlocks. |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 1090 | PredBB = &*std::prev(I); // this may have been changed in TryTailMergeBlocks |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 1091 | if (MergePotentials.size() == 1 && |
| 1092 | MergePotentials.begin()->getBlock() != PredBB) |
| 1093 | FixTail(MergePotentials.begin()->getBlock(), IBB, TII); |
Dale Johannesen | 9a25b3a | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 1094 | } |
Bill Wendling | 041793c | 2012-05-23 22:09:50 +0000 | [diff] [blame] | 1095 | |
Dale Johannesen | 9a25b3a | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 1096 | return MadeChange; |
| 1097 | } |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 1098 | |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 1099 | void BranchFolder::setCommonTailEdgeWeights(MachineBasicBlock &TailMBB) { |
| 1100 | SmallVector<BlockFrequency, 2> EdgeFreqLs(TailMBB.succ_size()); |
| 1101 | BlockFrequency AccumulatedMBBFreq; |
| 1102 | |
| 1103 | // Aggregate edge frequency of successor edge j: |
| 1104 | // edgeFreq(j) = sum (freq(bb) * edgeProb(bb, j)), |
| 1105 | // where bb is a basic block that is in SameTails. |
| 1106 | for (const auto &Src : SameTails) { |
| 1107 | const MachineBasicBlock *SrcMBB = Src.getBlock(); |
| 1108 | BlockFrequency BlockFreq = MBBFreqInfo.getBlockFreq(SrcMBB); |
| 1109 | AccumulatedMBBFreq += BlockFreq; |
| 1110 | |
| 1111 | // It is not necessary to recompute edge weights if TailBB has less than two |
| 1112 | // successors. |
| 1113 | if (TailMBB.succ_size() <= 1) |
| 1114 | continue; |
| 1115 | |
| 1116 | auto EdgeFreq = EdgeFreqLs.begin(); |
| 1117 | |
| 1118 | for (auto SuccI = TailMBB.succ_begin(), SuccE = TailMBB.succ_end(); |
| 1119 | SuccI != SuccE; ++SuccI, ++EdgeFreq) |
| 1120 | *EdgeFreq += BlockFreq * MBPI.getEdgeProbability(SrcMBB, *SuccI); |
| 1121 | } |
| 1122 | |
| 1123 | MBBFreqInfo.setBlockFreq(&TailMBB, AccumulatedMBBFreq); |
| 1124 | |
| 1125 | if (TailMBB.succ_size() <= 1) |
| 1126 | return; |
| 1127 | |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1128 | auto SumEdgeFreq = |
| 1129 | std::accumulate(EdgeFreqLs.begin(), EdgeFreqLs.end(), BlockFrequency(0)) |
| 1130 | .getFrequency(); |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 1131 | auto EdgeFreq = EdgeFreqLs.begin(); |
| 1132 | |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1133 | if (SumEdgeFreq > 0) { |
| 1134 | for (auto SuccI = TailMBB.succ_begin(), SuccE = TailMBB.succ_end(); |
| 1135 | SuccI != SuccE; ++SuccI, ++EdgeFreq) { |
| 1136 | auto Prob = BranchProbability::getBranchProbability( |
| 1137 | EdgeFreq->getFrequency(), SumEdgeFreq); |
| 1138 | TailMBB.setSuccProbability(SuccI, Prob); |
| 1139 | } |
| 1140 | } |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 1143 | //===----------------------------------------------------------------------===// |
| 1144 | // Branch Optimization |
| 1145 | //===----------------------------------------------------------------------===// |
| 1146 | |
| 1147 | bool BranchFolder::OptimizeBranches(MachineFunction &MF) { |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 1148 | bool MadeChange = false; |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1149 | |
Dale Johannesen | 12920dd | 2007-02-17 00:44:34 +0000 | [diff] [blame] | 1150 | // Make sure blocks are numbered in order |
| 1151 | MF.RenumberBlocks(); |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 1152 | // Renumbering blocks alters funclet membership, recalculate it. |
| 1153 | FuncletMembership = getFuncletMembership(MF); |
Dale Johannesen | 12920dd | 2007-02-17 00:44:34 +0000 | [diff] [blame] | 1154 | |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1155 | for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end(); |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1156 | I != E; ) { |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 1157 | MachineBasicBlock *MBB = &*I++; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 1158 | MadeChange |= OptimizeBlock(MBB); |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1159 | |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 1160 | // If it is dead, remove it. |
Jim Laskey | 9df1a1d | 2007-02-22 16:39:03 +0000 | [diff] [blame] | 1161 | if (MBB->pred_empty()) { |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 1162 | RemoveDeadBlock(MBB); |
| 1163 | MadeChange = true; |
| 1164 | ++NumDeadBlocks; |
| 1165 | } |
| 1166 | } |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 1167 | |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 1168 | return MadeChange; |
| 1169 | } |
| 1170 | |
Dale Johannesen | 5ebe0c1 | 2010-03-10 05:45:47 +0000 | [diff] [blame] | 1171 | // Blocks should be considered empty if they contain only debug info; |
| 1172 | // else the debug info would affect codegen. |
| 1173 | static bool IsEmptyBlock(MachineBasicBlock *MBB) { |
Benjamin Kramer | 6b56896 | 2015-06-23 14:47:29 +0000 | [diff] [blame] | 1174 | return MBB->getFirstNonDebugInstr() == MBB->end(); |
Dale Johannesen | 5ebe0c1 | 2010-03-10 05:45:47 +0000 | [diff] [blame] | 1175 | } |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 1176 | |
Dale Johannesen | e9b361b | 2010-03-10 19:57:56 +0000 | [diff] [blame] | 1177 | // Blocks with only debug info and branches should be considered the same |
| 1178 | // as blocks with only branches. |
| 1179 | static bool IsBranchOnlyBlock(MachineBasicBlock *MBB) { |
Benjamin Kramer | 6b56896 | 2015-06-23 14:47:29 +0000 | [diff] [blame] | 1180 | MachineBasicBlock::iterator I = MBB->getFirstNonDebugInstr(); |
| 1181 | assert(I != MBB->end() && "empty block!"); |
| 1182 | return I->isBranch(); |
Dale Johannesen | e9b361b | 2010-03-10 19:57:56 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Chris Lattner | 47ce261 | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 1185 | /// IsBetterFallthrough - Return true if it would be clearly better to |
| 1186 | /// fall-through to MBB1 than to fall through into MBB2. This has to return |
| 1187 | /// a strict ordering, returning true for both (MBB1,MBB2) and (MBB2,MBB1) will |
| 1188 | /// result in infinite loops. |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1189 | static bool IsBetterFallthrough(MachineBasicBlock *MBB1, |
Chris Lattner | a98c679 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 1190 | MachineBasicBlock *MBB2) { |
Chris Lattner | 7acdc17f | 2006-11-18 21:30:35 +0000 | [diff] [blame] | 1191 | // Right now, we use a simple heuristic. If MBB2 ends with a call, and |
| 1192 | // MBB1 doesn't, we prefer to fall through into MBB1. This allows us to |
Chris Lattner | 47ce261 | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 1193 | // optimize branches that branch to either a return block or an assert block |
| 1194 | // into a fallthrough to the return. |
Benjamin Kramer | 6b56896 | 2015-06-23 14:47:29 +0000 | [diff] [blame] | 1195 | MachineBasicBlock::iterator MBB1I = MBB1->getLastNonDebugInstr(); |
| 1196 | MachineBasicBlock::iterator MBB2I = MBB2->getLastNonDebugInstr(); |
| 1197 | if (MBB1I == MBB1->end() || MBB2I == MBB2->end()) |
| 1198 | return false; |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1199 | |
Christopher Lamb | d202e03 | 2007-12-10 07:24:06 +0000 | [diff] [blame] | 1200 | // If there is a clear successor ordering we make sure that one block |
| 1201 | // will fall through to the next |
| 1202 | if (MBB1->isSuccessor(MBB2)) return true; |
| 1203 | if (MBB2->isSuccessor(MBB1)) return false; |
Chris Lattner | 47ce261 | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 1204 | |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 1205 | return MBB2I->isCall() && !MBB1I->isCall(); |
Chris Lattner | 47ce261 | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Bill Wendling | 7c5dcb6 | 2012-03-07 08:49:42 +0000 | [diff] [blame] | 1208 | /// getBranchDebugLoc - Find and return, if any, the DebugLoc of the branch |
Benjamin Kramer | 6b56896 | 2015-06-23 14:47:29 +0000 | [diff] [blame] | 1209 | /// instructions on the block. |
Bill Wendling | 7c5dcb6 | 2012-03-07 08:49:42 +0000 | [diff] [blame] | 1210 | static DebugLoc getBranchDebugLoc(MachineBasicBlock &MBB) { |
Benjamin Kramer | 6b56896 | 2015-06-23 14:47:29 +0000 | [diff] [blame] | 1211 | MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr(); |
| 1212 | if (I != MBB.end() && I->isBranch()) |
Bill Wendling | 7c5dcb6 | 2012-03-07 08:49:42 +0000 | [diff] [blame] | 1213 | return I->getDebugLoc(); |
| 1214 | return DebugLoc(); |
| 1215 | } |
| 1216 | |
Chris Lattner | 3218e0e | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 1217 | /// OptimizeBlock - Analyze and optimize control flow related to the specified |
| 1218 | /// block. This is never called on the entry block. |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 1219 | bool BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) { |
| 1220 | bool MadeChange = false; |
Dan Gohman | f4141f1 | 2009-11-11 18:18:34 +0000 | [diff] [blame] | 1221 | MachineFunction &MF = *MBB->getParent(); |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1222 | ReoptimizeBlock: |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 1223 | |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 1224 | MachineFunction::iterator FallThrough = MBB->getIterator(); |
Chris Lattner | ceb51d8 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 1225 | ++FallThrough; |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1226 | |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 1227 | // Make sure MBB and FallThrough belong to the same funclet. |
| 1228 | bool SameFunclet = true; |
| 1229 | if (!FuncletMembership.empty() && FallThrough != MF.end()) { |
| 1230 | auto MBBFunclet = FuncletMembership.find(MBB); |
| 1231 | assert(MBBFunclet != FuncletMembership.end()); |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 1232 | auto FallThroughFunclet = FuncletMembership.find(&*FallThrough); |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 1233 | assert(FallThroughFunclet != FuncletMembership.end()); |
| 1234 | SameFunclet = MBBFunclet->second == FallThroughFunclet->second; |
| 1235 | } |
| 1236 | |
Chris Lattner | 3e8e57c | 2006-10-13 20:43:10 +0000 | [diff] [blame] | 1237 | // If this block is empty, make everyone use its fall-through, not the block |
Dale Johannesen | 1a401e6 | 2007-05-31 21:54:00 +0000 | [diff] [blame] | 1238 | // explicitly. Landing pads should not do this since the landing-pad table |
Dan Gohman | 6499790 | 2009-10-30 02:13:27 +0000 | [diff] [blame] | 1239 | // points to this block. Blocks with their addresses taken shouldn't be |
| 1240 | // optimized away. |
David Majnemer | 1619355 | 2015-10-04 02:22:52 +0000 | [diff] [blame] | 1241 | if (IsEmptyBlock(MBB) && !MBB->isEHPad() && !MBB->hasAddressTaken() && |
| 1242 | SameFunclet) { |
Chris Lattner | 4fe01c4 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1243 | // Dead block? Leave for cleanup later. |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 1244 | if (MBB->pred_empty()) return MadeChange; |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1245 | |
Dan Gohman | f4141f1 | 2009-11-11 18:18:34 +0000 | [diff] [blame] | 1246 | if (FallThrough == MF.end()) { |
Chris Lattner | 56c9d25 | 2006-10-17 17:13:52 +0000 | [diff] [blame] | 1247 | // TODO: Simplify preds to not branch here if possible! |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 1248 | } else if (FallThrough->isEHPad()) { |
Pete Cooper | 4d8d2ec | 2015-04-30 18:58:23 +0000 | [diff] [blame] | 1249 | // Don't rewrite to a landing pad fallthough. That could lead to the case |
| 1250 | // where a BB jumps to more than one landing pad. |
| 1251 | // TODO: Is it ever worth rewriting predecessors which don't already |
| 1252 | // jump to a landing pad, and so can safely jump to the fallthrough? |
Kyle Butt | 267164d | 2016-06-24 18:16:36 +0000 | [diff] [blame] | 1253 | } else if (MBB->isSuccessor(&*FallThrough)) { |
Chris Lattner | 56c9d25 | 2006-10-17 17:13:52 +0000 | [diff] [blame] | 1254 | // Rewrite all predecessors of the old block to go to the fallthrough |
| 1255 | // instead. |
Jim Laskey | 9df1a1d | 2007-02-22 16:39:03 +0000 | [diff] [blame] | 1256 | while (!MBB->pred_empty()) { |
Chris Lattner | 3218e0e | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 1257 | MachineBasicBlock *Pred = *(MBB->pred_end()-1); |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 1258 | Pred->ReplaceUsesOfBlockWith(MBB, &*FallThrough); |
Chris Lattner | 3218e0e | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 1259 | } |
Chris Lattner | 56c9d25 | 2006-10-17 17:13:52 +0000 | [diff] [blame] | 1260 | // If MBB was the target of a jump table, update jump tables to go to the |
| 1261 | // fallthrough instead. |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 1262 | if (MachineJumpTableInfo *MJTI = MF.getJumpTableInfo()) |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 1263 | MJTI->ReplaceMBBInJumpTables(MBB, &*FallThrough); |
Chris Lattner | 3218e0e | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 1264 | MadeChange = true; |
Chris Lattner | 25e48dd | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 1265 | } |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 1266 | return MadeChange; |
Chris Lattner | 25e48dd | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Chris Lattner | 3218e0e | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 1269 | // Check to see if we can simplify the terminator of the block before this |
| 1270 | // one. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1271 | MachineBasicBlock &PrevBB = *std::prev(MachineFunction::iterator(MBB)); |
Chris Lattner | bca3e29 | 2006-10-17 18:16:40 +0000 | [diff] [blame] | 1272 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1273 | MachineBasicBlock *PriorTBB = nullptr, *PriorFBB = nullptr; |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1274 | SmallVector<MachineOperand, 4> PriorCond; |
Chris Lattner | 504eeda | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1275 | bool PriorUnAnalyzable = |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1276 | TII->analyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, true); |
Chris Lattner | 4fe01c4 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1277 | if (!PriorUnAnalyzable) { |
| 1278 | // If the CFG for the prior block has extra edges, remove them. |
Evan Cheng | 2afd702 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1279 | MadeChange |= PrevBB.CorrectExtraCFGEdges(PriorTBB, PriorFBB, |
| 1280 | !PriorCond.empty()); |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1281 | |
Chris Lattner | 3218e0e | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 1282 | // If the previous branch is conditional and both conditions go to the same |
Chris Lattner | 3ca5218 | 2006-10-21 05:43:30 +0000 | [diff] [blame] | 1283 | // destination, remove the branch, replacing it with an unconditional one or |
| 1284 | // a fall-through. |
Chris Lattner | 3218e0e | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 1285 | if (PriorTBB && PriorTBB == PriorFBB) { |
Bill Wendling | 7c5dcb6 | 2012-03-07 08:49:42 +0000 | [diff] [blame] | 1286 | DebugLoc dl = getBranchDebugLoc(PrevBB); |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1287 | TII->removeBranch(PrevBB); |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1288 | PriorCond.clear(); |
Chris Lattner | ceb51d8 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 1289 | if (PriorTBB != MBB) |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 1290 | TII->insertBranch(PrevBB, PriorTBB, nullptr, PriorCond, dl); |
Chris Lattner | 3218e0e | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 1291 | MadeChange = true; |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 1292 | ++NumBranchOpts; |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1293 | goto ReoptimizeBlock; |
Chris Lattner | 3218e0e | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 1294 | } |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1295 | |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1296 | // If the previous block unconditionally falls through to this block and |
| 1297 | // this block has no other predecessors, move the contents of this block |
| 1298 | // into the prior block. This doesn't usually happen when SimplifyCFG |
Bob Wilson | 724d8a4 | 2009-11-17 17:40:31 +0000 | [diff] [blame] | 1299 | // has been used, but it can happen if tail merging splits a fall-through |
| 1300 | // predecessor of a block. |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1301 | // This has to check PrevBB->succ_size() because EH edges are ignored by |
| 1302 | // AnalyzeBranch. |
| 1303 | if (PriorCond.empty() && !PriorTBB && MBB->pred_size() == 1 && |
| 1304 | PrevBB.succ_size() == 1 && |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 1305 | !MBB->hasAddressTaken() && !MBB->isEHPad()) { |
David Greene | d60abbf | 2009-12-24 00:34:21 +0000 | [diff] [blame] | 1306 | DEBUG(dbgs() << "\nMerging into block: " << PrevBB |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1307 | << "From MBB: " << *MBB); |
Devang Patel | cdec114 | 2011-05-26 21:49:28 +0000 | [diff] [blame] | 1308 | // Remove redundant DBG_VALUEs first. |
Devang Patel | 42ddaa1 | 2011-05-26 21:47:59 +0000 | [diff] [blame] | 1309 | if (PrevBB.begin() != PrevBB.end()) { |
| 1310 | MachineBasicBlock::iterator PrevBBIter = PrevBB.end(); |
| 1311 | --PrevBBIter; |
| 1312 | MachineBasicBlock::iterator MBBIter = MBB->begin(); |
Andrew Trick | 9e76199 | 2012-02-08 21:22:43 +0000 | [diff] [blame] | 1313 | // Check if DBG_VALUE at the end of PrevBB is identical to the |
Devang Patel | cdec114 | 2011-05-26 21:49:28 +0000 | [diff] [blame] | 1314 | // DBG_VALUE at the beginning of MBB. |
Devang Patel | 42ddaa1 | 2011-05-26 21:47:59 +0000 | [diff] [blame] | 1315 | while (PrevBBIter != PrevBB.begin() && MBBIter != MBB->end() |
| 1316 | && PrevBBIter->isDebugValue() && MBBIter->isDebugValue()) { |
Duncan P. N. Exon Smith | fd8cc23 | 2016-02-27 20:01:33 +0000 | [diff] [blame] | 1317 | if (!MBBIter->isIdenticalTo(*PrevBBIter)) |
Devang Patel | 42ddaa1 | 2011-05-26 21:47:59 +0000 | [diff] [blame] | 1318 | break; |
Duncan P. N. Exon Smith | ebcce78 | 2016-02-27 20:27:44 +0000 | [diff] [blame] | 1319 | MachineInstr &DuplicateDbg = *MBBIter; |
Devang Patel | 42ddaa1 | 2011-05-26 21:47:59 +0000 | [diff] [blame] | 1320 | ++MBBIter; -- PrevBBIter; |
Duncan P. N. Exon Smith | ebcce78 | 2016-02-27 20:27:44 +0000 | [diff] [blame] | 1321 | DuplicateDbg.eraseFromParent(); |
Devang Patel | 42ddaa1 | 2011-05-26 21:47:59 +0000 | [diff] [blame] | 1322 | } |
| 1323 | } |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1324 | PrevBB.splice(PrevBB.end(), MBB, MBB->begin(), MBB->end()); |
Chad Rosier | 5dfe6da | 2012-02-22 17:25:00 +0000 | [diff] [blame] | 1325 | PrevBB.removeSuccessor(PrevBB.succ_begin()); |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1326 | assert(PrevBB.succ_empty()); |
| 1327 | PrevBB.transferSuccessors(MBB); |
| 1328 | MadeChange = true; |
| 1329 | return MadeChange; |
| 1330 | } |
Bob Wilson | 699f5b9 | 2009-11-16 17:56:13 +0000 | [diff] [blame] | 1331 | |
Chris Lattner | 3218e0e | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 1332 | // If the previous branch *only* branches to *this* block (conditional or |
| 1333 | // not) remove the branch. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1334 | if (PriorTBB == MBB && !PriorFBB) { |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1335 | TII->removeBranch(PrevBB); |
Chris Lattner | 3218e0e | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 1336 | MadeChange = true; |
Chris Lattner | 60c9d4d | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 1337 | ++NumBranchOpts; |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1338 | goto ReoptimizeBlock; |
Chris Lattner | 3218e0e | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 1339 | } |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1340 | |
Chris Lattner | 3ca5218 | 2006-10-21 05:43:30 +0000 | [diff] [blame] | 1341 | // If the prior block branches somewhere else on the condition and here if |
| 1342 | // the condition is false, remove the uncond second branch. |
Chris Lattner | ceb51d8 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 1343 | if (PriorFBB == MBB) { |
Bill Wendling | 7c5dcb6 | 2012-03-07 08:49:42 +0000 | [diff] [blame] | 1344 | DebugLoc dl = getBranchDebugLoc(PrevBB); |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1345 | TII->removeBranch(PrevBB); |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 1346 | TII->insertBranch(PrevBB, PriorTBB, nullptr, PriorCond, dl); |
Chris Lattner | 3ca5218 | 2006-10-21 05:43:30 +0000 | [diff] [blame] | 1347 | MadeChange = true; |
| 1348 | ++NumBranchOpts; |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1349 | goto ReoptimizeBlock; |
Chris Lattner | 3ca5218 | 2006-10-21 05:43:30 +0000 | [diff] [blame] | 1350 | } |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1351 | |
Chris Lattner | 28f17f4 | 2006-10-21 05:54:00 +0000 | [diff] [blame] | 1352 | // If the prior block branches here on true and somewhere else on false, and |
| 1353 | // if the branch condition is reversible, reverse the branch to create a |
| 1354 | // fall-through. |
Chris Lattner | ceb51d8 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 1355 | if (PriorTBB == MBB) { |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1356 | SmallVector<MachineOperand, 4> NewPriorCond(PriorCond); |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1357 | if (!TII->reverseBranchCondition(NewPriorCond)) { |
Bill Wendling | 7c5dcb6 | 2012-03-07 08:49:42 +0000 | [diff] [blame] | 1358 | DebugLoc dl = getBranchDebugLoc(PrevBB); |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1359 | TII->removeBranch(PrevBB); |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 1360 | TII->insertBranch(PrevBB, PriorFBB, nullptr, NewPriorCond, dl); |
Chris Lattner | 28f17f4 | 2006-10-21 05:54:00 +0000 | [diff] [blame] | 1361 | MadeChange = true; |
| 1362 | ++NumBranchOpts; |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1363 | goto ReoptimizeBlock; |
Chris Lattner | 28f17f4 | 2006-10-21 05:54:00 +0000 | [diff] [blame] | 1364 | } |
| 1365 | } |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1366 | |
Dan Gohman | ff97acd | 2009-10-22 00:03:58 +0000 | [diff] [blame] | 1367 | // If this block has no successors (e.g. it is a return block or ends with |
| 1368 | // a call to a no-return function like abort or __cxa_throw) and if the pred |
| 1369 | // falls through into this block, and if it would otherwise fall through |
| 1370 | // into the block after this, move this block to the end of the function. |
Chris Lattner | 7acdc17f | 2006-11-18 21:30:35 +0000 | [diff] [blame] | 1371 | // |
Chris Lattner | 47ce261 | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 1372 | // We consider it more likely that execution will stay in the function (e.g. |
| 1373 | // due to loops) than it is to exit it. This asserts in loops etc, moving |
| 1374 | // the assert condition out of the loop body. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1375 | if (MBB->succ_empty() && !PriorCond.empty() && !PriorFBB && |
Chris Lattner | 7acdc17f | 2006-11-18 21:30:35 +0000 | [diff] [blame] | 1376 | MachineFunction::iterator(PriorTBB) == FallThrough && |
Bob Wilson | 2d4ff12 | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 1377 | !MBB->canFallThrough()) { |
Chris Lattner | 56ec81f | 2006-11-18 21:56:39 +0000 | [diff] [blame] | 1378 | bool DoTransform = true; |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1379 | |
Chris Lattner | 47ce261 | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 1380 | // We have to be careful that the succs of PredBB aren't both no-successor |
| 1381 | // blocks. If neither have successors and if PredBB is the second from |
| 1382 | // last block in the function, we'd just keep swapping the two blocks for |
| 1383 | // last. Only do the swap if one is clearly better to fall through than |
| 1384 | // the other. |
Dan Gohman | f4141f1 | 2009-11-11 18:18:34 +0000 | [diff] [blame] | 1385 | if (FallThrough == --MF.end() && |
Chris Lattner | a98c679 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 1386 | !IsBetterFallthrough(PriorTBB, MBB)) |
Chris Lattner | 56ec81f | 2006-11-18 21:56:39 +0000 | [diff] [blame] | 1387 | DoTransform = false; |
| 1388 | |
Chris Lattner | 56ec81f | 2006-11-18 21:56:39 +0000 | [diff] [blame] | 1389 | if (DoTransform) { |
Chris Lattner | 47ce261 | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 1390 | // Reverse the branch so we will fall through on the previous true cond. |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1391 | SmallVector<MachineOperand, 4> NewPriorCond(PriorCond); |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1392 | if (!TII->reverseBranchCondition(NewPriorCond)) { |
David Greene | d60abbf | 2009-12-24 00:34:21 +0000 | [diff] [blame] | 1393 | DEBUG(dbgs() << "\nMoving MBB: " << *MBB |
Bill Wendling | c3f05e8 | 2009-08-22 20:03:00 +0000 | [diff] [blame] | 1394 | << "To make fallthrough to: " << *PriorTBB << "\n"); |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1395 | |
Bill Wendling | 7c5dcb6 | 2012-03-07 08:49:42 +0000 | [diff] [blame] | 1396 | DebugLoc dl = getBranchDebugLoc(PrevBB); |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1397 | TII->removeBranch(PrevBB); |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 1398 | TII->insertBranch(PrevBB, MBB, nullptr, NewPriorCond, dl); |
Chris Lattner | 47ce261 | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 1399 | |
| 1400 | // Move this block to the end of the function. |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 1401 | MBB->moveAfter(&MF.back()); |
Chris Lattner | 47ce261 | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 1402 | MadeChange = true; |
| 1403 | ++NumBranchOpts; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 1404 | return MadeChange; |
Chris Lattner | 47ce261 | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 1405 | } |
| 1406 | } |
| 1407 | } |
Chris Lattner | 3218e0e | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 1408 | } |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1409 | |
Hans Wennborg | 75e25f6 | 2016-09-07 17:52:14 +0000 | [diff] [blame] | 1410 | if (!IsEmptyBlock(MBB) && MBB->pred_size() == 1 && |
| 1411 | MF.getFunction()->optForSize()) { |
| 1412 | // Changing "Jcc foo; foo: jmp bar;" into "Jcc bar;" might change the branch |
| 1413 | // direction, thereby defeating careful block placement and regressing |
| 1414 | // performance. Therefore, only consider this for optsize functions. |
| 1415 | MachineInstr &TailCall = *MBB->getFirstNonDebugInstr(); |
| 1416 | if (TII->isUnconditionalTailCall(TailCall)) { |
| 1417 | MachineBasicBlock *Pred = *MBB->pred_begin(); |
| 1418 | MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr; |
| 1419 | SmallVector<MachineOperand, 4> PredCond; |
| 1420 | bool PredAnalyzable = |
| 1421 | !TII->analyzeBranch(*Pred, PredTBB, PredFBB, PredCond, true); |
| 1422 | |
| 1423 | if (PredAnalyzable && !PredCond.empty() && PredTBB == MBB) { |
| 1424 | // The predecessor has a conditional branch to this block which consists |
| 1425 | // of only a tail call. Try to fold the tail call into the conditional |
| 1426 | // branch. |
| 1427 | if (TII->canMakeTailCallConditional(PredCond, TailCall)) { |
| 1428 | // TODO: It would be nice if analyzeBranch() could provide a pointer |
| 1429 | // to the branch insturction so replaceBranchWithTailCall() doesn't |
| 1430 | // have to search for it. |
| 1431 | TII->replaceBranchWithTailCall(*Pred, PredCond, TailCall); |
| 1432 | ++NumTailCalls; |
| 1433 | Pred->removeSuccessor(MBB); |
| 1434 | MadeChange = true; |
| 1435 | return MadeChange; |
| 1436 | } |
| 1437 | } |
| 1438 | // If the predecessor is falling through to this block, we could reverse |
| 1439 | // the branch condition and fold the tail call into that. However, after |
| 1440 | // that we might have to re-arrange the CFG to fall through to the other |
| 1441 | // block and there is a high risk of regressing code size rather than |
| 1442 | // improving it. |
| 1443 | } |
| 1444 | } |
| 1445 | |
Chris Lattner | 4fe01c4 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1446 | // Analyze the branch in the current block. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1447 | MachineBasicBlock *CurTBB = nullptr, *CurFBB = nullptr; |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1448 | SmallVector<MachineOperand, 4> CurCond; |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1449 | bool CurUnAnalyzable = |
| 1450 | TII->analyzeBranch(*MBB, CurTBB, CurFBB, CurCond, true); |
Chris Lattner | 504eeda | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1451 | if (!CurUnAnalyzable) { |
Chris Lattner | 4fe01c4 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1452 | // If the CFG for the prior block has extra edges, remove them. |
Evan Cheng | 2afd702 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1453 | MadeChange |= MBB->CorrectExtraCFGEdges(CurTBB, CurFBB, !CurCond.empty()); |
Chris Lattner | 3e8e57c | 2006-10-13 20:43:10 +0000 | [diff] [blame] | 1454 | |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1455 | // If this is a two-way branch, and the FBB branches to this block, reverse |
Chris Lattner | bf3b57f | 2006-11-08 01:03:21 +0000 | [diff] [blame] | 1456 | // the condition so the single-basic-block loop is faster. Instead of: |
| 1457 | // Loop: xxx; jcc Out; jmp Loop |
| 1458 | // we want: |
| 1459 | // Loop: xxx; jncc Loop; jmp Out |
| 1460 | if (CurTBB && CurFBB && CurFBB == MBB && CurTBB != MBB) { |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1461 | SmallVector<MachineOperand, 4> NewCond(CurCond); |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1462 | if (!TII->reverseBranchCondition(NewCond)) { |
Bill Wendling | 7c5dcb6 | 2012-03-07 08:49:42 +0000 | [diff] [blame] | 1463 | DebugLoc dl = getBranchDebugLoc(*MBB); |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1464 | TII->removeBranch(*MBB); |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 1465 | TII->insertBranch(*MBB, CurFBB, CurTBB, NewCond, dl); |
Chris Lattner | bf3b57f | 2006-11-08 01:03:21 +0000 | [diff] [blame] | 1466 | MadeChange = true; |
| 1467 | ++NumBranchOpts; |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1468 | goto ReoptimizeBlock; |
Chris Lattner | bf3b57f | 2006-11-08 01:03:21 +0000 | [diff] [blame] | 1469 | } |
| 1470 | } |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1471 | |
Chris Lattner | 4fe01c4 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1472 | // If this branch is the only thing in its block, see if we can forward |
| 1473 | // other blocks across it. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1474 | if (CurTBB && CurCond.empty() && !CurFBB && |
Dale Johannesen | e9b361b | 2010-03-10 19:57:56 +0000 | [diff] [blame] | 1475 | IsBranchOnlyBlock(MBB) && CurTBB != MBB && |
Reid Kleckner | b9204a5 | 2015-11-11 23:09:31 +0000 | [diff] [blame] | 1476 | !MBB->hasAddressTaken() && !MBB->isEHPad()) { |
Bill Wendling | 7c5dcb6 | 2012-03-07 08:49:42 +0000 | [diff] [blame] | 1477 | DebugLoc dl = getBranchDebugLoc(*MBB); |
Chris Lattner | 4fe01c4 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1478 | // This block may contain just an unconditional branch. Because there can |
| 1479 | // be 'non-branch terminators' in the block, try removing the branch and |
| 1480 | // then seeing if the block is empty. |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1481 | TII->removeBranch(*MBB); |
Dale Johannesen | 5ebe0c1 | 2010-03-10 05:45:47 +0000 | [diff] [blame] | 1482 | // If the only things remaining in the block are debug info, remove these |
| 1483 | // as well, so this will behave the same as an empty block in non-debug |
| 1484 | // mode. |
Benjamin Kramer | 6b56896 | 2015-06-23 14:47:29 +0000 | [diff] [blame] | 1485 | if (IsEmptyBlock(MBB)) { |
| 1486 | // Make the block empty, losing the debug info (we could probably |
| 1487 | // improve this in some cases.) |
| 1488 | MBB->erase(MBB->begin(), MBB->end()); |
Dale Johannesen | 5ebe0c1 | 2010-03-10 05:45:47 +0000 | [diff] [blame] | 1489 | } |
Chris Lattner | 4fe01c4 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1490 | // If this block is just an unconditional branch to CurTBB, we can |
| 1491 | // usually completely eliminate the block. The only case we cannot |
| 1492 | // completely eliminate the block is when the block before this one |
| 1493 | // falls through into MBB and we can't understand the prior block's branch |
| 1494 | // condition. |
Chris Lattner | af83838 | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1495 | if (MBB->empty()) { |
Dan Gohman | 047a767 | 2009-12-05 00:44:40 +0000 | [diff] [blame] | 1496 | bool PredHasNoFallThrough = !PrevBB.canFallThrough(); |
Chris Lattner | af83838 | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1497 | if (PredHasNoFallThrough || !PriorUnAnalyzable || |
| 1498 | !PrevBB.isSuccessor(MBB)) { |
| 1499 | // If the prior block falls through into us, turn it into an |
| 1500 | // explicit branch to us to make updates simpler. |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1501 | if (!PredHasNoFallThrough && PrevBB.isSuccessor(MBB) && |
Chris Lattner | af83838 | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1502 | PriorTBB != MBB && PriorFBB != MBB) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1503 | if (!PriorTBB) { |
| 1504 | assert(PriorCond.empty() && !PriorFBB && |
Chris Lattner | c07657f | 2006-10-28 18:34:47 +0000 | [diff] [blame] | 1505 | "Bad branch analysis"); |
Chris Lattner | af83838 | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1506 | PriorTBB = MBB; |
| 1507 | } else { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1508 | assert(!PriorFBB && "Machine CFG out of date!"); |
Chris Lattner | af83838 | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1509 | PriorFBB = MBB; |
| 1510 | } |
Bill Wendling | 7c5dcb6 | 2012-03-07 08:49:42 +0000 | [diff] [blame] | 1511 | DebugLoc pdl = getBranchDebugLoc(PrevBB); |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1512 | TII->removeBranch(PrevBB); |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 1513 | TII->insertBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, pdl); |
Chris Lattner | 4fe01c4 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1514 | } |
Chris Lattner | 4fe01c4 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1515 | |
Chris Lattner | af83838 | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1516 | // Iterate through all the predecessors, revectoring each in-turn. |
David Greene | 451d1a6 | 2007-06-29 02:45:24 +0000 | [diff] [blame] | 1517 | size_t PI = 0; |
Chris Lattner | af83838 | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1518 | bool DidChange = false; |
| 1519 | bool HasBranchToSelf = false; |
David Greene | 451d1a6 | 2007-06-29 02:45:24 +0000 | [diff] [blame] | 1520 | while(PI != MBB->pred_size()) { |
| 1521 | MachineBasicBlock *PMBB = *(MBB->pred_begin() + PI); |
| 1522 | if (PMBB == MBB) { |
Chris Lattner | af83838 | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1523 | // If this block has an uncond branch to itself, leave it. |
| 1524 | ++PI; |
| 1525 | HasBranchToSelf = true; |
| 1526 | } else { |
| 1527 | DidChange = true; |
David Greene | 451d1a6 | 2007-06-29 02:45:24 +0000 | [diff] [blame] | 1528 | PMBB->ReplaceUsesOfBlockWith(MBB, CurTBB); |
Dale Johannesen | b571463 | 2009-05-11 21:54:13 +0000 | [diff] [blame] | 1529 | // If this change resulted in PMBB ending in a conditional |
| 1530 | // branch where both conditions go to the same destination, |
| 1531 | // change this to an unconditional branch (and fix the CFG). |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1532 | MachineBasicBlock *NewCurTBB = nullptr, *NewCurFBB = nullptr; |
Dale Johannesen | b571463 | 2009-05-11 21:54:13 +0000 | [diff] [blame] | 1533 | SmallVector<MachineOperand, 4> NewCurCond; |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1534 | bool NewCurUnAnalyzable = TII->analyzeBranch( |
| 1535 | *PMBB, NewCurTBB, NewCurFBB, NewCurCond, true); |
Dale Johannesen | b571463 | 2009-05-11 21:54:13 +0000 | [diff] [blame] | 1536 | if (!NewCurUnAnalyzable && NewCurTBB && NewCurTBB == NewCurFBB) { |
Bill Wendling | 7c5dcb6 | 2012-03-07 08:49:42 +0000 | [diff] [blame] | 1537 | DebugLoc pdl = getBranchDebugLoc(*PMBB); |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 1538 | TII->removeBranch(*PMBB); |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1539 | NewCurCond.clear(); |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 1540 | TII->insertBranch(*PMBB, NewCurTBB, nullptr, NewCurCond, pdl); |
Dale Johannesen | b571463 | 2009-05-11 21:54:13 +0000 | [diff] [blame] | 1541 | MadeChange = true; |
| 1542 | ++NumBranchOpts; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1543 | PMBB->CorrectExtraCFGEdges(NewCurTBB, nullptr, false); |
Dale Johannesen | b571463 | 2009-05-11 21:54:13 +0000 | [diff] [blame] | 1544 | } |
Chris Lattner | af83838 | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1545 | } |
Chris Lattner | 9f5a129 | 2006-10-21 06:11:43 +0000 | [diff] [blame] | 1546 | } |
Chris Lattner | 4fe01c4 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1547 | |
Chris Lattner | af83838 | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1548 | // Change any jumptables to go to the new MBB. |
Chris Lattner | b6db2c6 | 2010-01-25 23:26:13 +0000 | [diff] [blame] | 1549 | if (MachineJumpTableInfo *MJTI = MF.getJumpTableInfo()) |
| 1550 | MJTI->ReplaceMBBInJumpTables(MBB, CurTBB); |
Chris Lattner | af83838 | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1551 | if (DidChange) { |
| 1552 | ++NumBranchOpts; |
| 1553 | MadeChange = true; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 1554 | if (!HasBranchToSelf) return MadeChange; |
Chris Lattner | af83838 | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1555 | } |
Chris Lattner | 9f5a129 | 2006-10-21 06:11:43 +0000 | [diff] [blame] | 1556 | } |
Chris Lattner | 3e8e57c | 2006-10-13 20:43:10 +0000 | [diff] [blame] | 1557 | } |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1558 | |
Chris Lattner | 4fe01c4 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1559 | // Add the branch back if the block is more than just an uncond branch. |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 1560 | TII->insertBranch(*MBB, CurTBB, nullptr, CurCond, dl); |
Chris Lattner | 25e48dd | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 1561 | } |
Chris Lattner | 504eeda | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Bill Wendling | e8a525a | 2009-12-15 00:39:24 +0000 | [diff] [blame] | 1564 | // If the prior block doesn't fall through into this block, and if this |
| 1565 | // block doesn't fall through into some other block, see if we can find a |
| 1566 | // place to move this block where a fall-through will happen. |
| 1567 | if (!PrevBB.canFallThrough()) { |
| 1568 | |
Bob Wilson | bd22f19 | 2009-11-17 17:06:18 +0000 | [diff] [blame] | 1569 | // Now we know that there was no fall-through into this block, check to |
| 1570 | // see if it has a fall-through into its successor. |
Bob Wilson | 2d4ff12 | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 1571 | bool CurFallsThru = MBB->canFallThrough(); |
Bob Wilson | bd22f19 | 2009-11-17 17:06:18 +0000 | [diff] [blame] | 1572 | |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 1573 | if (!MBB->isEHPad()) { |
Jim Laskey | 2dc5245 | 2007-02-21 22:42:20 +0000 | [diff] [blame] | 1574 | // Check all the predecessors of this block. If one of them has no fall |
| 1575 | // throughs, move this block right after it. |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 1576 | for (MachineBasicBlock *PredBB : MBB->predecessors()) { |
Jim Laskey | 2dc5245 | 2007-02-21 22:42:20 +0000 | [diff] [blame] | 1577 | // Analyze the branch at the end of the pred. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1578 | MachineBasicBlock *PredTBB = nullptr, *PredFBB = nullptr; |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1579 | SmallVector<MachineOperand, 4> PredCond; |
Bill Wendling | e8a525a | 2009-12-15 00:39:24 +0000 | [diff] [blame] | 1580 | if (PredBB != MBB && !PredBB->canFallThrough() && |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1581 | !TII->analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond, true) && |
| 1582 | (!CurFallsThru || !CurTBB || !CurFBB) && |
| 1583 | (!CurFallsThru || MBB->getNumber() >= PredBB->getNumber())) { |
Bill Wendling | e8a525a | 2009-12-15 00:39:24 +0000 | [diff] [blame] | 1584 | // If the current block doesn't fall through, just move it. |
| 1585 | // If the current block can fall through and does not end with a |
| 1586 | // conditional branch, we need to append an unconditional jump to |
| 1587 | // the (current) next block. To avoid a possible compile-time |
| 1588 | // infinite loop, move blocks only backward in this case. |
| 1589 | // Also, if there are already 2 branches here, we cannot add a third; |
| 1590 | // this means we have the case |
| 1591 | // Bcc next |
| 1592 | // B elsewhere |
| 1593 | // next: |
Jim Laskey | 2dc5245 | 2007-02-21 22:42:20 +0000 | [diff] [blame] | 1594 | if (CurFallsThru) { |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 1595 | MachineBasicBlock *NextBB = &*std::next(MBB->getIterator()); |
Jim Laskey | 2dc5245 | 2007-02-21 22:42:20 +0000 | [diff] [blame] | 1596 | CurCond.clear(); |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 1597 | TII->insertBranch(*MBB, NextBB, nullptr, CurCond, DebugLoc()); |
Jim Laskey | 2dc5245 | 2007-02-21 22:42:20 +0000 | [diff] [blame] | 1598 | } |
| 1599 | MBB->moveAfter(PredBB); |
| 1600 | MadeChange = true; |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1601 | goto ReoptimizeBlock; |
Chris Lattner | ceb51d8 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 1602 | } |
| 1603 | } |
Dale Johannesen | 12920dd | 2007-02-17 00:44:34 +0000 | [diff] [blame] | 1604 | } |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1605 | |
Dale Johannesen | 12920dd | 2007-02-17 00:44:34 +0000 | [diff] [blame] | 1606 | if (!CurFallsThru) { |
Chris Lattner | 504eeda | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1607 | // Check all successors to see if we can move this block before it. |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 1608 | for (MachineBasicBlock *SuccBB : MBB->successors()) { |
Chris Lattner | 504eeda | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1609 | // Analyze the branch at the end of the block before the succ. |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 1610 | MachineFunction::iterator SuccPrev = --SuccBB->getIterator(); |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1611 | |
Chris Lattner | 4dbbace | 2007-04-30 23:35:00 +0000 | [diff] [blame] | 1612 | // If this block doesn't already fall-through to that successor, and if |
| 1613 | // the succ doesn't already have a block that can fall through into it, |
| 1614 | // and if the successor isn't an EH destination, we can arrange for the |
| 1615 | // fallthrough to happen. |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1616 | if (SuccBB != MBB && &*SuccPrev != MBB && |
Bob Wilson | 2d4ff12 | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 1617 | !SuccPrev->canFallThrough() && !CurUnAnalyzable && |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 1618 | !SuccBB->isEHPad()) { |
Chris Lattner | 504eeda | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1619 | MBB->moveBefore(SuccBB); |
| 1620 | MadeChange = true; |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1621 | goto ReoptimizeBlock; |
Chris Lattner | 504eeda | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1622 | } |
| 1623 | } |
Dan Gohman | c86b5a1 | 2009-11-11 18:38:14 +0000 | [diff] [blame] | 1624 | |
Chris Lattner | 504eeda | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1625 | // Okay, there is no really great place to put this block. If, however, |
| 1626 | // the block before this one would be a fall-through if this block were |
Andrew Kaylor | ff6a1ed | 2016-12-12 23:05:38 +0000 | [diff] [blame^] | 1627 | // removed, move this block to the end of the function. There is no real |
| 1628 | // advantage in "falling through" to an EH block, so we don't want to |
| 1629 | // perform this transformation for that case. |
| 1630 | // |
| 1631 | // Also, Windows EH introduced the possibility of an arbitrary number of |
| 1632 | // successors to a given block. The analyzeBranch call does not consider |
| 1633 | // exception handling and so we can get in a state where a block |
| 1634 | // containing a call is followed by multiple EH blocks that would be |
| 1635 | // rotated infinitely at the end of the function if the transformation |
| 1636 | // below were performed for EH "FallThrough" blocks. Therefore, even if |
| 1637 | // that appears not to be happening anymore, we should assume that it is |
| 1638 | // possible and not remove the "!FallThrough()->isEHPad" condition below. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1639 | MachineBasicBlock *PrevTBB = nullptr, *PrevFBB = nullptr; |
Dan Gohman | 64b5d0f | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 1640 | SmallVector<MachineOperand, 4> PrevCond; |
Dan Gohman | f4141f1 | 2009-11-11 18:18:34 +0000 | [diff] [blame] | 1641 | if (FallThrough != MF.end() && |
Andrew Kaylor | ff6a1ed | 2016-12-12 23:05:38 +0000 | [diff] [blame^] | 1642 | !FallThrough->isEHPad() && |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1643 | !TII->analyzeBranch(PrevBB, PrevTBB, PrevFBB, PrevCond, true) && |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 1644 | PrevBB.isSuccessor(&*FallThrough)) { |
| 1645 | MBB->moveAfter(&MF.back()); |
Chris Lattner | 504eeda | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1646 | MadeChange = true; |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 1647 | return MadeChange; |
Chris Lattner | 504eeda | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1648 | } |
Chris Lattner | ceb51d8 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 1649 | } |
Chris Lattner | 25e48dd | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 1650 | } |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 1651 | |
| 1652 | return MadeChange; |
Chris Lattner | 25e48dd | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 1653 | } |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1654 | |
| 1655 | //===----------------------------------------------------------------------===// |
| 1656 | // Hoist Common Code |
| 1657 | //===----------------------------------------------------------------------===// |
| 1658 | |
| 1659 | /// HoistCommonCode - Hoist common instruction sequences at the start of basic |
| 1660 | /// blocks to their common predecessor. |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1661 | bool BranchFolder::HoistCommonCode(MachineFunction &MF) { |
| 1662 | bool MadeChange = false; |
| 1663 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ) { |
Duncan P. N. Exon Smith | 980f8f2 | 2015-10-09 18:23:49 +0000 | [diff] [blame] | 1664 | MachineBasicBlock *MBB = &*I++; |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1665 | MadeChange |= HoistCommonCodeInSuccs(MBB); |
| 1666 | } |
| 1667 | |
| 1668 | return MadeChange; |
| 1669 | } |
| 1670 | |
| 1671 | /// findFalseBlock - BB has a fallthrough. Find its 'false' successor given |
| 1672 | /// its 'true' successor. |
| 1673 | static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB, |
| 1674 | MachineBasicBlock *TrueBB) { |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 1675 | for (MachineBasicBlock *SuccBB : BB->successors()) |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1676 | if (SuccBB != TrueBB) |
| 1677 | return SuccBB; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1678 | return nullptr; |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Jingyue Wu | 3a04dc6 | 2015-07-29 18:59:09 +0000 | [diff] [blame] | 1681 | template <class Container> |
| 1682 | static void addRegAndItsAliases(unsigned Reg, const TargetRegisterInfo *TRI, |
| 1683 | Container &Set) { |
| 1684 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 1685 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) |
| 1686 | Set.insert(*AI); |
| 1687 | } else { |
| 1688 | Set.insert(Reg); |
| 1689 | } |
| 1690 | } |
| 1691 | |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1692 | /// findHoistingInsertPosAndDeps - Find the location to move common instructions |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 1693 | /// in successors to. The location is usually just before the terminator, |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1694 | /// however if the terminator is a conditional branch and its previous |
| 1695 | /// instruction is the flag setting instruction, the previous instruction is |
| 1696 | /// the preferred location. This function also gathers uses and defs of the |
| 1697 | /// instructions from the insertion point to the end of the block. The data is |
| 1698 | /// used by HoistCommonCodeInSuccs to ensure safety. |
| 1699 | static |
| 1700 | MachineBasicBlock::iterator findHoistingInsertPosAndDeps(MachineBasicBlock *MBB, |
| 1701 | const TargetInstrInfo *TII, |
| 1702 | const TargetRegisterInfo *TRI, |
| 1703 | SmallSet<unsigned,4> &Uses, |
| 1704 | SmallSet<unsigned,4> &Defs) { |
| 1705 | MachineBasicBlock::iterator Loc = MBB->getFirstTerminator(); |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1706 | if (!TII->isUnpredicatedTerminator(*Loc)) |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1707 | return MBB->end(); |
| 1708 | |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 1709 | for (const MachineOperand &MO : Loc->operands()) { |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1710 | if (!MO.isReg()) |
| 1711 | continue; |
| 1712 | unsigned Reg = MO.getReg(); |
| 1713 | if (!Reg) |
| 1714 | continue; |
| 1715 | if (MO.isUse()) { |
Jingyue Wu | 3a04dc6 | 2015-07-29 18:59:09 +0000 | [diff] [blame] | 1716 | addRegAndItsAliases(Reg, TRI, Uses); |
Sasa Stankovic | 56c12e6 | 2014-06-05 13:42:48 +0000 | [diff] [blame] | 1717 | } else { |
| 1718 | if (!MO.isDead()) |
| 1719 | // Don't try to hoist code in the rare case the terminator defines a |
| 1720 | // register that is later used. |
| 1721 | return MBB->end(); |
| 1722 | |
| 1723 | // If the terminator defines a register, make sure we don't hoist |
| 1724 | // the instruction whose def might be clobbered by the terminator. |
Jingyue Wu | 3a04dc6 | 2015-07-29 18:59:09 +0000 | [diff] [blame] | 1725 | addRegAndItsAliases(Reg, TRI, Defs); |
Sasa Stankovic | 56c12e6 | 2014-06-05 13:42:48 +0000 | [diff] [blame] | 1726 | } |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | if (Uses.empty()) |
| 1730 | return Loc; |
| 1731 | if (Loc == MBB->begin()) |
| 1732 | return MBB->end(); |
| 1733 | |
| 1734 | // The terminator is probably a conditional branch, try not to separate the |
| 1735 | // branch from condition setting instruction. |
| 1736 | MachineBasicBlock::iterator PI = Loc; |
| 1737 | --PI; |
Ekaterina Romanova | b9aea93 | 2014-03-26 22:15:28 +0000 | [diff] [blame] | 1738 | while (PI != MBB->begin() && PI->isDebugValue()) |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1739 | --PI; |
| 1740 | |
| 1741 | bool IsDef = false; |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 1742 | for (const MachineOperand &MO : PI->operands()) { |
Jakob Stoklund Olesen | e9e30d0 | 2012-02-15 23:42:54 +0000 | [diff] [blame] | 1743 | // If PI has a regmask operand, it is probably a call. Separate away. |
| 1744 | if (MO.isRegMask()) |
| 1745 | return Loc; |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1746 | if (!MO.isReg() || MO.isUse()) |
| 1747 | continue; |
| 1748 | unsigned Reg = MO.getReg(); |
| 1749 | if (!Reg) |
| 1750 | continue; |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 1751 | if (Uses.count(Reg)) { |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1752 | IsDef = true; |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 1753 | break; |
| 1754 | } |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1755 | } |
| 1756 | if (!IsDef) |
| 1757 | // The condition setting instruction is not just before the conditional |
| 1758 | // branch. |
| 1759 | return Loc; |
| 1760 | |
| 1761 | // Be conservative, don't insert instruction above something that may have |
| 1762 | // side-effects. And since it's potentially bad to separate flag setting |
| 1763 | // instruction from the conditional branch, just abort the optimization |
| 1764 | // completely. |
| 1765 | // Also avoid moving code above predicated instruction since it's hard to |
| 1766 | // reason about register liveness with predicated instruction. |
| 1767 | bool DontMoveAcrossStore = true; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1768 | if (!PI->isSafeToMove(nullptr, DontMoveAcrossStore) || TII->isPredicated(*PI)) |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1769 | return MBB->end(); |
| 1770 | |
| 1771 | |
| 1772 | // Find out what registers are live. Note this routine is ignoring other live |
| 1773 | // registers which are only used by instructions in successor blocks. |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 1774 | for (const MachineOperand &MO : PI->operands()) { |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1775 | if (!MO.isReg()) |
| 1776 | continue; |
| 1777 | unsigned Reg = MO.getReg(); |
| 1778 | if (!Reg) |
| 1779 | continue; |
| 1780 | if (MO.isUse()) { |
Jingyue Wu | 3a04dc6 | 2015-07-29 18:59:09 +0000 | [diff] [blame] | 1781 | addRegAndItsAliases(Reg, TRI, Uses); |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1782 | } else { |
Benjamin Kramer | f29db27 | 2012-08-22 15:37:57 +0000 | [diff] [blame] | 1783 | if (Uses.erase(Reg)) { |
Jingyue Wu | 3a04dc6 | 2015-07-29 18:59:09 +0000 | [diff] [blame] | 1784 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 1785 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) |
| 1786 | Uses.erase(*SubRegs); // Use sub-registers to be conservative |
| 1787 | } |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1788 | } |
Jingyue Wu | 3a04dc6 | 2015-07-29 18:59:09 +0000 | [diff] [blame] | 1789 | addRegAndItsAliases(Reg, TRI, Defs); |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1790 | } |
| 1791 | } |
| 1792 | |
| 1793 | return PI; |
| 1794 | } |
| 1795 | |
| 1796 | /// HoistCommonCodeInSuccs - If the successors of MBB has common instruction |
| 1797 | /// sequence at the start of the function, move the instructions before MBB |
| 1798 | /// terminator if it's legal. |
| 1799 | bool BranchFolder::HoistCommonCodeInSuccs(MachineBasicBlock *MBB) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1800 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1801 | SmallVector<MachineOperand, 4> Cond; |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1802 | if (TII->analyzeBranch(*MBB, TBB, FBB, Cond, true) || !TBB || Cond.empty()) |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1803 | return false; |
| 1804 | |
| 1805 | if (!FBB) FBB = findFalseBlock(MBB, TBB); |
| 1806 | if (!FBB) |
| 1807 | // Malformed bcc? True and false blocks are the same? |
| 1808 | return false; |
| 1809 | |
| 1810 | // Restrict the optimization to cases where MBB is the only predecessor, |
| 1811 | // it is an obvious win. |
| 1812 | if (TBB->pred_size() > 1 || FBB->pred_size() > 1) |
| 1813 | return false; |
| 1814 | |
| 1815 | // Find a suitable position to hoist the common instructions to. Also figure |
| 1816 | // out which registers are used or defined by instructions from the insertion |
| 1817 | // point to the end of the block. |
| 1818 | SmallSet<unsigned, 4> Uses, Defs; |
| 1819 | MachineBasicBlock::iterator Loc = |
| 1820 | findHoistingInsertPosAndDeps(MBB, TII, TRI, Uses, Defs); |
| 1821 | if (Loc == MBB->end()) |
| 1822 | return false; |
| 1823 | |
| 1824 | bool HasDups = false; |
Evan Cheng | 43054e6 | 2011-05-12 20:30:01 +0000 | [diff] [blame] | 1825 | SmallVector<unsigned, 4> LocalDefs; |
| 1826 | SmallSet<unsigned, 4> LocalDefsSet; |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1827 | MachineBasicBlock::iterator TIB = TBB->begin(); |
| 1828 | MachineBasicBlock::iterator FIB = FBB->begin(); |
| 1829 | MachineBasicBlock::iterator TIE = TBB->end(); |
| 1830 | MachineBasicBlock::iterator FIE = FBB->end(); |
| 1831 | while (TIB != TIE && FIB != FIE) { |
| 1832 | // Skip dbg_value instructions. These do not count. |
| 1833 | if (TIB->isDebugValue()) { |
| 1834 | while (TIB != TIE && TIB->isDebugValue()) |
| 1835 | ++TIB; |
| 1836 | if (TIB == TIE) |
| 1837 | break; |
| 1838 | } |
| 1839 | if (FIB->isDebugValue()) { |
| 1840 | while (FIB != FIE && FIB->isDebugValue()) |
| 1841 | ++FIB; |
| 1842 | if (FIB == FIE) |
| 1843 | break; |
| 1844 | } |
Duncan P. N. Exon Smith | fd8cc23 | 2016-02-27 20:01:33 +0000 | [diff] [blame] | 1845 | if (!TIB->isIdenticalTo(*FIB, MachineInstr::CheckKillDead)) |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1846 | break; |
| 1847 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1848 | if (TII->isPredicated(*TIB)) |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1849 | // Hard to reason about register liveness with predicated instruction. |
| 1850 | break; |
| 1851 | |
| 1852 | bool IsSafe = true; |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 1853 | for (MachineOperand &MO : TIB->operands()) { |
Jakob Stoklund Olesen | e9e30d0 | 2012-02-15 23:42:54 +0000 | [diff] [blame] | 1854 | // Don't attempt to hoist instructions with register masks. |
| 1855 | if (MO.isRegMask()) { |
| 1856 | IsSafe = false; |
| 1857 | break; |
| 1858 | } |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1859 | if (!MO.isReg()) |
| 1860 | continue; |
| 1861 | unsigned Reg = MO.getReg(); |
| 1862 | if (!Reg) |
| 1863 | continue; |
| 1864 | if (MO.isDef()) { |
| 1865 | if (Uses.count(Reg)) { |
| 1866 | // Avoid clobbering a register that's used by the instruction at |
| 1867 | // the point of insertion. |
| 1868 | IsSafe = false; |
| 1869 | break; |
| 1870 | } |
| 1871 | |
| 1872 | if (Defs.count(Reg) && !MO.isDead()) { |
| 1873 | // Don't hoist the instruction if the def would be clobber by the |
| 1874 | // instruction at the point insertion. FIXME: This is overly |
| 1875 | // conservative. It should be possible to hoist the instructions |
| 1876 | // in BB2 in the following example: |
| 1877 | // BB1: |
| 1878 | // r1, eflag = op1 r2, r3 |
| 1879 | // brcc eflag |
| 1880 | // |
| 1881 | // BB2: |
| 1882 | // r1 = op2, ... |
| 1883 | // = op3, r1<kill> |
| 1884 | IsSafe = false; |
| 1885 | break; |
| 1886 | } |
Evan Cheng | 43054e6 | 2011-05-12 20:30:01 +0000 | [diff] [blame] | 1887 | } else if (!LocalDefsSet.count(Reg)) { |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1888 | if (Defs.count(Reg)) { |
| 1889 | // Use is defined by the instruction at the point of insertion. |
| 1890 | IsSafe = false; |
| 1891 | break; |
| 1892 | } |
Evan Cheng | 5c03a6b | 2012-01-12 20:31:24 +0000 | [diff] [blame] | 1893 | |
| 1894 | if (MO.isKill() && Uses.count(Reg)) |
| 1895 | // Kills a register that's read by the instruction at the point of |
| 1896 | // insertion. Remove the kill marker. |
| 1897 | MO.setIsKill(false); |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1898 | } |
| 1899 | } |
| 1900 | if (!IsSafe) |
| 1901 | break; |
| 1902 | |
| 1903 | bool DontMoveAcrossStore = true; |
Matthias Braun | 07066cc | 2015-05-19 21:22:20 +0000 | [diff] [blame] | 1904 | if (!TIB->isSafeToMove(nullptr, DontMoveAcrossStore)) |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1905 | break; |
| 1906 | |
Jakob Stoklund Olesen | d633abe | 2011-08-05 18:47:07 +0000 | [diff] [blame] | 1907 | // Remove kills from LocalDefsSet, these registers had short live ranges. |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 1908 | for (const MachineOperand &MO : TIB->operands()) { |
Jakob Stoklund Olesen | d633abe | 2011-08-05 18:47:07 +0000 | [diff] [blame] | 1909 | if (!MO.isReg() || !MO.isUse() || !MO.isKill()) |
| 1910 | continue; |
| 1911 | unsigned Reg = MO.getReg(); |
| 1912 | if (!Reg || !LocalDefsSet.count(Reg)) |
| 1913 | continue; |
Jingyue Wu | 3a04dc6 | 2015-07-29 18:59:09 +0000 | [diff] [blame] | 1914 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 1915 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) |
| 1916 | LocalDefsSet.erase(*AI); |
| 1917 | } else { |
| 1918 | LocalDefsSet.erase(Reg); |
| 1919 | } |
Jakob Stoklund Olesen | d633abe | 2011-08-05 18:47:07 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Evan Cheng | 43054e6 | 2011-05-12 20:30:01 +0000 | [diff] [blame] | 1922 | // Track local defs so we can update liveins. |
Craig Topper | 5db36df | 2015-09-16 03:52:35 +0000 | [diff] [blame] | 1923 | for (const MachineOperand &MO : TIB->operands()) { |
Jakob Stoklund Olesen | d633abe | 2011-08-05 18:47:07 +0000 | [diff] [blame] | 1924 | if (!MO.isReg() || !MO.isDef() || MO.isDead()) |
Evan Cheng | 43054e6 | 2011-05-12 20:30:01 +0000 | [diff] [blame] | 1925 | continue; |
| 1926 | unsigned Reg = MO.getReg(); |
Dan Gohman | 0cfb5f8 | 2016-05-10 04:24:02 +0000 | [diff] [blame] | 1927 | if (!Reg || TargetRegisterInfo::isVirtualRegister(Reg)) |
Evan Cheng | 43054e6 | 2011-05-12 20:30:01 +0000 | [diff] [blame] | 1928 | continue; |
Jakob Stoklund Olesen | d633abe | 2011-08-05 18:47:07 +0000 | [diff] [blame] | 1929 | LocalDefs.push_back(Reg); |
Jingyue Wu | 3a04dc6 | 2015-07-29 18:59:09 +0000 | [diff] [blame] | 1930 | addRegAndItsAliases(Reg, TRI, LocalDefsSet); |
Evan Cheng | 43054e6 | 2011-05-12 20:30:01 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
Chad Rosier | 5dfe6da | 2012-02-22 17:25:00 +0000 | [diff] [blame] | 1933 | HasDups = true; |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1934 | ++TIB; |
| 1935 | ++FIB; |
| 1936 | } |
| 1937 | |
| 1938 | if (!HasDups) |
| 1939 | return false; |
| 1940 | |
| 1941 | MBB->splice(Loc, TBB, TBB->begin(), TIB); |
| 1942 | FBB->erase(FBB->begin(), FIB); |
Evan Cheng | 43054e6 | 2011-05-12 20:30:01 +0000 | [diff] [blame] | 1943 | |
| 1944 | // Update livein's. |
Ahmed Bougacha | b678219 | 2016-09-12 16:05:31 +0000 | [diff] [blame] | 1945 | bool AddedLiveIns = false; |
Evan Cheng | 43054e6 | 2011-05-12 20:30:01 +0000 | [diff] [blame] | 1946 | for (unsigned i = 0, e = LocalDefs.size(); i != e; ++i) { |
| 1947 | unsigned Def = LocalDefs[i]; |
| 1948 | if (LocalDefsSet.count(Def)) { |
| 1949 | TBB->addLiveIn(Def); |
| 1950 | FBB->addLiveIn(Def); |
Ahmed Bougacha | b678219 | 2016-09-12 16:05:31 +0000 | [diff] [blame] | 1951 | AddedLiveIns = true; |
Evan Cheng | 43054e6 | 2011-05-12 20:30:01 +0000 | [diff] [blame] | 1952 | } |
| 1953 | } |
| 1954 | |
Ahmed Bougacha | b678219 | 2016-09-12 16:05:31 +0000 | [diff] [blame] | 1955 | if (AddedLiveIns) { |
| 1956 | TBB->sortUniqueLiveIns(); |
| 1957 | FBB->sortUniqueLiveIns(); |
| 1958 | } |
| 1959 | |
Evan Cheng | cfdf339 | 2011-05-12 00:56:58 +0000 | [diff] [blame] | 1960 | ++NumHoist; |
| 1961 | return true; |
| 1962 | } |