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