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