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