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