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