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