Chris Lattner | 21ab22e | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 1 | //===-- BranchFolding.cpp - Fold machine code branch instructions ---------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 21ab22e | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 21ab22e | 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 |
| 15 | // SSA form. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Chris Lattner | f10a56a | 2006-11-18 21:56:39 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "branchfolding" |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 20 | #include "BranchFolding.h" |
Bob Wilson | 2c04dae | 2009-10-28 22:10:20 +0000 | [diff] [blame] | 21 | #include "llvm/Function.h" |
Chris Lattner | 21ab22e | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/Passes.h" |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chris Lattner | 21ab22e | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chris Lattner | c50ffcb | 2006-10-17 17:13:52 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Dale Johannesen | 69cb9b7 | 2007-03-20 21:35:06 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/RegisterScavenging.h" |
Chris Lattner | 21ab22e | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetInstrInfo.h" |
| 28 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | f10a56a | 2006-11-18 21:56:39 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ErrorHandling.h" |
Bill Wendling | 3403bcd | 2009-08-22 20:03:00 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Evan Cheng | 80b09fe | 2008-04-10 02:32:10 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallSet.h" |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/Statistic.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/STLExtras.h" |
Jeff Cohen | d41b30d | 2006-11-05 19:31:28 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Chris Lattner | 21ab22e | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 40 | STATISTIC(NumDeadBlocks, "Number of dead blocks removed"); |
| 41 | STATISTIC(NumBranchOpts, "Number of branches optimized"); |
| 42 | STATISTIC(NumTailMerge , "Number of block tails merged"); |
Dale Johannesen | 81da02b | 2007-05-22 17:14:46 +0000 | [diff] [blame] | 43 | static cl::opt<cl::boolOrDefault> FlagEnableTailMerge("enable-tail-merge", |
| 44 | cl::init(cl::BOU_UNSET), cl::Hidden); |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 45 | // Throttle for huge numbers of predecessors (compile speed problems) |
| 46 | static cl::opt<unsigned> |
| 47 | TailMergeThreshold("tail-merge-threshold", |
| 48 | cl::desc("Max number of predecessors to consider tail merging"), |
Dale Johannesen | 622addb | 2008-10-27 02:10:21 +0000 | [diff] [blame] | 49 | cl::init(150), cl::Hidden); |
Dale Johannesen | 1a90a5a | 2007-06-08 01:08:52 +0000 | [diff] [blame] | 50 | |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 51 | |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 52 | char BranchFolderPass::ID = 0; |
Chris Lattner | 21ab22e | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 53 | |
Bob Wilson | a597103 | 2009-10-28 20:46:46 +0000 | [diff] [blame] | 54 | FunctionPass *llvm::createBranchFoldingPass(bool DefaultEnableTailMerge) { |
| 55 | return new BranchFolderPass(DefaultEnableTailMerge); |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bool BranchFolderPass::runOnMachineFunction(MachineFunction &MF) { |
| 59 | return OptimizeFunction(MF, |
| 60 | MF.getTarget().getInstrInfo(), |
| 61 | MF.getTarget().getRegisterInfo(), |
| 62 | getAnalysisIfAvailable<MachineModuleInfo>()); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | |
Bob Wilson | a597103 | 2009-10-28 20:46:46 +0000 | [diff] [blame] | 67 | BranchFolder::BranchFolder(bool defaultEnableTailMerge) { |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 68 | switch (FlagEnableTailMerge) { |
| 69 | case cl::BOU_UNSET: EnableTailMerge = defaultEnableTailMerge; break; |
| 70 | case cl::BOU_TRUE: EnableTailMerge = true; break; |
| 71 | case cl::BOU_FALSE: EnableTailMerge = false; break; |
| 72 | } |
Evan Cheng | b3c2742 | 2009-09-03 23:54:22 +0000 | [diff] [blame] | 73 | } |
Chris Lattner | 21ab22e | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 74 | |
Chris Lattner | c50ffcb | 2006-10-17 17:13:52 +0000 | [diff] [blame] | 75 | /// RemoveDeadBlock - Remove the specified dead machine basic block from the |
| 76 | /// function, updating the CFG. |
Chris Lattner | 683747a | 2006-10-17 23:17:27 +0000 | [diff] [blame] | 77 | void BranchFolder::RemoveDeadBlock(MachineBasicBlock *MBB) { |
Jim Laskey | 033c971 | 2007-02-22 16:39:03 +0000 | [diff] [blame] | 78 | assert(MBB->pred_empty() && "MBB must be dead!"); |
Bill Wendling | 3403bcd | 2009-08-22 20:03:00 +0000 | [diff] [blame] | 79 | DEBUG(errs() << "\nRemoving MBB: " << *MBB); |
Chris Lattner | 683747a | 2006-10-17 23:17:27 +0000 | [diff] [blame] | 80 | |
Chris Lattner | c50ffcb | 2006-10-17 17:13:52 +0000 | [diff] [blame] | 81 | MachineFunction *MF = MBB->getParent(); |
| 82 | // drop all successors. |
| 83 | while (!MBB->succ_empty()) |
| 84 | MBB->removeSuccessor(MBB->succ_end()-1); |
Chris Lattner | 683747a | 2006-10-17 23:17:27 +0000 | [diff] [blame] | 85 | |
Duncan Sands | 68d4d1d | 2008-07-29 20:56:02 +0000 | [diff] [blame] | 86 | // If there are any labels in the basic block, unregister them from |
| 87 | // MachineModuleInfo. |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 88 | if (MMI && !MBB->empty()) { |
Chris Lattner | 683747a | 2006-10-17 23:17:27 +0000 | [diff] [blame] | 89 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
| 90 | I != E; ++I) { |
Duncan Sands | 68d4d1d | 2008-07-29 20:56:02 +0000 | [diff] [blame] | 91 | if (I->isLabel()) |
Chris Lattner | 683747a | 2006-10-17 23:17:27 +0000 | [diff] [blame] | 92 | // The label ID # is always operand #0, an immediate. |
Jim Laskey | 44c3b9f | 2007-01-26 21:22:28 +0000 | [diff] [blame] | 93 | MMI->InvalidateLabel(I->getOperand(0).getImm()); |
Chris Lattner | 683747a | 2006-10-17 23:17:27 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Chris Lattner | c50ffcb | 2006-10-17 17:13:52 +0000 | [diff] [blame] | 97 | // Remove the block. |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 98 | MF->erase(MBB); |
Chris Lattner | c50ffcb | 2006-10-17 17:13:52 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Evan Cheng | 80b09fe | 2008-04-10 02:32:10 +0000 | [diff] [blame] | 101 | /// OptimizeImpDefsBlock - If a basic block is just a bunch of implicit_def |
| 102 | /// followed by terminators, and if the implicitly defined registers are not |
| 103 | /// used by the terminators, remove those implicit_def's. e.g. |
| 104 | /// BB1: |
| 105 | /// r0 = implicit_def |
| 106 | /// r1 = implicit_def |
| 107 | /// br |
| 108 | /// This block can be optimized away later if the implicit instructions are |
| 109 | /// removed. |
| 110 | bool BranchFolder::OptimizeImpDefsBlock(MachineBasicBlock *MBB) { |
| 111 | SmallSet<unsigned, 4> ImpDefRegs; |
| 112 | MachineBasicBlock::iterator I = MBB->begin(); |
| 113 | while (I != MBB->end()) { |
| 114 | if (I->getOpcode() != TargetInstrInfo::IMPLICIT_DEF) |
| 115 | break; |
| 116 | unsigned Reg = I->getOperand(0).getReg(); |
| 117 | ImpDefRegs.insert(Reg); |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 118 | for (const unsigned *SubRegs = TRI->getSubRegisters(Reg); |
Evan Cheng | 80b09fe | 2008-04-10 02:32:10 +0000 | [diff] [blame] | 119 | unsigned SubReg = *SubRegs; ++SubRegs) |
| 120 | ImpDefRegs.insert(SubReg); |
| 121 | ++I; |
| 122 | } |
| 123 | if (ImpDefRegs.empty()) |
| 124 | return false; |
| 125 | |
| 126 | MachineBasicBlock::iterator FirstTerm = I; |
| 127 | while (I != MBB->end()) { |
| 128 | if (!TII->isUnpredicatedTerminator(I)) |
| 129 | return false; |
| 130 | // See if it uses any of the implicitly defined registers. |
| 131 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 132 | MachineOperand &MO = I->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 133 | if (!MO.isReg() || !MO.isUse()) |
Evan Cheng | 80b09fe | 2008-04-10 02:32:10 +0000 | [diff] [blame] | 134 | continue; |
| 135 | unsigned Reg = MO.getReg(); |
| 136 | if (ImpDefRegs.count(Reg)) |
| 137 | return false; |
| 138 | } |
| 139 | ++I; |
| 140 | } |
| 141 | |
| 142 | I = MBB->begin(); |
| 143 | while (I != FirstTerm) { |
| 144 | MachineInstr *ImpDefMI = &*I; |
| 145 | ++I; |
| 146 | MBB->erase(ImpDefMI); |
| 147 | } |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 152 | /// OptimizeFunction - Perhaps branch folding, tail merging and other |
| 153 | /// CFG optimizations on the given function. |
| 154 | bool BranchFolder::OptimizeFunction(MachineFunction &MF, |
| 155 | const TargetInstrInfo *tii, |
| 156 | const TargetRegisterInfo *tri, |
| 157 | MachineModuleInfo *mmi) { |
| 158 | if (!tii) return false; |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 159 | |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 160 | TII = tii; |
| 161 | TRI = tri; |
| 162 | MMI = mmi; |
| 163 | |
| 164 | RS = TRI->requiresRegisterScavenging(MF) ? new RegScavenger() : NULL; |
Evan Cheng | 80b09fe | 2008-04-10 02:32:10 +0000 | [diff] [blame] | 165 | |
Dale Johannesen | 14ba0cc | 2007-05-15 21:19:17 +0000 | [diff] [blame] | 166 | // Fix CFG. The later algorithms expect it to be right. |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 167 | bool MadeChange = false; |
Dale Johannesen | 14ba0cc | 2007-05-15 21:19:17 +0000 | [diff] [blame] | 168 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; I++) { |
| 169 | MachineBasicBlock *MBB = I, *TBB = 0, *FBB = 0; |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 170 | SmallVector<MachineOperand, 4> Cond; |
Evan Cheng | dc54d31 | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 171 | if (!TII->AnalyzeBranch(*MBB, TBB, FBB, Cond, true)) |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 172 | MadeChange |= MBB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty()); |
| 173 | MadeChange |= OptimizeImpDefsBlock(MBB); |
Dale Johannesen | 14ba0cc | 2007-05-15 21:19:17 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 177 | bool MadeChangeThisIteration = true; |
| 178 | while (MadeChangeThisIteration) { |
| 179 | MadeChangeThisIteration = false; |
| 180 | MadeChangeThisIteration |= TailMergeBlocks(MF); |
| 181 | MadeChangeThisIteration |= OptimizeBranches(MF); |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 182 | MadeChange |= MadeChangeThisIteration; |
Chris Lattner | 21ab22e | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Chris Lattner | 6acfe12 | 2006-10-28 18:34:47 +0000 | [diff] [blame] | 185 | // See if any jump tables have become mergable or dead as the code generator |
| 186 | // did its thing. |
| 187 | MachineJumpTableInfo *JTI = MF.getJumpTableInfo(); |
| 188 | const std::vector<MachineJumpTableEntry> &JTs = JTI->getJumpTables(); |
| 189 | if (!JTs.empty()) { |
| 190 | // Figure out how these jump tables should be merged. |
| 191 | std::vector<unsigned> JTMapping; |
| 192 | JTMapping.reserve(JTs.size()); |
| 193 | |
| 194 | // We always keep the 0th jump table. |
| 195 | JTMapping.push_back(0); |
| 196 | |
| 197 | // Scan the jump tables, seeing if there are any duplicates. Note that this |
| 198 | // is N^2, which should be fixed someday. |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 199 | for (unsigned i = 1, e = JTs.size(); i != e; ++i) { |
| 200 | if (JTs[i].MBBs.empty()) |
| 201 | JTMapping.push_back(i); |
| 202 | else |
| 203 | JTMapping.push_back(JTI->getJumpTableIndex(JTs[i].MBBs)); |
| 204 | } |
Chris Lattner | 6acfe12 | 2006-10-28 18:34:47 +0000 | [diff] [blame] | 205 | |
| 206 | // If a jump table was merge with another one, walk the function rewriting |
| 207 | // references to jump tables to reference the new JT ID's. Keep track of |
| 208 | // whether we see a jump table idx, if not, we can delete the JT. |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 209 | BitVector JTIsLive(JTs.size()); |
Chris Lattner | 6acfe12 | 2006-10-28 18:34:47 +0000 | [diff] [blame] | 210 | for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); |
| 211 | BB != E; ++BB) { |
| 212 | for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); |
| 213 | I != E; ++I) |
| 214 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) { |
| 215 | MachineOperand &Op = I->getOperand(op); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 216 | if (!Op.isJTI()) continue; |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 217 | unsigned NewIdx = JTMapping[Op.getIndex()]; |
| 218 | Op.setIndex(NewIdx); |
Chris Lattner | 6acfe12 | 2006-10-28 18:34:47 +0000 | [diff] [blame] | 219 | |
| 220 | // Remember that this JT is live. |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 221 | JTIsLive.set(NewIdx); |
Chris Lattner | 6acfe12 | 2006-10-28 18:34:47 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | // Finally, remove dead jump tables. This happens either because the |
| 226 | // indirect jump was unreachable (and thus deleted) or because the jump |
| 227 | // table was merged with some other one. |
| 228 | for (unsigned i = 0, e = JTIsLive.size(); i != e; ++i) |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 229 | if (!JTIsLive.test(i)) { |
Chris Lattner | 6acfe12 | 2006-10-28 18:34:47 +0000 | [diff] [blame] | 230 | JTI->RemoveJumpTable(i); |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 231 | MadeChange = true; |
Chris Lattner | 6acfe12 | 2006-10-28 18:34:47 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 234 | |
Dale Johannesen | 69cb9b7 | 2007-03-20 21:35:06 +0000 | [diff] [blame] | 235 | delete RS; |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 236 | return MadeChange; |
Chris Lattner | 21ab22e | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 239 | //===----------------------------------------------------------------------===// |
| 240 | // Tail Merging of Blocks |
| 241 | //===----------------------------------------------------------------------===// |
| 242 | |
| 243 | /// HashMachineInstr - Compute a hash value for MI and its operands. |
| 244 | static unsigned HashMachineInstr(const MachineInstr *MI) { |
| 245 | unsigned Hash = MI->getOpcode(); |
| 246 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 247 | const MachineOperand &Op = MI->getOperand(i); |
| 248 | |
| 249 | // Merge in bits from the operand if easy. |
| 250 | unsigned OperandHash = 0; |
| 251 | switch (Op.getType()) { |
| 252 | case MachineOperand::MO_Register: OperandHash = Op.getReg(); break; |
| 253 | case MachineOperand::MO_Immediate: OperandHash = Op.getImm(); break; |
| 254 | case MachineOperand::MO_MachineBasicBlock: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 255 | OperandHash = Op.getMBB()->getNumber(); |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 256 | break; |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 257 | case MachineOperand::MO_FrameIndex: |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 258 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 259 | case MachineOperand::MO_JumpTableIndex: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 260 | OperandHash = Op.getIndex(); |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 261 | break; |
| 262 | case MachineOperand::MO_GlobalAddress: |
| 263 | case MachineOperand::MO_ExternalSymbol: |
| 264 | // Global address / external symbol are too hard, don't bother, but do |
| 265 | // pull in the offset. |
| 266 | OperandHash = Op.getOffset(); |
| 267 | break; |
| 268 | default: break; |
| 269 | } |
| 270 | |
| 271 | Hash += ((OperandHash << 3) | Op.getType()) << (i&31); |
| 272 | } |
| 273 | return Hash; |
| 274 | } |
| 275 | |
Dale Johannesen | 7aea832 | 2007-05-23 21:07:20 +0000 | [diff] [blame] | 276 | /// HashEndOfMBB - Hash the last few instructions in the MBB. For blocks |
| 277 | /// with no successors, we hash two instructions, because cross-jumping |
| 278 | /// only saves code when at least two instructions are removed (since a |
| 279 | /// branch must be inserted). For blocks with a successor, one of the |
| 280 | /// two blocks to be tail-merged will end with a branch already, so |
| 281 | /// it gains to cross-jump even for one instruction. |
| 282 | |
| 283 | static unsigned HashEndOfMBB(const MachineBasicBlock *MBB, |
| 284 | unsigned minCommonTailLength) { |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 285 | MachineBasicBlock::const_iterator I = MBB->end(); |
| 286 | if (I == MBB->begin()) |
| 287 | return 0; // Empty MBB. |
| 288 | |
| 289 | --I; |
| 290 | unsigned Hash = HashMachineInstr(I); |
| 291 | |
Dale Johannesen | 7aea832 | 2007-05-23 21:07:20 +0000 | [diff] [blame] | 292 | if (I == MBB->begin() || minCommonTailLength == 1) |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 293 | return Hash; // Single instr MBB. |
| 294 | |
| 295 | --I; |
| 296 | // Hash in the second-to-last instruction. |
| 297 | Hash ^= HashMachineInstr(I) << 2; |
| 298 | return Hash; |
| 299 | } |
| 300 | |
| 301 | /// ComputeCommonTailLength - Given two machine basic blocks, compute the number |
| 302 | /// of instructions they actually have in common together at their end. Return |
| 303 | /// iterators for the first shared instruction in each block. |
| 304 | static unsigned ComputeCommonTailLength(MachineBasicBlock *MBB1, |
| 305 | MachineBasicBlock *MBB2, |
| 306 | MachineBasicBlock::iterator &I1, |
| 307 | MachineBasicBlock::iterator &I2) { |
| 308 | I1 = MBB1->end(); |
| 309 | I2 = MBB2->end(); |
| 310 | |
| 311 | unsigned TailLen = 0; |
| 312 | while (I1 != MBB1->begin() && I2 != MBB2->begin()) { |
| 313 | --I1; --I2; |
Bill Wendling | 80629c8 | 2007-10-19 21:09:55 +0000 | [diff] [blame] | 314 | if (!I1->isIdenticalTo(I2) || |
Bill Wendling | da6efc5 | 2007-10-25 19:49:32 +0000 | [diff] [blame] | 315 | // FIXME: This check is dubious. It's used to get around a problem where |
Bill Wendling | 0713a22 | 2007-10-25 18:23:45 +0000 | [diff] [blame] | 316 | // people incorrectly expect inline asm directives to remain in the same |
| 317 | // relative order. This is untenable because normal compiler |
| 318 | // optimizations (like this one) may reorder and/or merge these |
| 319 | // directives. |
Bill Wendling | 80629c8 | 2007-10-19 21:09:55 +0000 | [diff] [blame] | 320 | I1->getOpcode() == TargetInstrInfo::INLINEASM) { |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 321 | ++I1; ++I2; |
| 322 | break; |
| 323 | } |
| 324 | ++TailLen; |
| 325 | } |
| 326 | return TailLen; |
| 327 | } |
| 328 | |
| 329 | /// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 330 | /// after it, replacing it with an unconditional branch to NewDest. This |
| 331 | /// returns true if OldInst's block is modified, false if NewDest is modified. |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 332 | void BranchFolder::ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst, |
| 333 | MachineBasicBlock *NewDest) { |
| 334 | MachineBasicBlock *OldBB = OldInst->getParent(); |
| 335 | |
| 336 | // Remove all the old successors of OldBB from the CFG. |
| 337 | while (!OldBB->succ_empty()) |
| 338 | OldBB->removeSuccessor(OldBB->succ_begin()); |
| 339 | |
| 340 | // Remove all the dead instructions from the end of OldBB. |
| 341 | OldBB->erase(OldInst, OldBB->end()); |
| 342 | |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 343 | // If OldBB isn't immediately before OldBB, insert a branch to it. |
| 344 | if (++MachineFunction::iterator(OldBB) != MachineFunction::iterator(NewDest)) |
Dan Gohman | 1501cdb | 2008-08-22 16:07:55 +0000 | [diff] [blame] | 345 | TII->InsertBranch(*OldBB, NewDest, 0, SmallVector<MachineOperand, 0>()); |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 346 | OldBB->addSuccessor(NewDest); |
| 347 | ++NumTailMerge; |
| 348 | } |
| 349 | |
Chris Lattner | 1d08d83 | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 350 | /// SplitMBBAt - Given a machine basic block and an iterator into it, split the |
| 351 | /// MBB so that the part before the iterator falls into the part starting at the |
| 352 | /// iterator. This returns the new MBB. |
| 353 | MachineBasicBlock *BranchFolder::SplitMBBAt(MachineBasicBlock &CurMBB, |
| 354 | MachineBasicBlock::iterator BBI1) { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 355 | MachineFunction &MF = *CurMBB.getParent(); |
| 356 | |
Chris Lattner | 1d08d83 | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 357 | // Create the fall-through block. |
| 358 | MachineFunction::iterator MBBI = &CurMBB; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 359 | MachineBasicBlock *NewMBB =MF.CreateMachineBasicBlock(CurMBB.getBasicBlock()); |
| 360 | CurMBB.getParent()->insert(++MBBI, NewMBB); |
Chris Lattner | 1d08d83 | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 361 | |
| 362 | // Move all the successors of this block to the specified block. |
Dan Gohman | 04478e5 | 2008-06-19 17:22:29 +0000 | [diff] [blame] | 363 | NewMBB->transferSuccessors(&CurMBB); |
Chris Lattner | 1d08d83 | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 364 | |
| 365 | // Add an edge from CurMBB to NewMBB for the fall-through. |
| 366 | CurMBB.addSuccessor(NewMBB); |
| 367 | |
| 368 | // Splice the code over. |
| 369 | NewMBB->splice(NewMBB->end(), &CurMBB, BBI1, CurMBB.end()); |
Dale Johannesen | 69cb9b7 | 2007-03-20 21:35:06 +0000 | [diff] [blame] | 370 | |
| 371 | // For targets that use the register scavenger, we must maintain LiveIns. |
| 372 | if (RS) { |
| 373 | RS->enterBasicBlock(&CurMBB); |
| 374 | if (!CurMBB.empty()) |
| 375 | RS->forward(prior(CurMBB.end())); |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 376 | BitVector RegsLiveAtExit(TRI->getNumRegs()); |
Dale Johannesen | 69cb9b7 | 2007-03-20 21:35:06 +0000 | [diff] [blame] | 377 | RS->getRegsUsed(RegsLiveAtExit, false); |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 378 | for (unsigned int i=0, e=TRI->getNumRegs(); i!=e; i++) |
Dale Johannesen | 69cb9b7 | 2007-03-20 21:35:06 +0000 | [diff] [blame] | 379 | if (RegsLiveAtExit[i]) |
| 380 | NewMBB->addLiveIn(i); |
| 381 | } |
| 382 | |
Chris Lattner | 1d08d83 | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 383 | return NewMBB; |
| 384 | } |
| 385 | |
Chris Lattner | d4bf3c2 | 2006-11-01 19:36:29 +0000 | [diff] [blame] | 386 | /// EstimateRuntime - Make a rough estimate for how long it will take to run |
| 387 | /// the specified code. |
| 388 | static unsigned EstimateRuntime(MachineBasicBlock::iterator I, |
Chris Lattner | 6924430 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 389 | MachineBasicBlock::iterator E) { |
Chris Lattner | d4bf3c2 | 2006-11-01 19:36:29 +0000 | [diff] [blame] | 390 | unsigned Time = 0; |
| 391 | for (; I != E; ++I) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 392 | const TargetInstrDesc &TID = I->getDesc(); |
| 393 | if (TID.isCall()) |
Chris Lattner | d4bf3c2 | 2006-11-01 19:36:29 +0000 | [diff] [blame] | 394 | Time += 10; |
Dan Gohman | 41474ba | 2008-12-03 02:30:17 +0000 | [diff] [blame] | 395 | else if (TID.mayLoad() || TID.mayStore()) |
Chris Lattner | d4bf3c2 | 2006-11-01 19:36:29 +0000 | [diff] [blame] | 396 | Time += 2; |
| 397 | else |
| 398 | ++Time; |
| 399 | } |
| 400 | return Time; |
| 401 | } |
| 402 | |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 403 | // CurMBB needs to add an unconditional branch to SuccMBB (we removed these |
| 404 | // branches temporarily for tail merging). In the case where CurMBB ends |
| 405 | // with a conditional branch to the next block, optimize by reversing the |
| 406 | // test and conditionally branching to SuccMBB instead. |
| 407 | |
| 408 | static void FixTail(MachineBasicBlock* CurMBB, MachineBasicBlock *SuccBB, |
| 409 | const TargetInstrInfo *TII) { |
| 410 | MachineFunction *MF = CurMBB->getParent(); |
| 411 | MachineFunction::iterator I = next(MachineFunction::iterator(CurMBB)); |
| 412 | MachineBasicBlock *TBB = 0, *FBB = 0; |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 413 | SmallVector<MachineOperand, 4> Cond; |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 414 | if (I != MF->end() && |
Evan Cheng | dc54d31 | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 415 | !TII->AnalyzeBranch(*CurMBB, TBB, FBB, Cond, true)) { |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 416 | MachineBasicBlock *NextBB = I; |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 417 | if (TBB == NextBB && !Cond.empty() && !FBB) { |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 418 | if (!TII->ReverseBranchCondition(Cond)) { |
| 419 | TII->RemoveBranch(*CurMBB); |
| 420 | TII->InsertBranch(*CurMBB, SuccBB, NULL, Cond); |
| 421 | return; |
| 422 | } |
| 423 | } |
| 424 | } |
Dan Gohman | 1501cdb | 2008-08-22 16:07:55 +0000 | [diff] [blame] | 425 | TII->InsertBranch(*CurMBB, SuccBB, NULL, SmallVector<MachineOperand, 0>()); |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Dale Johannesen | 44008c5 | 2007-05-30 00:32:01 +0000 | [diff] [blame] | 428 | static bool MergeCompare(const std::pair<unsigned,MachineBasicBlock*> &p, |
| 429 | const std::pair<unsigned,MachineBasicBlock*> &q) { |
Dale Johannesen | 95ef406 | 2007-05-29 23:47:50 +0000 | [diff] [blame] | 430 | if (p.first < q.first) |
| 431 | return true; |
| 432 | else if (p.first > q.first) |
| 433 | return false; |
| 434 | else if (p.second->getNumber() < q.second->getNumber()) |
| 435 | return true; |
| 436 | else if (p.second->getNumber() > q.second->getNumber()) |
| 437 | return false; |
David Greene | 67fcdf7 | 2007-07-10 22:00:30 +0000 | [diff] [blame] | 438 | else { |
Duncan Sands | 97b4ac8 | 2007-07-11 08:47:55 +0000 | [diff] [blame] | 439 | // _GLIBCXX_DEBUG checks strict weak ordering, which involves comparing |
| 440 | // an object with itself. |
| 441 | #ifndef _GLIBCXX_DEBUG |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 442 | llvm_unreachable("Predecessor appears twice"); |
David Greene | 67fcdf7 | 2007-07-10 22:00:30 +0000 | [diff] [blame] | 443 | #endif |
Dan Gohman | 5d5ee80 | 2009-01-08 22:19:34 +0000 | [diff] [blame] | 444 | return false; |
David Greene | 67fcdf7 | 2007-07-10 22:00:30 +0000 | [diff] [blame] | 445 | } |
Dale Johannesen | 95ef406 | 2007-05-29 23:47:50 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Bob Wilson | 7b888b8 | 2009-10-29 18:40:06 +0000 | [diff] [blame] | 448 | /// ProfitableToMerge - Check if two machine basic blocks have a common tail |
| 449 | /// and decide if it would be profitable to merge those tails. Return the |
| 450 | /// length of the common tail and iterators to the first common instruction |
| 451 | /// in each block. |
| 452 | static bool ProfitableToMerge(MachineBasicBlock *MBB1, |
| 453 | MachineBasicBlock *MBB2, |
| 454 | unsigned minCommonTailLength, |
| 455 | unsigned &CommonTailLen, |
| 456 | MachineBasicBlock::iterator &I1, |
| 457 | MachineBasicBlock::iterator &I2) { |
| 458 | CommonTailLen = ComputeCommonTailLength(MBB1, MBB2, I1, I2); |
| 459 | MachineFunction *MF = MBB1->getParent(); |
| 460 | |
| 461 | if (CommonTailLen >= minCommonTailLength) |
| 462 | return true; |
| 463 | |
| 464 | if (CommonTailLen == 0) |
| 465 | return false; |
| 466 | |
| 467 | // If we are optimizing for code size, 1 instruction in common is enough if |
| 468 | // we don't have to split a block. At worst we will be replacing a |
| 469 | // fallthrough into the common tail with a branch, which at worst breaks |
| 470 | // even with falling through into the duplicated common tail. |
| 471 | if (MF->getFunction()->hasFnAttr(Attribute::OptimizeForSize) && |
| 472 | (I1 == MBB1->begin() || I2 == MBB2->begin())) |
| 473 | return true; |
| 474 | |
| 475 | return false; |
| 476 | } |
| 477 | |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 478 | /// ComputeSameTails - Look through all the blocks in MergePotentials that have |
| 479 | /// hash CurHash (guaranteed to match the last element). Build the vector |
| 480 | /// SameTails of all those that have the (same) largest number of instructions |
| 481 | /// in common of any pair of these blocks. SameTails entries contain an |
| 482 | /// iterator into MergePotentials (from which the MachineBasicBlock can be |
| 483 | /// found) and a MachineBasicBlock::iterator into that MBB indicating the |
| 484 | /// instruction where the matching code sequence begins. |
| 485 | /// Order of elements in SameTails is the reverse of the order in which |
| 486 | /// those blocks appear in MergePotentials (where they are not necessarily |
| 487 | /// consecutive). |
| 488 | unsigned BranchFolder::ComputeSameTails(unsigned CurHash, |
| 489 | unsigned minCommonTailLength) { |
| 490 | unsigned maxCommonTailLength = 0U; |
| 491 | SameTails.clear(); |
| 492 | MachineBasicBlock::iterator TrialBBI1, TrialBBI2; |
| 493 | MPIterator HighestMPIter = prior(MergePotentials.end()); |
| 494 | for (MPIterator CurMPIter = prior(MergePotentials.end()), |
| 495 | B = MergePotentials.begin(); |
| 496 | CurMPIter!=B && CurMPIter->first==CurHash; |
| 497 | --CurMPIter) { |
| 498 | for (MPIterator I = prior(CurMPIter); I->first==CurHash ; --I) { |
Bob Wilson | 7b888b8 | 2009-10-29 18:40:06 +0000 | [diff] [blame] | 499 | unsigned CommonTailLen; |
| 500 | if (ProfitableToMerge(CurMPIter->second, I->second, minCommonTailLength, |
| 501 | CommonTailLen, TrialBBI1, TrialBBI2)) { |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 502 | if (CommonTailLen > maxCommonTailLength) { |
| 503 | SameTails.clear(); |
| 504 | maxCommonTailLength = CommonTailLen; |
| 505 | HighestMPIter = CurMPIter; |
| 506 | SameTails.push_back(std::make_pair(CurMPIter, TrialBBI1)); |
| 507 | } |
| 508 | if (HighestMPIter == CurMPIter && |
| 509 | CommonTailLen == maxCommonTailLength) |
| 510 | SameTails.push_back(std::make_pair(I, TrialBBI2)); |
| 511 | } |
| 512 | if (I==B) |
| 513 | break; |
| 514 | } |
| 515 | } |
| 516 | return maxCommonTailLength; |
| 517 | } |
| 518 | |
| 519 | /// RemoveBlocksWithHash - Remove all blocks with hash CurHash from |
| 520 | /// MergePotentials, restoring branches at ends of blocks as appropriate. |
| 521 | void BranchFolder::RemoveBlocksWithHash(unsigned CurHash, |
| 522 | MachineBasicBlock* SuccBB, |
| 523 | MachineBasicBlock* PredBB) { |
Dale Johannesen | 679860e | 2008-05-23 17:19:02 +0000 | [diff] [blame] | 524 | MPIterator CurMPIter, B; |
| 525 | for (CurMPIter = prior(MergePotentials.end()), B = MergePotentials.begin(); |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 526 | CurMPIter->first==CurHash; |
| 527 | --CurMPIter) { |
| 528 | // Put the unconditional branch back, if we need one. |
| 529 | MachineBasicBlock *CurMBB = CurMPIter->second; |
| 530 | if (SuccBB && CurMBB != PredBB) |
| 531 | FixTail(CurMBB, SuccBB, TII); |
Dale Johannesen | 679860e | 2008-05-23 17:19:02 +0000 | [diff] [blame] | 532 | if (CurMPIter==B) |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 533 | break; |
| 534 | } |
Dale Johannesen | 679860e | 2008-05-23 17:19:02 +0000 | [diff] [blame] | 535 | if (CurMPIter->first!=CurHash) |
| 536 | CurMPIter++; |
| 537 | MergePotentials.erase(CurMPIter, MergePotentials.end()); |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Dale Johannesen | 51b2b9e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 540 | /// CreateCommonTailOnlyBlock - None of the blocks to be tail-merged consist |
| 541 | /// only of the common tail. Create a block that does by splitting one. |
| 542 | unsigned BranchFolder::CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB, |
| 543 | unsigned maxCommonTailLength) { |
| 544 | unsigned i, commonTailIndex; |
| 545 | unsigned TimeEstimate = ~0U; |
| 546 | for (i=0, commonTailIndex=0; i<SameTails.size(); i++) { |
| 547 | // Use PredBB if possible; that doesn't require a new branch. |
| 548 | if (SameTails[i].first->second==PredBB) { |
| 549 | commonTailIndex = i; |
| 550 | break; |
| 551 | } |
| 552 | // Otherwise, make a (fairly bogus) choice based on estimate of |
| 553 | // how long it will take the various blocks to execute. |
| 554 | unsigned t = EstimateRuntime(SameTails[i].first->second->begin(), |
| 555 | SameTails[i].second); |
| 556 | if (t<=TimeEstimate) { |
| 557 | TimeEstimate = t; |
| 558 | commonTailIndex = i; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | MachineBasicBlock::iterator BBI = SameTails[commonTailIndex].second; |
| 563 | MachineBasicBlock *MBB = SameTails[commonTailIndex].first->second; |
| 564 | |
Bill Wendling | 3403bcd | 2009-08-22 20:03:00 +0000 | [diff] [blame] | 565 | DEBUG(errs() << "\nSplitting " << MBB->getNumber() << ", size " |
| 566 | << maxCommonTailLength); |
Dale Johannesen | 51b2b9e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 567 | |
| 568 | MachineBasicBlock *newMBB = SplitMBBAt(*MBB, BBI); |
| 569 | SameTails[commonTailIndex].first->second = newMBB; |
| 570 | SameTails[commonTailIndex].second = newMBB->begin(); |
| 571 | // If we split PredBB, newMBB is the new predecessor. |
| 572 | if (PredBB==MBB) |
| 573 | PredBB = newMBB; |
| 574 | |
| 575 | return commonTailIndex; |
| 576 | } |
| 577 | |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 578 | // See if any of the blocks in MergePotentials (which all have a common single |
| 579 | // successor, or all have no successor) can be tail-merged. If there is a |
| 580 | // successor, any blocks in MergePotentials that are not tail-merged and |
| 581 | // are not immediately before Succ must have an unconditional branch to |
| 582 | // Succ added (but the predecessor/successor lists need no adjustment). |
| 583 | // The lone predecessor of Succ that falls through into Succ, |
| 584 | // if any, is given in PredBB. |
| 585 | |
| 586 | bool BranchFolder::TryMergeBlocks(MachineBasicBlock *SuccBB, |
| 587 | MachineBasicBlock* PredBB) { |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 588 | bool MadeChange = false; |
| 589 | |
Evan Cheng | 31886db | 2008-02-19 02:09:37 +0000 | [diff] [blame] | 590 | // It doesn't make sense to save a single instruction since tail merging |
| 591 | // will add a jump. |
| 592 | // FIXME: Ask the target to provide the threshold? |
| 593 | unsigned minCommonTailLength = (SuccBB ? 1 : 2) + 1; |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 594 | |
Bill Wendling | 3403bcd | 2009-08-22 20:03:00 +0000 | [diff] [blame] | 595 | DEBUG(errs() << "\nTryMergeBlocks " << MergePotentials.size() << '\n'); |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 597 | // Sort by hash value so that blocks with identical end sequences sort |
| 598 | // together. |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 599 | std::stable_sort(MergePotentials.begin(), MergePotentials.end(),MergeCompare); |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 600 | |
| 601 | // Walk through equivalence sets looking for actual exact matches. |
| 602 | while (MergePotentials.size() > 1) { |
Dale Johannesen | 6ae83fa | 2008-05-09 21:24:35 +0000 | [diff] [blame] | 603 | unsigned CurHash = prior(MergePotentials.end())->first; |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 604 | |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 605 | // Build SameTails, identifying the set of blocks with this hash code |
| 606 | // and with the maximum number of instructions in common. |
| 607 | unsigned maxCommonTailLength = ComputeSameTails(CurHash, |
| 608 | minCommonTailLength); |
Dale Johannesen | 7aea832 | 2007-05-23 21:07:20 +0000 | [diff] [blame] | 609 | |
Dale Johannesen | a5a2117 | 2007-06-01 23:02:45 +0000 | [diff] [blame] | 610 | // If we didn't find any pair that has at least minCommonTailLength |
Dale Johannesen | 6ae83fa | 2008-05-09 21:24:35 +0000 | [diff] [blame] | 611 | // instructions in common, remove all blocks with this hash code and retry. |
| 612 | if (SameTails.empty()) { |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 613 | RemoveBlocksWithHash(CurHash, SuccBB, PredBB); |
Dale Johannesen | 7aea832 | 2007-05-23 21:07:20 +0000 | [diff] [blame] | 614 | continue; |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 615 | } |
Chris Lattner | 1d08d83 | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 616 | |
Dale Johannesen | 6ae83fa | 2008-05-09 21:24:35 +0000 | [diff] [blame] | 617 | // If one of the blocks is the entire common tail (and not the entry |
Dale Johannesen | 51b2b9e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 618 | // block, which we can't jump to), we can treat all blocks with this same |
| 619 | // tail at once. Use PredBB if that is one of the possibilities, as that |
| 620 | // will not introduce any extra branches. |
| 621 | MachineBasicBlock *EntryBB = MergePotentials.begin()->second-> |
| 622 | getParent()->begin(); |
| 623 | unsigned int commonTailIndex, i; |
| 624 | for (commonTailIndex=SameTails.size(), i=0; i<SameTails.size(); i++) { |
Dale Johannesen | 6ae83fa | 2008-05-09 21:24:35 +0000 | [diff] [blame] | 625 | MachineBasicBlock *MBB = SameTails[i].first->second; |
Dale Johannesen | 51b2b9e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 626 | if (MBB->begin() == SameTails[i].second && MBB != EntryBB) { |
| 627 | commonTailIndex = i; |
| 628 | if (MBB==PredBB) |
| 629 | break; |
Dale Johannesen | 6ae83fa | 2008-05-09 21:24:35 +0000 | [diff] [blame] | 630 | } |
Dale Johannesen | 6ae83fa | 2008-05-09 21:24:35 +0000 | [diff] [blame] | 631 | } |
Dale Johannesen | a5a2117 | 2007-06-01 23:02:45 +0000 | [diff] [blame] | 632 | |
Dale Johannesen | 51b2b9e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 633 | if (commonTailIndex==SameTails.size()) { |
| 634 | // None of the blocks consist entirely of the common tail. |
| 635 | // Split a block so that one does. |
| 636 | commonTailIndex = CreateCommonTailOnlyBlock(PredBB, maxCommonTailLength); |
Chris Lattner | 1d08d83 | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 637 | } |
Dale Johannesen | 51b2b9e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 638 | |
| 639 | MachineBasicBlock *MBB = SameTails[commonTailIndex].first->second; |
| 640 | // MBB is common tail. Adjust all other BB's to jump to this one. |
| 641 | // Traversal must be forwards so erases work. |
Bill Wendling | 3403bcd | 2009-08-22 20:03:00 +0000 | [diff] [blame] | 642 | DEBUG(errs() << "\nUsing common tail " << MBB->getNumber() << " for "); |
Dale Johannesen | 51b2b9e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 643 | for (unsigned int i=0; i<SameTails.size(); ++i) { |
| 644 | if (commonTailIndex==i) |
| 645 | continue; |
Bill Wendling | 3403bcd | 2009-08-22 20:03:00 +0000 | [diff] [blame] | 646 | DEBUG(errs() << SameTails[i].first->second->getNumber() << ","); |
Dale Johannesen | 51b2b9e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 647 | // Hack the end off BB i, making it jump to BB commonTailIndex instead. |
| 648 | ReplaceTailWithBranchTo(SameTails[i].second, MBB); |
| 649 | // BB i is no longer a predecessor of SuccBB; remove it from the worklist. |
| 650 | MergePotentials.erase(SameTails[i].first); |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 651 | } |
Bill Wendling | 3403bcd | 2009-08-22 20:03:00 +0000 | [diff] [blame] | 652 | DEBUG(errs() << "\n"); |
Dale Johannesen | 51b2b9e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 653 | // We leave commonTailIndex in the worklist in case there are other blocks |
| 654 | // that match it with a smaller number of instructions. |
Chris Lattner | 1d08d83 | 2006-11-01 01:16:12 +0000 | [diff] [blame] | 655 | MadeChange = true; |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 656 | } |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 657 | return MadeChange; |
| 658 | } |
| 659 | |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 660 | bool BranchFolder::TailMergeBlocks(MachineFunction &MF) { |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 661 | |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 662 | if (!EnableTailMerge) return false; |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 663 | |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 664 | bool MadeChange = false; |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 665 | |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 666 | // First find blocks with no successors. |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 667 | MergePotentials.clear(); |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 668 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) { |
| 669 | if (I->succ_empty()) |
Dale Johannesen | 7aea832 | 2007-05-23 21:07:20 +0000 | [diff] [blame] | 670 | MergePotentials.push_back(std::make_pair(HashEndOfMBB(I, 2U), I)); |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 671 | } |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 672 | // See if we can do any tail merging on those. |
Dale Johannesen | 6ae83fa | 2008-05-09 21:24:35 +0000 | [diff] [blame] | 673 | if (MergePotentials.size() < TailMergeThreshold && |
| 674 | MergePotentials.size() >= 2) |
Dale Johannesen | 53af4c0 | 2007-06-08 00:34:27 +0000 | [diff] [blame] | 675 | MadeChange |= TryMergeBlocks(NULL, NULL); |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 676 | |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 677 | // Look at blocks (IBB) with multiple predecessors (PBB). |
| 678 | // We change each predecessor to a canonical form, by |
| 679 | // (1) temporarily removing any unconditional branch from the predecessor |
| 680 | // to IBB, and |
| 681 | // (2) alter conditional branches so they branch to the other block |
| 682 | // not IBB; this may require adding back an unconditional branch to IBB |
| 683 | // later, where there wasn't one coming in. E.g. |
| 684 | // Bcc IBB |
| 685 | // fallthrough to QBB |
| 686 | // here becomes |
| 687 | // Bncc QBB |
| 688 | // with a conceptual B to IBB after that, which never actually exists. |
| 689 | // With those changes, we see whether the predecessors' tails match, |
| 690 | // and merge them if so. We change things out of canonical form and |
| 691 | // back to the way they were later in the process. (OptimizeBranches |
| 692 | // would undo some of this, but we can't use it, because we'd get into |
| 693 | // a compile-time infinite loop repeatedly doing and undoing the same |
| 694 | // transformations.) |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 695 | |
| 696 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) { |
Dale Johannesen | 62bc8a4 | 2008-07-01 21:50:14 +0000 | [diff] [blame] | 697 | if (I->pred_size() >= 2 && I->pred_size() < TailMergeThreshold) { |
Dan Gohman | da65822 | 2009-08-18 15:18:18 +0000 | [diff] [blame] | 698 | SmallPtrSet<MachineBasicBlock *, 8> UniquePreds; |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 699 | MachineBasicBlock *IBB = I; |
| 700 | MachineBasicBlock *PredBB = prior(I); |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 701 | MergePotentials.clear(); |
Dale Johannesen | 1a90a5a | 2007-06-08 01:08:52 +0000 | [diff] [blame] | 702 | for (MachineBasicBlock::pred_iterator P = I->pred_begin(), |
| 703 | E2 = I->pred_end(); |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 704 | P != E2; ++P) { |
| 705 | MachineBasicBlock* PBB = *P; |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 706 | // Skip blocks that loop to themselves, can't tail merge these. |
| 707 | if (PBB==IBB) |
| 708 | continue; |
Dan Gohman | da65822 | 2009-08-18 15:18:18 +0000 | [diff] [blame] | 709 | // Visit each predecessor only once. |
| 710 | if (!UniquePreds.insert(PBB)) |
| 711 | continue; |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 712 | MachineBasicBlock *TBB = 0, *FBB = 0; |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 713 | SmallVector<MachineOperand, 4> Cond; |
Evan Cheng | dc54d31 | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 714 | if (!TII->AnalyzeBranch(*PBB, TBB, FBB, Cond, true)) { |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 715 | // Failing case: IBB is the target of a cbr, and |
| 716 | // we cannot reverse the branch. |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 717 | SmallVector<MachineOperand, 4> NewCond(Cond); |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 718 | if (!Cond.empty() && TBB==IBB) { |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 719 | if (TII->ReverseBranchCondition(NewCond)) |
| 720 | continue; |
| 721 | // This is the QBB case described above |
| 722 | if (!FBB) |
| 723 | FBB = next(MachineFunction::iterator(PBB)); |
| 724 | } |
Dale Johannesen | fe7e397 | 2007-06-04 23:52:54 +0000 | [diff] [blame] | 725 | // Failing case: the only way IBB can be reached from PBB is via |
| 726 | // exception handling. Happens for landing pads. Would be nice |
| 727 | // to have a bit in the edge so we didn't have to do all this. |
| 728 | if (IBB->isLandingPad()) { |
| 729 | MachineFunction::iterator IP = PBB; IP++; |
| 730 | MachineBasicBlock* PredNextBB = NULL; |
| 731 | if (IP!=MF.end()) |
| 732 | PredNextBB = IP; |
| 733 | if (TBB==NULL) { |
| 734 | if (IBB!=PredNextBB) // fallthrough |
| 735 | continue; |
| 736 | } else if (FBB) { |
| 737 | if (TBB!=IBB && FBB!=IBB) // cbr then ubr |
| 738 | continue; |
Dan Gohman | 3035959 | 2008-01-29 13:02:09 +0000 | [diff] [blame] | 739 | } else if (Cond.empty()) { |
Dale Johannesen | fe7e397 | 2007-06-04 23:52:54 +0000 | [diff] [blame] | 740 | if (TBB!=IBB) // ubr |
| 741 | continue; |
| 742 | } else { |
| 743 | if (TBB!=IBB && IBB!=PredNextBB) // cbr |
| 744 | continue; |
| 745 | } |
| 746 | } |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 747 | // Remove the unconditional branch at the end, if any. |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 748 | if (TBB && (Cond.empty() || FBB)) { |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 749 | TII->RemoveBranch(*PBB); |
Dale Johannesen | 6b8583c | 2008-05-09 23:28:24 +0000 | [diff] [blame] | 750 | if (!Cond.empty()) |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 751 | // reinsert conditional branch only, for now |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 752 | TII->InsertBranch(*PBB, (TBB==IBB) ? FBB : TBB, 0, NewCond); |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 753 | } |
Dale Johannesen | 7aea832 | 2007-05-23 21:07:20 +0000 | [diff] [blame] | 754 | MergePotentials.push_back(std::make_pair(HashEndOfMBB(PBB, 1U), *P)); |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | if (MergePotentials.size() >= 2) |
| 758 | MadeChange |= TryMergeBlocks(I, PredBB); |
| 759 | // Reinsert an unconditional branch if needed. |
Dale Johannesen | 51b2b9e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 760 | // The 1 below can occur as a result of removing blocks in TryMergeBlocks. |
Dale Johannesen | 1cf08c1 | 2007-05-18 01:28:58 +0000 | [diff] [blame] | 761 | PredBB = prior(I); // this may have been changed in TryMergeBlocks |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 762 | if (MergePotentials.size()==1 && |
Dale Johannesen | 51b2b9e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 763 | MergePotentials.begin()->second != PredBB) |
| 764 | FixTail(MergePotentials.begin()->second, I, TII); |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 765 | } |
| 766 | } |
Dale Johannesen | 7d33b4c | 2007-05-07 20:57:21 +0000 | [diff] [blame] | 767 | return MadeChange; |
| 768 | } |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 769 | |
| 770 | //===----------------------------------------------------------------------===// |
| 771 | // Branch Optimization |
| 772 | //===----------------------------------------------------------------------===// |
| 773 | |
| 774 | bool BranchFolder::OptimizeBranches(MachineFunction &MF) { |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 775 | bool MadeChange = false; |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 776 | |
Dale Johannesen | 6b896ce | 2007-02-17 00:44:34 +0000 | [diff] [blame] | 777 | // Make sure blocks are numbered in order |
| 778 | MF.RenumberBlocks(); |
| 779 | |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 780 | for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) { |
| 781 | MachineBasicBlock *MBB = I++; |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 782 | MadeChange |= OptimizeBlock(MBB); |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 783 | |
| 784 | // If it is dead, remove it. |
Jim Laskey | 033c971 | 2007-02-22 16:39:03 +0000 | [diff] [blame] | 785 | if (MBB->pred_empty()) { |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 786 | RemoveDeadBlock(MBB); |
| 787 | MadeChange = true; |
| 788 | ++NumDeadBlocks; |
| 789 | } |
| 790 | } |
| 791 | return MadeChange; |
| 792 | } |
| 793 | |
| 794 | |
Chris Lattner | 6b0e3f8 | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 795 | /// CanFallThrough - Return true if the specified block (with the specified |
| 796 | /// branch condition) can implicitly transfer control to the block after it by |
| 797 | /// falling off the end of it. This should return false if it can reach the |
| 798 | /// block after it, but it uses an explicit branch to do so (e.g. a table jump). |
| 799 | /// |
| 800 | /// True is a conservative answer. |
| 801 | /// |
| 802 | bool BranchFolder::CanFallThrough(MachineBasicBlock *CurBB, |
| 803 | bool BranchUnAnalyzable, |
Dale Johannesen | 51b2b9e | 2008-05-12 20:33:57 +0000 | [diff] [blame] | 804 | MachineBasicBlock *TBB, |
| 805 | MachineBasicBlock *FBB, |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 806 | const SmallVectorImpl<MachineOperand> &Cond) { |
Chris Lattner | 6b0e3f8 | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 807 | MachineFunction::iterator Fallthrough = CurBB; |
| 808 | ++Fallthrough; |
| 809 | // If FallthroughBlock is off the end of the function, it can't fall through. |
| 810 | if (Fallthrough == CurBB->getParent()->end()) |
| 811 | return false; |
| 812 | |
| 813 | // If FallthroughBlock isn't a successor of CurBB, no fallthrough is possible. |
| 814 | if (!CurBB->isSuccessor(Fallthrough)) |
| 815 | return false; |
| 816 | |
| 817 | // If we couldn't analyze the branch, assume it could fall through. |
| 818 | if (BranchUnAnalyzable) return true; |
| 819 | |
Chris Lattner | 7d09784 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 820 | // If there is no branch, control always falls through. |
| 821 | if (TBB == 0) return true; |
| 822 | |
| 823 | // If there is some explicit branch to the fallthrough block, it can obviously |
| 824 | // reach, even though the branch should get folded to fall through implicitly. |
Chris Lattner | 6b0e3f8 | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 825 | if (MachineFunction::iterator(TBB) == Fallthrough || |
| 826 | MachineFunction::iterator(FBB) == Fallthrough) |
Chris Lattner | 7d09784 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 827 | return true; |
| 828 | |
| 829 | // If it's an unconditional branch to some block not the fall through, it |
| 830 | // doesn't fall through. |
| 831 | if (Cond.empty()) return false; |
| 832 | |
| 833 | // Otherwise, if it is conditional and has no explicit false block, it falls |
| 834 | // through. |
Chris Lattner | c2e91e3 | 2006-10-25 22:21:37 +0000 | [diff] [blame] | 835 | return FBB == 0; |
Chris Lattner | 7d09784 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Chris Lattner | 6b0e3f8 | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 838 | /// CanFallThrough - Return true if the specified can implicitly transfer |
| 839 | /// control to the block after it by falling off the end of it. This should |
| 840 | /// return false if it can reach the block after it, but it uses an explicit |
| 841 | /// branch to do so (e.g. a table jump). |
| 842 | /// |
| 843 | /// True is a conservative answer. |
| 844 | /// |
| 845 | bool BranchFolder::CanFallThrough(MachineBasicBlock *CurBB) { |
| 846 | MachineBasicBlock *TBB = 0, *FBB = 0; |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 847 | SmallVector<MachineOperand, 4> Cond; |
Evan Cheng | dc54d31 | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 848 | bool CurUnAnalyzable = TII->AnalyzeBranch(*CurBB, TBB, FBB, Cond, true); |
Chris Lattner | 6b0e3f8 | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 849 | return CanFallThrough(CurBB, CurUnAnalyzable, TBB, FBB, Cond); |
| 850 | } |
| 851 | |
Chris Lattner | a7bef4a | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 852 | /// IsBetterFallthrough - Return true if it would be clearly better to |
| 853 | /// fall-through to MBB1 than to fall through into MBB2. This has to return |
| 854 | /// a strict ordering, returning true for both (MBB1,MBB2) and (MBB2,MBB1) will |
| 855 | /// result in infinite loops. |
| 856 | static bool IsBetterFallthrough(MachineBasicBlock *MBB1, |
Chris Lattner | 6924430 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 857 | MachineBasicBlock *MBB2) { |
Chris Lattner | 154e104 | 2006-11-18 21:30:35 +0000 | [diff] [blame] | 858 | // Right now, we use a simple heuristic. If MBB2 ends with a call, and |
| 859 | // MBB1 doesn't, we prefer to fall through into MBB1. This allows us to |
Chris Lattner | a7bef4a | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 860 | // optimize branches that branch to either a return block or an assert block |
| 861 | // into a fallthrough to the return. |
| 862 | if (MBB1->empty() || MBB2->empty()) return false; |
Christopher Lamb | 11a4f64 | 2007-12-10 07:24:06 +0000 | [diff] [blame] | 863 | |
| 864 | // If there is a clear successor ordering we make sure that one block |
| 865 | // will fall through to the next |
| 866 | if (MBB1->isSuccessor(MBB2)) return true; |
| 867 | if (MBB2->isSuccessor(MBB1)) return false; |
Chris Lattner | a7bef4a | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 868 | |
| 869 | MachineInstr *MBB1I = --MBB1->end(); |
| 870 | MachineInstr *MBB2I = --MBB2->end(); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 871 | return MBB2I->getDesc().isCall() && !MBB1I->getDesc().isCall(); |
Chris Lattner | a7bef4a | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 874 | /// OptimizeBlock - Analyze and optimize control flow related to the specified |
| 875 | /// block. This is never called on the entry block. |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 876 | bool BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) { |
| 877 | bool MadeChange = false; |
| 878 | |
Chris Lattner | 7d09784 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 879 | MachineFunction::iterator FallThrough = MBB; |
| 880 | ++FallThrough; |
| 881 | |
Chris Lattner | eb15eee | 2006-10-13 20:43:10 +0000 | [diff] [blame] | 882 | // If this block is empty, make everyone use its fall-through, not the block |
Dale Johannesen | a52dd15 | 2007-05-31 21:54:00 +0000 | [diff] [blame] | 883 | // explicitly. Landing pads should not do this since the landing-pad table |
Dan Gohman | ab91810 | 2009-10-30 02:13:27 +0000 | [diff] [blame^] | 884 | // points to this block. Blocks with their addresses taken shouldn't be |
| 885 | // optimized away. |
| 886 | if (MBB->empty() && !MBB->isLandingPad() && !MBB->hasAddressTaken()) { |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 887 | // Dead block? Leave for cleanup later. |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 888 | if (MBB->pred_empty()) return MadeChange; |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 889 | |
Chris Lattner | c50ffcb | 2006-10-17 17:13:52 +0000 | [diff] [blame] | 890 | if (FallThrough == MBB->getParent()->end()) { |
| 891 | // TODO: Simplify preds to not branch here if possible! |
| 892 | } else { |
| 893 | // Rewrite all predecessors of the old block to go to the fallthrough |
| 894 | // instead. |
Jim Laskey | 033c971 | 2007-02-22 16:39:03 +0000 | [diff] [blame] | 895 | while (!MBB->pred_empty()) { |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 896 | MachineBasicBlock *Pred = *(MBB->pred_end()-1); |
Evan Cheng | 0370fad | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 897 | Pred->ReplaceUsesOfBlockWith(MBB, FallThrough); |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 898 | } |
Chris Lattner | c50ffcb | 2006-10-17 17:13:52 +0000 | [diff] [blame] | 899 | // If MBB was the target of a jump table, update jump tables to go to the |
| 900 | // fallthrough instead. |
Chris Lattner | 6acfe12 | 2006-10-28 18:34:47 +0000 | [diff] [blame] | 901 | MBB->getParent()->getJumpTableInfo()-> |
| 902 | ReplaceMBBInJumpTables(MBB, FallThrough); |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 903 | MadeChange = true; |
Chris Lattner | 21ab22e | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 904 | } |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 905 | return MadeChange; |
Chris Lattner | 21ab22e | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 908 | // Check to see if we can simplify the terminator of the block before this |
| 909 | // one. |
Chris Lattner | 7d09784 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 910 | MachineBasicBlock &PrevBB = *prior(MachineFunction::iterator(MBB)); |
Chris Lattner | ffddf6b | 2006-10-17 18:16:40 +0000 | [diff] [blame] | 911 | |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 912 | MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0; |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 913 | SmallVector<MachineOperand, 4> PriorCond; |
Chris Lattner | 6b0e3f8 | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 914 | bool PriorUnAnalyzable = |
Evan Cheng | dc54d31 | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 915 | TII->AnalyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, true); |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 916 | if (!PriorUnAnalyzable) { |
| 917 | // If the CFG for the prior block has extra edges, remove them. |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 918 | MadeChange |= PrevBB.CorrectExtraCFGEdges(PriorTBB, PriorFBB, |
| 919 | !PriorCond.empty()); |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 920 | |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 921 | // If the previous branch is conditional and both conditions go to the same |
Chris Lattner | 2d47bd9 | 2006-10-21 05:43:30 +0000 | [diff] [blame] | 922 | // destination, remove the branch, replacing it with an unconditional one or |
| 923 | // a fall-through. |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 924 | if (PriorTBB && PriorTBB == PriorFBB) { |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 925 | TII->RemoveBranch(PrevBB); |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 926 | PriorCond.clear(); |
Chris Lattner | 7d09784 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 927 | if (PriorTBB != MBB) |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 928 | TII->InsertBranch(PrevBB, PriorTBB, 0, PriorCond); |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 929 | MadeChange = true; |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 930 | ++NumBranchOpts; |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 931 | return OptimizeBlock(MBB); |
| 932 | } |
| 933 | |
| 934 | // If the previous branch *only* branches to *this* block (conditional or |
| 935 | // not) remove the branch. |
Chris Lattner | 7d09784 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 936 | if (PriorTBB == MBB && PriorFBB == 0) { |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 937 | TII->RemoveBranch(PrevBB); |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 938 | MadeChange = true; |
Chris Lattner | 1214305 | 2006-10-21 00:47:49 +0000 | [diff] [blame] | 939 | ++NumBranchOpts; |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 940 | return OptimizeBlock(MBB); |
| 941 | } |
Chris Lattner | 2d47bd9 | 2006-10-21 05:43:30 +0000 | [diff] [blame] | 942 | |
| 943 | // If the prior block branches somewhere else on the condition and here if |
| 944 | // the condition is false, remove the uncond second branch. |
Chris Lattner | 7d09784 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 945 | if (PriorFBB == MBB) { |
Chris Lattner | 2d47bd9 | 2006-10-21 05:43:30 +0000 | [diff] [blame] | 946 | TII->RemoveBranch(PrevBB); |
| 947 | TII->InsertBranch(PrevBB, PriorTBB, 0, PriorCond); |
| 948 | MadeChange = true; |
| 949 | ++NumBranchOpts; |
| 950 | return OptimizeBlock(MBB); |
| 951 | } |
Chris Lattner | a2d7995 | 2006-10-21 05:54:00 +0000 | [diff] [blame] | 952 | |
| 953 | // If the prior block branches here on true and somewhere else on false, and |
| 954 | // if the branch condition is reversible, reverse the branch to create a |
| 955 | // fall-through. |
Chris Lattner | 7d09784 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 956 | if (PriorTBB == MBB) { |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 957 | SmallVector<MachineOperand, 4> NewPriorCond(PriorCond); |
Chris Lattner | a2d7995 | 2006-10-21 05:54:00 +0000 | [diff] [blame] | 958 | if (!TII->ReverseBranchCondition(NewPriorCond)) { |
| 959 | TII->RemoveBranch(PrevBB); |
| 960 | TII->InsertBranch(PrevBB, PriorFBB, 0, NewPriorCond); |
| 961 | MadeChange = true; |
| 962 | ++NumBranchOpts; |
| 963 | return OptimizeBlock(MBB); |
| 964 | } |
| 965 | } |
Chris Lattner | a7bef4a | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 966 | |
Dan Gohman | 6d31268 | 2009-10-22 00:03:58 +0000 | [diff] [blame] | 967 | // If this block has no successors (e.g. it is a return block or ends with |
| 968 | // a call to a no-return function like abort or __cxa_throw) and if the pred |
| 969 | // falls through into this block, and if it would otherwise fall through |
| 970 | // into the block after this, move this block to the end of the function. |
Chris Lattner | 154e104 | 2006-11-18 21:30:35 +0000 | [diff] [blame] | 971 | // |
Chris Lattner | a7bef4a | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 972 | // We consider it more likely that execution will stay in the function (e.g. |
| 973 | // due to loops) than it is to exit it. This asserts in loops etc, moving |
| 974 | // the assert condition out of the loop body. |
Dan Gohman | 6d31268 | 2009-10-22 00:03:58 +0000 | [diff] [blame] | 975 | if (MBB->succ_empty() && !PriorCond.empty() && PriorFBB == 0 && |
Chris Lattner | 154e104 | 2006-11-18 21:30:35 +0000 | [diff] [blame] | 976 | MachineFunction::iterator(PriorTBB) == FallThrough && |
| 977 | !CanFallThrough(MBB)) { |
Chris Lattner | f10a56a | 2006-11-18 21:56:39 +0000 | [diff] [blame] | 978 | bool DoTransform = true; |
| 979 | |
Chris Lattner | a7bef4a | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 980 | // We have to be careful that the succs of PredBB aren't both no-successor |
| 981 | // blocks. If neither have successors and if PredBB is the second from |
| 982 | // last block in the function, we'd just keep swapping the two blocks for |
| 983 | // last. Only do the swap if one is clearly better to fall through than |
| 984 | // the other. |
Chris Lattner | f10a56a | 2006-11-18 21:56:39 +0000 | [diff] [blame] | 985 | if (FallThrough == --MBB->getParent()->end() && |
Chris Lattner | 6924430 | 2008-01-07 01:56:04 +0000 | [diff] [blame] | 986 | !IsBetterFallthrough(PriorTBB, MBB)) |
Chris Lattner | f10a56a | 2006-11-18 21:56:39 +0000 | [diff] [blame] | 987 | DoTransform = false; |
| 988 | |
| 989 | // We don't want to do this transformation if we have control flow like: |
| 990 | // br cond BB2 |
| 991 | // BB1: |
| 992 | // .. |
| 993 | // jmp BBX |
| 994 | // BB2: |
| 995 | // .. |
| 996 | // ret |
| 997 | // |
| 998 | // In this case, we could actually be moving the return block *into* a |
| 999 | // loop! |
Chris Lattner | 4b10591 | 2006-11-18 22:25:39 +0000 | [diff] [blame] | 1000 | if (DoTransform && !MBB->succ_empty() && |
| 1001 | (!CanFallThrough(PriorTBB) || PriorTBB->empty())) |
Chris Lattner | f10a56a | 2006-11-18 21:56:39 +0000 | [diff] [blame] | 1002 | DoTransform = false; |
Chris Lattner | a7bef4a | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 1003 | |
Chris Lattner | f10a56a | 2006-11-18 21:56:39 +0000 | [diff] [blame] | 1004 | |
| 1005 | if (DoTransform) { |
Chris Lattner | a7bef4a | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 1006 | // Reverse the branch so we will fall through on the previous true cond. |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1007 | SmallVector<MachineOperand, 4> NewPriorCond(PriorCond); |
Chris Lattner | a7bef4a | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 1008 | if (!TII->ReverseBranchCondition(NewPriorCond)) { |
Bill Wendling | 3403bcd | 2009-08-22 20:03:00 +0000 | [diff] [blame] | 1009 | DEBUG(errs() << "\nMoving MBB: " << *MBB |
| 1010 | << "To make fallthrough to: " << *PriorTBB << "\n"); |
Chris Lattner | f10a56a | 2006-11-18 21:56:39 +0000 | [diff] [blame] | 1011 | |
Chris Lattner | a7bef4a | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 1012 | TII->RemoveBranch(PrevBB); |
| 1013 | TII->InsertBranch(PrevBB, MBB, 0, NewPriorCond); |
| 1014 | |
| 1015 | // Move this block to the end of the function. |
| 1016 | MBB->moveAfter(--MBB->getParent()->end()); |
| 1017 | MadeChange = true; |
| 1018 | ++NumBranchOpts; |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 1019 | return MadeChange; |
Chris Lattner | a7bef4a | 2006-11-18 20:47:54 +0000 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | } |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 1023 | } |
Chris Lattner | 7821a8a | 2006-10-14 00:21:48 +0000 | [diff] [blame] | 1024 | |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1025 | // Analyze the branch in the current block. |
| 1026 | MachineBasicBlock *CurTBB = 0, *CurFBB = 0; |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1027 | SmallVector<MachineOperand, 4> CurCond; |
Evan Cheng | dc54d31 | 2009-02-09 07:14:22 +0000 | [diff] [blame] | 1028 | bool CurUnAnalyzable= TII->AnalyzeBranch(*MBB, CurTBB, CurFBB, CurCond, true); |
Chris Lattner | 6b0e3f8 | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1029 | if (!CurUnAnalyzable) { |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1030 | // If the CFG for the prior block has extra edges, remove them. |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1031 | MadeChange |= MBB->CorrectExtraCFGEdges(CurTBB, CurFBB, !CurCond.empty()); |
Chris Lattner | eb15eee | 2006-10-13 20:43:10 +0000 | [diff] [blame] | 1032 | |
Chris Lattner | 5d05695 | 2006-11-08 01:03:21 +0000 | [diff] [blame] | 1033 | // If this is a two-way branch, and the FBB branches to this block, reverse |
| 1034 | // the condition so the single-basic-block loop is faster. Instead of: |
| 1035 | // Loop: xxx; jcc Out; jmp Loop |
| 1036 | // we want: |
| 1037 | // Loop: xxx; jncc Loop; jmp Out |
| 1038 | if (CurTBB && CurFBB && CurFBB == MBB && CurTBB != MBB) { |
Owen Anderson | 44eb65c | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1039 | SmallVector<MachineOperand, 4> NewCond(CurCond); |
Chris Lattner | 5d05695 | 2006-11-08 01:03:21 +0000 | [diff] [blame] | 1040 | if (!TII->ReverseBranchCondition(NewCond)) { |
| 1041 | TII->RemoveBranch(*MBB); |
| 1042 | TII->InsertBranch(*MBB, CurFBB, CurTBB, NewCond); |
| 1043 | MadeChange = true; |
| 1044 | ++NumBranchOpts; |
| 1045 | return OptimizeBlock(MBB); |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1050 | // If this branch is the only thing in its block, see if we can forward |
| 1051 | // other blocks across it. |
| 1052 | if (CurTBB && CurCond.empty() && CurFBB == 0 && |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 1053 | MBB->begin()->getDesc().isBranch() && CurTBB != MBB) { |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1054 | // This block may contain just an unconditional branch. Because there can |
| 1055 | // be 'non-branch terminators' in the block, try removing the branch and |
| 1056 | // then seeing if the block is empty. |
| 1057 | TII->RemoveBranch(*MBB); |
| 1058 | |
| 1059 | // If this block is just an unconditional branch to CurTBB, we can |
| 1060 | // usually completely eliminate the block. The only case we cannot |
| 1061 | // completely eliminate the block is when the block before this one |
| 1062 | // falls through into MBB and we can't understand the prior block's branch |
| 1063 | // condition. |
Chris Lattner | cf420cc | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1064 | if (MBB->empty()) { |
| 1065 | bool PredHasNoFallThrough = TII->BlockHasNoFallThrough(PrevBB); |
| 1066 | if (PredHasNoFallThrough || !PriorUnAnalyzable || |
| 1067 | !PrevBB.isSuccessor(MBB)) { |
| 1068 | // If the prior block falls through into us, turn it into an |
| 1069 | // explicit branch to us to make updates simpler. |
| 1070 | if (!PredHasNoFallThrough && PrevBB.isSuccessor(MBB) && |
| 1071 | PriorTBB != MBB && PriorFBB != MBB) { |
| 1072 | if (PriorTBB == 0) { |
Chris Lattner | 6acfe12 | 2006-10-28 18:34:47 +0000 | [diff] [blame] | 1073 | assert(PriorCond.empty() && PriorFBB == 0 && |
| 1074 | "Bad branch analysis"); |
Chris Lattner | cf420cc | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1075 | PriorTBB = MBB; |
| 1076 | } else { |
| 1077 | assert(PriorFBB == 0 && "Machine CFG out of date!"); |
| 1078 | PriorFBB = MBB; |
| 1079 | } |
| 1080 | TII->RemoveBranch(PrevBB); |
| 1081 | TII->InsertBranch(PrevBB, PriorTBB, PriorFBB, PriorCond); |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1082 | } |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1083 | |
Chris Lattner | cf420cc | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1084 | // Iterate through all the predecessors, revectoring each in-turn. |
David Greene | 8a46d34 | 2007-06-29 02:45:24 +0000 | [diff] [blame] | 1085 | size_t PI = 0; |
Chris Lattner | cf420cc | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1086 | bool DidChange = false; |
| 1087 | bool HasBranchToSelf = false; |
David Greene | 8a46d34 | 2007-06-29 02:45:24 +0000 | [diff] [blame] | 1088 | while(PI != MBB->pred_size()) { |
| 1089 | MachineBasicBlock *PMBB = *(MBB->pred_begin() + PI); |
| 1090 | if (PMBB == MBB) { |
Chris Lattner | cf420cc | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1091 | // If this block has an uncond branch to itself, leave it. |
| 1092 | ++PI; |
| 1093 | HasBranchToSelf = true; |
| 1094 | } else { |
| 1095 | DidChange = true; |
David Greene | 8a46d34 | 2007-06-29 02:45:24 +0000 | [diff] [blame] | 1096 | PMBB->ReplaceUsesOfBlockWith(MBB, CurTBB); |
Dale Johannesen | bf06f6a | 2009-05-11 21:54:13 +0000 | [diff] [blame] | 1097 | // If this change resulted in PMBB ending in a conditional |
| 1098 | // branch where both conditions go to the same destination, |
| 1099 | // change this to an unconditional branch (and fix the CFG). |
| 1100 | MachineBasicBlock *NewCurTBB = 0, *NewCurFBB = 0; |
| 1101 | SmallVector<MachineOperand, 4> NewCurCond; |
| 1102 | bool NewCurUnAnalyzable = TII->AnalyzeBranch(*PMBB, NewCurTBB, |
| 1103 | NewCurFBB, NewCurCond, true); |
| 1104 | if (!NewCurUnAnalyzable && NewCurTBB && NewCurTBB == NewCurFBB) { |
| 1105 | TII->RemoveBranch(*PMBB); |
| 1106 | NewCurCond.clear(); |
| 1107 | TII->InsertBranch(*PMBB, NewCurTBB, 0, NewCurCond); |
| 1108 | MadeChange = true; |
| 1109 | ++NumBranchOpts; |
| 1110 | PMBB->CorrectExtraCFGEdges(NewCurTBB, NewCurFBB, false); |
| 1111 | } |
Chris Lattner | cf420cc | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1112 | } |
Chris Lattner | 4bc135e | 2006-10-21 06:11:43 +0000 | [diff] [blame] | 1113 | } |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1114 | |
Chris Lattner | cf420cc | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1115 | // Change any jumptables to go to the new MBB. |
Chris Lattner | 6acfe12 | 2006-10-28 18:34:47 +0000 | [diff] [blame] | 1116 | MBB->getParent()->getJumpTableInfo()-> |
| 1117 | ReplaceMBBInJumpTables(MBB, CurTBB); |
Chris Lattner | cf420cc | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1118 | if (DidChange) { |
| 1119 | ++NumBranchOpts; |
| 1120 | MadeChange = true; |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 1121 | if (!HasBranchToSelf) return MadeChange; |
Chris Lattner | cf420cc | 2006-10-28 17:32:47 +0000 | [diff] [blame] | 1122 | } |
Chris Lattner | 4bc135e | 2006-10-21 06:11:43 +0000 | [diff] [blame] | 1123 | } |
Chris Lattner | eb15eee | 2006-10-13 20:43:10 +0000 | [diff] [blame] | 1124 | } |
Chris Lattner | eb15eee | 2006-10-13 20:43:10 +0000 | [diff] [blame] | 1125 | |
Chris Lattner | 386e290 | 2006-10-21 05:08:28 +0000 | [diff] [blame] | 1126 | // Add the branch back if the block is more than just an uncond branch. |
| 1127 | TII->InsertBranch(*MBB, CurTBB, 0, CurCond); |
Chris Lattner | 21ab22e | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 1128 | } |
Chris Lattner | 6b0e3f8 | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | // If the prior block doesn't fall through into this block, and if this |
| 1132 | // block doesn't fall through into some other block, see if we can find a |
| 1133 | // place to move this block where a fall-through will happen. |
| 1134 | if (!CanFallThrough(&PrevBB, PriorUnAnalyzable, |
| 1135 | PriorTBB, PriorFBB, PriorCond)) { |
| 1136 | // Now we know that there was no fall-through into this block, check to |
| 1137 | // see if it has a fall-through into its successor. |
Dale Johannesen | 6b896ce | 2007-02-17 00:44:34 +0000 | [diff] [blame] | 1138 | bool CurFallsThru = CanFallThrough(MBB, CurUnAnalyzable, CurTBB, CurFBB, |
Chris Lattner | 77edc4b | 2007-04-30 23:35:00 +0000 | [diff] [blame] | 1139 | CurCond); |
Dale Johannesen | 6b896ce | 2007-02-17 00:44:34 +0000 | [diff] [blame] | 1140 | |
Jim Laskey | 02b3f5e | 2007-02-21 22:42:20 +0000 | [diff] [blame] | 1141 | if (!MBB->isLandingPad()) { |
| 1142 | // Check all the predecessors of this block. If one of them has no fall |
| 1143 | // throughs, move this block right after it. |
| 1144 | for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(), |
| 1145 | E = MBB->pred_end(); PI != E; ++PI) { |
| 1146 | // Analyze the branch at the end of the pred. |
| 1147 | MachineBasicBlock *PredBB = *PI; |
| 1148 | MachineFunction::iterator PredFallthrough = PredBB; ++PredFallthrough; |
| 1149 | if (PredBB != MBB && !CanFallThrough(PredBB) |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 1150 | && (!CurFallsThru || !CurTBB || !CurFBB) |
Jim Laskey | 02b3f5e | 2007-02-21 22:42:20 +0000 | [diff] [blame] | 1151 | && (!CurFallsThru || MBB->getNumber() >= PredBB->getNumber())) { |
| 1152 | // If the current block doesn't fall through, just move it. |
| 1153 | // If the current block can fall through and does not end with a |
| 1154 | // conditional branch, we need to append an unconditional jump to |
| 1155 | // the (current) next block. To avoid a possible compile-time |
| 1156 | // infinite loop, move blocks only backward in this case. |
Dale Johannesen | 76b38fc | 2007-05-10 01:01:49 +0000 | [diff] [blame] | 1157 | // Also, if there are already 2 branches here, we cannot add a third; |
| 1158 | // this means we have the case |
| 1159 | // Bcc next |
| 1160 | // B elsewhere |
| 1161 | // next: |
Jim Laskey | 02b3f5e | 2007-02-21 22:42:20 +0000 | [diff] [blame] | 1162 | if (CurFallsThru) { |
| 1163 | MachineBasicBlock *NextBB = next(MachineFunction::iterator(MBB)); |
| 1164 | CurCond.clear(); |
| 1165 | TII->InsertBranch(*MBB, NextBB, 0, CurCond); |
| 1166 | } |
| 1167 | MBB->moveAfter(PredBB); |
| 1168 | MadeChange = true; |
| 1169 | return OptimizeBlock(MBB); |
Chris Lattner | 7d09784 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 1170 | } |
| 1171 | } |
Dale Johannesen | 6b896ce | 2007-02-17 00:44:34 +0000 | [diff] [blame] | 1172 | } |
Chris Lattner | 6b0e3f8 | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1173 | |
Dale Johannesen | 6b896ce | 2007-02-17 00:44:34 +0000 | [diff] [blame] | 1174 | if (!CurFallsThru) { |
Chris Lattner | 6b0e3f8 | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1175 | // Check all successors to see if we can move this block before it. |
| 1176 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), |
| 1177 | E = MBB->succ_end(); SI != E; ++SI) { |
| 1178 | // Analyze the branch at the end of the block before the succ. |
| 1179 | MachineBasicBlock *SuccBB = *SI; |
| 1180 | MachineFunction::iterator SuccPrev = SuccBB; --SuccPrev; |
Chris Lattner | 6b0e3f8 | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1181 | std::vector<MachineOperand> SuccPrevCond; |
Chris Lattner | 77edc4b | 2007-04-30 23:35:00 +0000 | [diff] [blame] | 1182 | |
| 1183 | // If this block doesn't already fall-through to that successor, and if |
| 1184 | // the succ doesn't already have a block that can fall through into it, |
| 1185 | // and if the successor isn't an EH destination, we can arrange for the |
| 1186 | // fallthrough to happen. |
| 1187 | if (SuccBB != MBB && !CanFallThrough(SuccPrev) && |
| 1188 | !SuccBB->isLandingPad()) { |
Chris Lattner | 6b0e3f8 | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1189 | MBB->moveBefore(SuccBB); |
| 1190 | MadeChange = true; |
| 1191 | return OptimizeBlock(MBB); |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | // Okay, there is no really great place to put this block. If, however, |
| 1196 | // the block before this one would be a fall-through if this block were |
| 1197 | // removed, move this block to the end of the function. |
| 1198 | if (FallThrough != MBB->getParent()->end() && |
| 1199 | PrevBB.isSuccessor(FallThrough)) { |
| 1200 | MBB->moveAfter(--MBB->getParent()->end()); |
| 1201 | MadeChange = true; |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 1202 | return MadeChange; |
Chris Lattner | 6b0e3f8 | 2006-10-29 21:05:41 +0000 | [diff] [blame] | 1203 | } |
Chris Lattner | 7d09784 | 2006-10-24 01:12:32 +0000 | [diff] [blame] | 1204 | } |
Chris Lattner | 21ab22e | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 1205 | } |
Evan Cheng | 030a0a0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 1206 | |
| 1207 | return MadeChange; |
Chris Lattner | 21ab22e | 2004-07-31 10:01:27 +0000 | [diff] [blame] | 1208 | } |