blob: a8fee1c9ac360f6ca51720c0db37a86c9c8472ad [file] [log] [blame]
Chris Lattner21ab22e2004-07-31 10:01:27 +00001//===-- BranchFolding.cpp - Fold machine code branch instructions ---------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner21ab22e2004-07-31 10:01:27 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner21ab22e2004-07-31 10:01:27 +00008//===----------------------------------------------------------------------===//
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 Lattnerf10a56a2006-11-18 21:56:39 +000019#define DEBUG_TYPE "branchfolding"
Chris Lattner21ab22e2004-07-31 10:01:27 +000020#include "llvm/CodeGen/Passes.h"
Jim Laskey44c3b9f2007-01-26 21:22:28 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner21ab22e2004-07-31 10:01:27 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerc50ffcb2006-10-17 17:13:52 +000023#include "llvm/CodeGen/MachineJumpTableInfo.h"
Dale Johannesen69cb9b72007-03-20 21:35:06 +000024#include "llvm/CodeGen/RegisterScavenging.h"
Chris Lattner21ab22e2004-07-31 10:01:27 +000025#include "llvm/Target/TargetInstrInfo.h"
26#include "llvm/Target/TargetMachine.h"
Dan Gohman6f0d0242008-02-10 18:45:23 +000027#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner12143052006-10-21 00:47:49 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnerf10a56a2006-11-18 21:56:39 +000029#include "llvm/Support/Debug.h"
Evan Cheng80b09fe2008-04-10 02:32:10 +000030#include "llvm/ADT/SmallSet.h"
Chris Lattner12143052006-10-21 00:47:49 +000031#include "llvm/ADT/Statistic.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/ADT/STLExtras.h"
Jeff Cohend41b30d2006-11-05 19:31:28 +000033#include <algorithm>
Chris Lattner21ab22e2004-07-31 10:01:27 +000034using namespace llvm;
35
Chris Lattnercd3245a2006-12-19 22:41:21 +000036STATISTIC(NumDeadBlocks, "Number of dead blocks removed");
37STATISTIC(NumBranchOpts, "Number of branches optimized");
38STATISTIC(NumTailMerge , "Number of block tails merged");
Dale Johannesen81da02b2007-05-22 17:14:46 +000039static cl::opt<cl::boolOrDefault> FlagEnableTailMerge("enable-tail-merge",
40 cl::init(cl::BOU_UNSET), cl::Hidden);
Chris Lattner21ab22e2004-07-31 10:01:27 +000041namespace {
Dale Johannesen1a90a5a2007-06-08 01:08:52 +000042 // Throttle for huge numbers of predecessors (compile speed problems)
Dan Gohman4a3f6c82008-05-06 01:53:16 +000043 static cl::opt<unsigned>
Dale Johannesen1a90a5a2007-06-08 01:08:52 +000044 TailMergeThreshold("tail-merge-threshold",
45 cl::desc("Max number of predecessors to consider tail merging"),
46 cl::init(100), cl::Hidden);
47
Evan Chengfb8075d2008-02-28 00:43:03 +000048 struct VISIBILITY_HIDDEN BranchFolder : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000049 static char ID;
Dan Gohman61e729e2007-08-02 21:21:54 +000050 explicit BranchFolder(bool defaultEnableTailMerge) :
Dale Johannesen81da02b2007-05-22 17:14:46 +000051 MachineFunctionPass((intptr_t)&ID) {
52 switch (FlagEnableTailMerge) {
53 case cl::BOU_UNSET: EnableTailMerge = defaultEnableTailMerge; break;
54 case cl::BOU_TRUE: EnableTailMerge = true; break;
55 case cl::BOU_FALSE: EnableTailMerge = false; break;
56 }
57 }
Devang Patel794fd752007-05-01 21:15:47 +000058
Chris Lattner21ab22e2004-07-31 10:01:27 +000059 virtual bool runOnMachineFunction(MachineFunction &MF);
Chris Lattner7821a8a2006-10-14 00:21:48 +000060 virtual const char *getPassName() const { return "Control Flow Optimizer"; }
61 const TargetInstrInfo *TII;
Jim Laskey44c3b9f2007-01-26 21:22:28 +000062 MachineModuleInfo *MMI;
Chris Lattner7821a8a2006-10-14 00:21:48 +000063 bool MadeChange;
Chris Lattner21ab22e2004-07-31 10:01:27 +000064 private:
Chris Lattner12143052006-10-21 00:47:49 +000065 // Tail Merging.
Dale Johannesen81da02b2007-05-22 17:14:46 +000066 bool EnableTailMerge;
Chris Lattner12143052006-10-21 00:47:49 +000067 bool TailMergeBlocks(MachineFunction &MF);
Dale Johannesen7d33b4c2007-05-07 20:57:21 +000068 bool TryMergeBlocks(MachineBasicBlock* SuccBB,
69 MachineBasicBlock* PredBB);
Chris Lattner12143052006-10-21 00:47:49 +000070 void ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
71 MachineBasicBlock *NewDest);
Chris Lattner1d08d832006-11-01 01:16:12 +000072 MachineBasicBlock *SplitMBBAt(MachineBasicBlock &CurMBB,
73 MachineBasicBlock::iterator BBI1);
Dale Johannesen6b8583c2008-05-09 23:28:24 +000074 unsigned ComputeSameTails(unsigned CurHash, unsigned minCommonTailLength);
75 void RemoveBlocksWithHash(unsigned CurHash, MachineBasicBlock* SuccBB,
76 MachineBasicBlock* PredBB);
Dale Johannesen51b2b9e2008-05-12 20:33:57 +000077 unsigned CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
78 unsigned maxCommonTailLength);
Dale Johannesen69cb9b72007-03-20 21:35:06 +000079
Dale Johannesen6ae83fa2008-05-09 21:24:35 +000080 typedef std::pair<unsigned,MachineBasicBlock*> MergePotentialsElt;
Dale Johannesen6ae83fa2008-05-09 21:24:35 +000081 typedef std::vector<MergePotentialsElt>::iterator MPIterator;
Dale Johannesen6b8583c2008-05-09 23:28:24 +000082 std::vector<MergePotentialsElt> MergePotentials;
Dale Johannesen51b2b9e2008-05-12 20:33:57 +000083
Dale Johannesen6b8583c2008-05-09 23:28:24 +000084 typedef std::pair<MPIterator, MachineBasicBlock::iterator> SameTailElt;
85 std::vector<SameTailElt> SameTails;
Dale Johannesen6ae83fa2008-05-09 21:24:35 +000086
Dan Gohman6f0d0242008-02-10 18:45:23 +000087 const TargetRegisterInfo *RegInfo;
Dale Johannesen69cb9b72007-03-20 21:35:06 +000088 RegScavenger *RS;
Chris Lattner12143052006-10-21 00:47:49 +000089 // Branch optzn.
90 bool OptimizeBranches(MachineFunction &MF);
Chris Lattner7d097842006-10-24 01:12:32 +000091 void OptimizeBlock(MachineBasicBlock *MBB);
Chris Lattner683747a2006-10-17 23:17:27 +000092 void RemoveDeadBlock(MachineBasicBlock *MBB);
Evan Cheng80b09fe2008-04-10 02:32:10 +000093 bool OptimizeImpDefsBlock(MachineBasicBlock *MBB);
Chris Lattner6b0e3f82006-10-29 21:05:41 +000094
95 bool CanFallThrough(MachineBasicBlock *CurBB);
96 bool CanFallThrough(MachineBasicBlock *CurBB, bool BranchUnAnalyzable,
97 MachineBasicBlock *TBB, MachineBasicBlock *FBB,
98 const std::vector<MachineOperand> &Cond);
Chris Lattner21ab22e2004-07-31 10:01:27 +000099 };
Devang Patel19974732007-05-03 01:11:54 +0000100 char BranchFolder::ID = 0;
Chris Lattner21ab22e2004-07-31 10:01:27 +0000101}
102
Dale Johannesen81da02b2007-05-22 17:14:46 +0000103FunctionPass *llvm::createBranchFoldingPass(bool DefaultEnableTailMerge) {
104 return new BranchFolder(DefaultEnableTailMerge); }
Chris Lattner21ab22e2004-07-31 10:01:27 +0000105
Chris Lattnerc50ffcb2006-10-17 17:13:52 +0000106/// RemoveDeadBlock - Remove the specified dead machine basic block from the
107/// function, updating the CFG.
Chris Lattner683747a2006-10-17 23:17:27 +0000108void BranchFolder::RemoveDeadBlock(MachineBasicBlock *MBB) {
Jim Laskey033c9712007-02-22 16:39:03 +0000109 assert(MBB->pred_empty() && "MBB must be dead!");
Jim Laskey02b3f5e2007-02-21 22:42:20 +0000110 DOUT << "\nRemoving MBB: " << *MBB;
Chris Lattner683747a2006-10-17 23:17:27 +0000111
Chris Lattnerc50ffcb2006-10-17 17:13:52 +0000112 MachineFunction *MF = MBB->getParent();
113 // drop all successors.
114 while (!MBB->succ_empty())
115 MBB->removeSuccessor(MBB->succ_end()-1);
Chris Lattner683747a2006-10-17 23:17:27 +0000116
Jim Laskey1ee29252007-01-26 14:34:52 +0000117 // If there is DWARF info to active, check to see if there are any LABEL
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000118 // records in the basic block. If so, unregister them from MachineModuleInfo.
119 if (MMI && !MBB->empty()) {
Chris Lattner683747a2006-10-17 23:17:27 +0000120 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
121 I != E; ++I) {
Jim Laskey1ee29252007-01-26 14:34:52 +0000122 if ((unsigned)I->getOpcode() == TargetInstrInfo::LABEL) {
Chris Lattner683747a2006-10-17 23:17:27 +0000123 // The label ID # is always operand #0, an immediate.
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000124 MMI->InvalidateLabel(I->getOperand(0).getImm());
Chris Lattner683747a2006-10-17 23:17:27 +0000125 }
126 }
127 }
128
Chris Lattnerc50ffcb2006-10-17 17:13:52 +0000129 // Remove the block.
130 MF->getBasicBlockList().erase(MBB);
131}
132
Evan Cheng80b09fe2008-04-10 02:32:10 +0000133/// OptimizeImpDefsBlock - If a basic block is just a bunch of implicit_def
134/// followed by terminators, and if the implicitly defined registers are not
135/// used by the terminators, remove those implicit_def's. e.g.
136/// BB1:
137/// r0 = implicit_def
138/// r1 = implicit_def
139/// br
140/// This block can be optimized away later if the implicit instructions are
141/// removed.
142bool BranchFolder::OptimizeImpDefsBlock(MachineBasicBlock *MBB) {
143 SmallSet<unsigned, 4> ImpDefRegs;
144 MachineBasicBlock::iterator I = MBB->begin();
145 while (I != MBB->end()) {
146 if (I->getOpcode() != TargetInstrInfo::IMPLICIT_DEF)
147 break;
148 unsigned Reg = I->getOperand(0).getReg();
149 ImpDefRegs.insert(Reg);
150 for (const unsigned *SubRegs = RegInfo->getSubRegisters(Reg);
151 unsigned SubReg = *SubRegs; ++SubRegs)
152 ImpDefRegs.insert(SubReg);
153 ++I;
154 }
155 if (ImpDefRegs.empty())
156 return false;
157
158 MachineBasicBlock::iterator FirstTerm = I;
159 while (I != MBB->end()) {
160 if (!TII->isUnpredicatedTerminator(I))
161 return false;
162 // See if it uses any of the implicitly defined registers.
163 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
164 MachineOperand &MO = I->getOperand(i);
165 if (!MO.isReg() || !MO.isUse())
166 continue;
167 unsigned Reg = MO.getReg();
168 if (ImpDefRegs.count(Reg))
169 return false;
170 }
171 ++I;
172 }
173
174 I = MBB->begin();
175 while (I != FirstTerm) {
176 MachineInstr *ImpDefMI = &*I;
177 ++I;
178 MBB->erase(ImpDefMI);
179 }
180
181 return true;
182}
183
Chris Lattner21ab22e2004-07-31 10:01:27 +0000184bool BranchFolder::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner7821a8a2006-10-14 00:21:48 +0000185 TII = MF.getTarget().getInstrInfo();
186 if (!TII) return false;
187
Evan Cheng80b09fe2008-04-10 02:32:10 +0000188 RegInfo = MF.getTarget().getRegisterInfo();
189
Dale Johannesen14ba0cc2007-05-15 21:19:17 +0000190 // Fix CFG. The later algorithms expect it to be right.
191 bool EverMadeChange = false;
192 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; I++) {
193 MachineBasicBlock *MBB = I, *TBB = 0, *FBB = 0;
194 std::vector<MachineOperand> Cond;
195 if (!TII->AnalyzeBranch(*MBB, TBB, FBB, Cond))
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000196 EverMadeChange |= MBB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty());
Evan Cheng80b09fe2008-04-10 02:32:10 +0000197 EverMadeChange |= OptimizeImpDefsBlock(MBB);
Dale Johannesen14ba0cc2007-05-15 21:19:17 +0000198 }
199
Dale Johannesen69cb9b72007-03-20 21:35:06 +0000200 RS = RegInfo->requiresRegisterScavenging(MF) ? new RegScavenger() : NULL;
201
Jim Laskey44c3b9f2007-01-26 21:22:28 +0000202 MMI = getAnalysisToUpdate<MachineModuleInfo>();
Dale Johannesen76b38fc2007-05-10 01:01:49 +0000203
Chris Lattner12143052006-10-21 00:47:49 +0000204 bool MadeChangeThisIteration = true;
205 while (MadeChangeThisIteration) {
206 MadeChangeThisIteration = false;
207 MadeChangeThisIteration |= TailMergeBlocks(MF);
208 MadeChangeThisIteration |= OptimizeBranches(MF);
209 EverMadeChange |= MadeChangeThisIteration;
Chris Lattner21ab22e2004-07-31 10:01:27 +0000210 }
211
Chris Lattner6acfe122006-10-28 18:34:47 +0000212 // See if any jump tables have become mergable or dead as the code generator
213 // did its thing.
214 MachineJumpTableInfo *JTI = MF.getJumpTableInfo();
215 const std::vector<MachineJumpTableEntry> &JTs = JTI->getJumpTables();
216 if (!JTs.empty()) {
217 // Figure out how these jump tables should be merged.
218 std::vector<unsigned> JTMapping;
219 JTMapping.reserve(JTs.size());
220
221 // We always keep the 0th jump table.
222 JTMapping.push_back(0);
223
224 // Scan the jump tables, seeing if there are any duplicates. Note that this
225 // is N^2, which should be fixed someday.
226 for (unsigned i = 1, e = JTs.size(); i != e; ++i)
227 JTMapping.push_back(JTI->getJumpTableIndex(JTs[i].MBBs));
228
229 // If a jump table was merge with another one, walk the function rewriting
230 // references to jump tables to reference the new JT ID's. Keep track of
231 // whether we see a jump table idx, if not, we can delete the JT.
Dale Johannesen6b8583c2008-05-09 23:28:24 +0000232 BitVector JTIsLive(JTs.size());
Chris Lattner6acfe122006-10-28 18:34:47 +0000233 for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
234 BB != E; ++BB) {
235 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
236 I != E; ++I)
237 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
238 MachineOperand &Op = I->getOperand(op);
239 if (!Op.isJumpTableIndex()) continue;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000240 unsigned NewIdx = JTMapping[Op.getIndex()];
241 Op.setIndex(NewIdx);
Chris Lattner6acfe122006-10-28 18:34:47 +0000242
243 // Remember that this JT is live.
Dale Johannesen6b8583c2008-05-09 23:28:24 +0000244 JTIsLive.set(NewIdx);
Chris Lattner6acfe122006-10-28 18:34:47 +0000245 }
246 }
247
248 // Finally, remove dead jump tables. This happens either because the
249 // indirect jump was unreachable (and thus deleted) or because the jump
250 // table was merged with some other one.
251 for (unsigned i = 0, e = JTIsLive.size(); i != e; ++i)
Dale Johannesen6b8583c2008-05-09 23:28:24 +0000252 if (!JTIsLive.test(i)) {
Chris Lattner6acfe122006-10-28 18:34:47 +0000253 JTI->RemoveJumpTable(i);
254 EverMadeChange = true;
255 }
256 }
257
Dale Johannesen69cb9b72007-03-20 21:35:06 +0000258 delete RS;
Chris Lattner21ab22e2004-07-31 10:01:27 +0000259 return EverMadeChange;
260}
261
Chris Lattner12143052006-10-21 00:47:49 +0000262//===----------------------------------------------------------------------===//
263// Tail Merging of Blocks
264//===----------------------------------------------------------------------===//
265
266/// HashMachineInstr - Compute a hash value for MI and its operands.
267static unsigned HashMachineInstr(const MachineInstr *MI) {
268 unsigned Hash = MI->getOpcode();
269 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
270 const MachineOperand &Op = MI->getOperand(i);
271
272 // Merge in bits from the operand if easy.
273 unsigned OperandHash = 0;
274 switch (Op.getType()) {
275 case MachineOperand::MO_Register: OperandHash = Op.getReg(); break;
276 case MachineOperand::MO_Immediate: OperandHash = Op.getImm(); break;
277 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000278 OperandHash = Op.getMBB()->getNumber();
Chris Lattner12143052006-10-21 00:47:49 +0000279 break;
Chris Lattner8aa797a2007-12-30 23:10:15 +0000280 case MachineOperand::MO_FrameIndex:
Chris Lattner12143052006-10-21 00:47:49 +0000281 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner12143052006-10-21 00:47:49 +0000282 case MachineOperand::MO_JumpTableIndex:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000283 OperandHash = Op.getIndex();
Chris Lattner12143052006-10-21 00:47:49 +0000284 break;
285 case MachineOperand::MO_GlobalAddress:
286 case MachineOperand::MO_ExternalSymbol:
287 // Global address / external symbol are too hard, don't bother, but do
288 // pull in the offset.
289 OperandHash = Op.getOffset();
290 break;
291 default: break;
292 }
293
294 Hash += ((OperandHash << 3) | Op.getType()) << (i&31);
295 }
296 return Hash;
297}
298
Dale Johannesen7aea8322007-05-23 21:07:20 +0000299/// HashEndOfMBB - Hash the last few instructions in the MBB. For blocks
300/// with no successors, we hash two instructions, because cross-jumping
301/// only saves code when at least two instructions are removed (since a
302/// branch must be inserted). For blocks with a successor, one of the
303/// two blocks to be tail-merged will end with a branch already, so
304/// it gains to cross-jump even for one instruction.
305
306static unsigned HashEndOfMBB(const MachineBasicBlock *MBB,
307 unsigned minCommonTailLength) {
Chris Lattner12143052006-10-21 00:47:49 +0000308 MachineBasicBlock::const_iterator I = MBB->end();
309 if (I == MBB->begin())
310 return 0; // Empty MBB.
311
312 --I;
313 unsigned Hash = HashMachineInstr(I);
314
Dale Johannesen7aea8322007-05-23 21:07:20 +0000315 if (I == MBB->begin() || minCommonTailLength == 1)
Chris Lattner12143052006-10-21 00:47:49 +0000316 return Hash; // Single instr MBB.
317
318 --I;
319 // Hash in the second-to-last instruction.
320 Hash ^= HashMachineInstr(I) << 2;
321 return Hash;
322}
323
324/// ComputeCommonTailLength - Given two machine basic blocks, compute the number
325/// of instructions they actually have in common together at their end. Return
326/// iterators for the first shared instruction in each block.
327static unsigned ComputeCommonTailLength(MachineBasicBlock *MBB1,
328 MachineBasicBlock *MBB2,
329 MachineBasicBlock::iterator &I1,
330 MachineBasicBlock::iterator &I2) {
331 I1 = MBB1->end();
332 I2 = MBB2->end();
333
334 unsigned TailLen = 0;
335 while (I1 != MBB1->begin() && I2 != MBB2->begin()) {
336 --I1; --I2;
Bill Wendling80629c82007-10-19 21:09:55 +0000337 if (!I1->isIdenticalTo(I2) ||
Bill Wendlingda6efc52007-10-25 19:49:32 +0000338 // FIXME: This check is dubious. It's used to get around a problem where
Bill Wendling0713a222007-10-25 18:23:45 +0000339 // people incorrectly expect inline asm directives to remain in the same
340 // relative order. This is untenable because normal compiler
341 // optimizations (like this one) may reorder and/or merge these
342 // directives.
Bill Wendling80629c82007-10-19 21:09:55 +0000343 I1->getOpcode() == TargetInstrInfo::INLINEASM) {
Chris Lattner12143052006-10-21 00:47:49 +0000344 ++I1; ++I2;
345 break;
346 }
347 ++TailLen;
348 }
349 return TailLen;
350}
351
352/// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything
Chris Lattner386e2902006-10-21 05:08:28 +0000353/// after it, replacing it with an unconditional branch to NewDest. This
354/// returns true if OldInst's block is modified, false if NewDest is modified.
Chris Lattner12143052006-10-21 00:47:49 +0000355void BranchFolder::ReplaceTailWithBranchTo(MachineBasicBlock::iterator OldInst,
356 MachineBasicBlock *NewDest) {
357 MachineBasicBlock *OldBB = OldInst->getParent();
358
359 // Remove all the old successors of OldBB from the CFG.
360 while (!OldBB->succ_empty())
361 OldBB->removeSuccessor(OldBB->succ_begin());
362
363 // Remove all the dead instructions from the end of OldBB.
364 OldBB->erase(OldInst, OldBB->end());
365
Chris Lattner386e2902006-10-21 05:08:28 +0000366 // If OldBB isn't immediately before OldBB, insert a branch to it.
367 if (++MachineFunction::iterator(OldBB) != MachineFunction::iterator(NewDest))
368 TII->InsertBranch(*OldBB, NewDest, 0, std::vector<MachineOperand>());
Chris Lattner12143052006-10-21 00:47:49 +0000369 OldBB->addSuccessor(NewDest);
370 ++NumTailMerge;
371}
372
Chris Lattner1d08d832006-11-01 01:16:12 +0000373/// SplitMBBAt - Given a machine basic block and an iterator into it, split the
374/// MBB so that the part before the iterator falls into the part starting at the
375/// iterator. This returns the new MBB.
376MachineBasicBlock *BranchFolder::SplitMBBAt(MachineBasicBlock &CurMBB,
377 MachineBasicBlock::iterator BBI1) {
378 // Create the fall-through block.
379 MachineFunction::iterator MBBI = &CurMBB;
380 MachineBasicBlock *NewMBB = new MachineBasicBlock(CurMBB.getBasicBlock());
381 CurMBB.getParent()->getBasicBlockList().insert(++MBBI, NewMBB);
382
383 // Move all the successors of this block to the specified block.
384 while (!CurMBB.succ_empty()) {
385 MachineBasicBlock *S = *(CurMBB.succ_end()-1);
386 NewMBB->addSuccessor(S);
387 CurMBB.removeSuccessor(S);
388 }
389
390 // Add an edge from CurMBB to NewMBB for the fall-through.
391 CurMBB.addSuccessor(NewMBB);
392
393 // Splice the code over.
394 NewMBB->splice(NewMBB->end(), &CurMBB, BBI1, CurMBB.end());
Dale Johannesen69cb9b72007-03-20 21:35:06 +0000395
396 // For targets that use the register scavenger, we must maintain LiveIns.
397 if (RS) {
398 RS->enterBasicBlock(&CurMBB);
399 if (!CurMBB.empty())
400 RS->forward(prior(CurMBB.end()));
401 BitVector RegsLiveAtExit(RegInfo->getNumRegs());
402 RS->getRegsUsed(RegsLiveAtExit, false);
403 for (unsigned int i=0, e=RegInfo->getNumRegs(); i!=e; i++)
404 if (RegsLiveAtExit[i])
405 NewMBB->addLiveIn(i);
406 }
407
Chris Lattner1d08d832006-11-01 01:16:12 +0000408 return NewMBB;
409}
410
Chris Lattnerd4bf3c22006-11-01 19:36:29 +0000411/// EstimateRuntime - Make a rough estimate for how long it will take to run
412/// the specified code.
413static unsigned EstimateRuntime(MachineBasicBlock::iterator I,
Chris Lattner69244302008-01-07 01:56:04 +0000414 MachineBasicBlock::iterator E) {
Chris Lattnerd4bf3c22006-11-01 19:36:29 +0000415 unsigned Time = 0;
416 for (; I != E; ++I) {
Chris Lattner749c6f62008-01-07 07:27:27 +0000417 const TargetInstrDesc &TID = I->getDesc();
418 if (TID.isCall())
Chris Lattnerd4bf3c22006-11-01 19:36:29 +0000419 Time += 10;
Chris Lattner749c6f62008-01-07 07:27:27 +0000420 else if (TID.isSimpleLoad() || TID.mayStore())
Chris Lattnerd4bf3c22006-11-01 19:36:29 +0000421 Time += 2;
422 else
423 ++Time;
424 }
425 return Time;
426}
427
Dale Johannesen76b38fc2007-05-10 01:01:49 +0000428// CurMBB needs to add an unconditional branch to SuccMBB (we removed these
429// branches temporarily for tail merging). In the case where CurMBB ends
430// with a conditional branch to the next block, optimize by reversing the
431// test and conditionally branching to SuccMBB instead.
432
433static void FixTail(MachineBasicBlock* CurMBB, MachineBasicBlock *SuccBB,
434 const TargetInstrInfo *TII) {
435 MachineFunction *MF = CurMBB->getParent();
436 MachineFunction::iterator I = next(MachineFunction::iterator(CurMBB));
437 MachineBasicBlock *TBB = 0, *FBB = 0;
438 std::vector<MachineOperand> Cond;
439 if (I != MF->end() &&
440 !TII->AnalyzeBranch(*CurMBB, TBB, FBB, Cond)) {
441 MachineBasicBlock *NextBB = I;
Dale Johannesen6b8583c2008-05-09 23:28:24 +0000442 if (TBB == NextBB && !Cond.empty() && !FBB) {
Dale Johannesen76b38fc2007-05-10 01:01:49 +0000443 if (!TII->ReverseBranchCondition(Cond)) {
444 TII->RemoveBranch(*CurMBB);
445 TII->InsertBranch(*CurMBB, SuccBB, NULL, Cond);
446 return;
447 }
448 }
449 }
450 TII->InsertBranch(*CurMBB, SuccBB, NULL, std::vector<MachineOperand>());
451}
452
Dale Johannesen44008c52007-05-30 00:32:01 +0000453static bool MergeCompare(const std::pair<unsigned,MachineBasicBlock*> &p,
454 const std::pair<unsigned,MachineBasicBlock*> &q) {
Dale Johannesen95ef4062007-05-29 23:47:50 +0000455 if (p.first < q.first)
456 return true;
457 else if (p.first > q.first)
458 return false;
459 else if (p.second->getNumber() < q.second->getNumber())
460 return true;
461 else if (p.second->getNumber() > q.second->getNumber())
462 return false;
David Greene67fcdf72007-07-10 22:00:30 +0000463 else {
Duncan Sands97b4ac82007-07-11 08:47:55 +0000464 // _GLIBCXX_DEBUG checks strict weak ordering, which involves comparing
465 // an object with itself.
466#ifndef _GLIBCXX_DEBUG
Dale Johannesen95ef4062007-05-29 23:47:50 +0000467 assert(0 && "Predecessor appears twice");
David Greene67fcdf72007-07-10 22:00:30 +0000468#endif
Duncan Sands97b4ac82007-07-11 08:47:55 +0000469 return(false);
David Greene67fcdf72007-07-10 22:00:30 +0000470 }
Dale Johannesen95ef4062007-05-29 23:47:50 +0000471}
472
Dale Johannesen6b8583c2008-05-09 23:28:24 +0000473/// ComputeSameTails - Look through all the blocks in MergePotentials that have
474/// hash CurHash (guaranteed to match the last element). Build the vector
475/// SameTails of all those that have the (same) largest number of instructions
476/// in common of any pair of these blocks. SameTails entries contain an
477/// iterator into MergePotentials (from which the MachineBasicBlock can be
478/// found) and a MachineBasicBlock::iterator into that MBB indicating the
479/// instruction where the matching code sequence begins.
480/// Order of elements in SameTails is the reverse of the order in which
481/// those blocks appear in MergePotentials (where they are not necessarily
482/// consecutive).
483unsigned BranchFolder::ComputeSameTails(unsigned CurHash,
484 unsigned minCommonTailLength) {
485 unsigned maxCommonTailLength = 0U;
486 SameTails.clear();
487 MachineBasicBlock::iterator TrialBBI1, TrialBBI2;
488 MPIterator HighestMPIter = prior(MergePotentials.end());
489 for (MPIterator CurMPIter = prior(MergePotentials.end()),
490 B = MergePotentials.begin();
491 CurMPIter!=B && CurMPIter->first==CurHash;
492 --CurMPIter) {
493 for (MPIterator I = prior(CurMPIter); I->first==CurHash ; --I) {
494 unsigned CommonTailLen = ComputeCommonTailLength(
495 CurMPIter->second,
496 I->second,
497 TrialBBI1, TrialBBI2);
498 if (CommonTailLen >= minCommonTailLength) {
499 if (CommonTailLen > maxCommonTailLength) {
500 SameTails.clear();
501 maxCommonTailLength = CommonTailLen;
502 HighestMPIter = CurMPIter;
503 SameTails.push_back(std::make_pair(CurMPIter, TrialBBI1));
504 }
505 if (HighestMPIter == CurMPIter &&
506 CommonTailLen == maxCommonTailLength)
507 SameTails.push_back(std::make_pair(I, TrialBBI2));
508 }
509 if (I==B)
510 break;
511 }
512 }
513 return maxCommonTailLength;
514}
515
516/// RemoveBlocksWithHash - Remove all blocks with hash CurHash from
517/// MergePotentials, restoring branches at ends of blocks as appropriate.
518void BranchFolder::RemoveBlocksWithHash(unsigned CurHash,
519 MachineBasicBlock* SuccBB,
520 MachineBasicBlock* PredBB) {
521 for (MPIterator CurMPIter = prior(MergePotentials.end()),
522 B = MergePotentials.begin();
523 CurMPIter->first==CurHash;
524 --CurMPIter) {
525 // Put the unconditional branch back, if we need one.
526 MachineBasicBlock *CurMBB = CurMPIter->second;
527 if (SuccBB && CurMBB != PredBB)
528 FixTail(CurMBB, SuccBB, TII);
529 MergePotentials.erase(CurMPIter);
530 if (CurMPIter==B)
531 break;
532 }
533}
534
Dale Johannesen51b2b9e2008-05-12 20:33:57 +0000535/// CreateCommonTailOnlyBlock - None of the blocks to be tail-merged consist
536/// only of the common tail. Create a block that does by splitting one.
537unsigned BranchFolder::CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
538 unsigned maxCommonTailLength) {
539 unsigned i, commonTailIndex;
540 unsigned TimeEstimate = ~0U;
541 for (i=0, commonTailIndex=0; i<SameTails.size(); i++) {
542 // Use PredBB if possible; that doesn't require a new branch.
543 if (SameTails[i].first->second==PredBB) {
544 commonTailIndex = i;
545 break;
546 }
547 // Otherwise, make a (fairly bogus) choice based on estimate of
548 // how long it will take the various blocks to execute.
549 unsigned t = EstimateRuntime(SameTails[i].first->second->begin(),
550 SameTails[i].second);
551 if (t<=TimeEstimate) {
552 TimeEstimate = t;
553 commonTailIndex = i;
554 }
555 }
556
557 MachineBasicBlock::iterator BBI = SameTails[commonTailIndex].second;
558 MachineBasicBlock *MBB = SameTails[commonTailIndex].first->second;
559
560 DOUT << "\nSplitting " << MBB->getNumber() << ", size " <<
561 maxCommonTailLength;
562
563 MachineBasicBlock *newMBB = SplitMBBAt(*MBB, BBI);
564 SameTails[commonTailIndex].first->second = newMBB;
565 SameTails[commonTailIndex].second = newMBB->begin();
566 // If we split PredBB, newMBB is the new predecessor.
567 if (PredBB==MBB)
568 PredBB = newMBB;
569
570 return commonTailIndex;
571}
572
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000573// See if any of the blocks in MergePotentials (which all have a common single
574// successor, or all have no successor) can be tail-merged. If there is a
575// successor, any blocks in MergePotentials that are not tail-merged and
576// are not immediately before Succ must have an unconditional branch to
577// Succ added (but the predecessor/successor lists need no adjustment).
578// The lone predecessor of Succ that falls through into Succ,
579// if any, is given in PredBB.
580
581bool BranchFolder::TryMergeBlocks(MachineBasicBlock *SuccBB,
582 MachineBasicBlock* PredBB) {
Evan Cheng31886db2008-02-19 02:09:37 +0000583 // It doesn't make sense to save a single instruction since tail merging
584 // will add a jump.
585 // FIXME: Ask the target to provide the threshold?
586 unsigned minCommonTailLength = (SuccBB ? 1 : 2) + 1;
Chris Lattner12143052006-10-21 00:47:49 +0000587 MadeChange = false;
588
Dale Johannesen6ae83fa2008-05-09 21:24:35 +0000589 DOUT << "\nTryMergeBlocks " << MergePotentials.size();
Dale Johannesen6b8583c2008-05-09 23:28:24 +0000590
Chris Lattner12143052006-10-21 00:47:49 +0000591 // Sort by hash value so that blocks with identical end sequences sort
592 // together.
Dale Johannesen6b8583c2008-05-09 23:28:24 +0000593 std::stable_sort(MergePotentials.begin(), MergePotentials.end(),MergeCompare);
Chris Lattner12143052006-10-21 00:47:49 +0000594
595 // Walk through equivalence sets looking for actual exact matches.
596 while (MergePotentials.size() > 1) {
Dale Johannesen6ae83fa2008-05-09 21:24:35 +0000597 unsigned CurHash = prior(MergePotentials.end())->first;
Chris Lattner12143052006-10-21 00:47:49 +0000598
Dale Johannesen6b8583c2008-05-09 23:28:24 +0000599 // Build SameTails, identifying the set of blocks with this hash code
600 // and with the maximum number of instructions in common.
601 unsigned maxCommonTailLength = ComputeSameTails(CurHash,
602 minCommonTailLength);
Dale Johannesen7aea8322007-05-23 21:07:20 +0000603
Dale Johannesena5a21172007-06-01 23:02:45 +0000604 // If we didn't find any pair that has at least minCommonTailLength
Dale Johannesen6ae83fa2008-05-09 21:24:35 +0000605 // instructions in common, remove all blocks with this hash code and retry.
606 if (SameTails.empty()) {
Dale Johannesen6b8583c2008-05-09 23:28:24 +0000607 RemoveBlocksWithHash(CurHash, SuccBB, PredBB);
Dale Johannesen7aea8322007-05-23 21:07:20 +0000608 continue;
Chris Lattner12143052006-10-21 00:47:49 +0000609 }
Chris Lattner1d08d832006-11-01 01:16:12 +0000610
Dale Johannesen6ae83fa2008-05-09 21:24:35 +0000611 // If one of the blocks is the entire common tail (and not the entry
Dale Johannesen51b2b9e2008-05-12 20:33:57 +0000612 // block, which we can't jump to), we can treat all blocks with this same
613 // tail at once. Use PredBB if that is one of the possibilities, as that
614 // will not introduce any extra branches.
615 MachineBasicBlock *EntryBB = MergePotentials.begin()->second->
616 getParent()->begin();
617 unsigned int commonTailIndex, i;
618 for (commonTailIndex=SameTails.size(), i=0; i<SameTails.size(); i++) {
Dale Johannesen6ae83fa2008-05-09 21:24:35 +0000619 MachineBasicBlock *MBB = SameTails[i].first->second;
Dale Johannesen51b2b9e2008-05-12 20:33:57 +0000620 if (MBB->begin() == SameTails[i].second && MBB != EntryBB) {
621 commonTailIndex = i;
622 if (MBB==PredBB)
623 break;
Dale Johannesen6ae83fa2008-05-09 21:24:35 +0000624 }
Dale Johannesen6ae83fa2008-05-09 21:24:35 +0000625 }
Dale Johannesena5a21172007-06-01 23:02:45 +0000626
Dale Johannesen51b2b9e2008-05-12 20:33:57 +0000627 if (commonTailIndex==SameTails.size()) {
628 // None of the blocks consist entirely of the common tail.
629 // Split a block so that one does.
630 commonTailIndex = CreateCommonTailOnlyBlock(PredBB, maxCommonTailLength);
Chris Lattner1d08d832006-11-01 01:16:12 +0000631 }
Dale Johannesen51b2b9e2008-05-12 20:33:57 +0000632
633 MachineBasicBlock *MBB = SameTails[commonTailIndex].first->second;
634 // MBB is common tail. Adjust all other BB's to jump to this one.
635 // Traversal must be forwards so erases work.
636 DOUT << "\nUsing common tail " << MBB->getNumber() << " for ";
637 for (unsigned int i=0; i<SameTails.size(); ++i) {
638 if (commonTailIndex==i)
639 continue;
640 DOUT << SameTails[i].first->second->getNumber() << ",";
641 // Hack the end off BB i, making it jump to BB commonTailIndex instead.
642 ReplaceTailWithBranchTo(SameTails[i].second, MBB);
643 // BB i is no longer a predecessor of SuccBB; remove it from the worklist.
644 MergePotentials.erase(SameTails[i].first);
Chris Lattner12143052006-10-21 00:47:49 +0000645 }
Dale Johannesen51b2b9e2008-05-12 20:33:57 +0000646 DOUT << "\n";
647 // We leave commonTailIndex in the worklist in case there are other blocks
648 // that match it with a smaller number of instructions.
Chris Lattner1d08d832006-11-01 01:16:12 +0000649 MadeChange = true;
Chris Lattner12143052006-10-21 00:47:49 +0000650 }
Chris Lattner12143052006-10-21 00:47:49 +0000651 return MadeChange;
652}
653
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000654bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
Dale Johannesen76b38fc2007-05-10 01:01:49 +0000655
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000656 if (!EnableTailMerge) return false;
Dale Johannesen76b38fc2007-05-10 01:01:49 +0000657
658 MadeChange = false;
659
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000660 // First find blocks with no successors.
Dale Johannesen76b38fc2007-05-10 01:01:49 +0000661 MergePotentials.clear();
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000662 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
663 if (I->succ_empty())
Dale Johannesen7aea8322007-05-23 21:07:20 +0000664 MergePotentials.push_back(std::make_pair(HashEndOfMBB(I, 2U), I));
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000665 }
Dale Johannesen76b38fc2007-05-10 01:01:49 +0000666 // See if we can do any tail merging on those.
Dale Johannesen6ae83fa2008-05-09 21:24:35 +0000667 if (MergePotentials.size() < TailMergeThreshold &&
668 MergePotentials.size() >= 2)
Dale Johannesen53af4c02007-06-08 00:34:27 +0000669 MadeChange |= TryMergeBlocks(NULL, NULL);
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000670
Dale Johannesen76b38fc2007-05-10 01:01:49 +0000671 // Look at blocks (IBB) with multiple predecessors (PBB).
672 // We change each predecessor to a canonical form, by
673 // (1) temporarily removing any unconditional branch from the predecessor
674 // to IBB, and
675 // (2) alter conditional branches so they branch to the other block
676 // not IBB; this may require adding back an unconditional branch to IBB
677 // later, where there wasn't one coming in. E.g.
678 // Bcc IBB
679 // fallthrough to QBB
680 // here becomes
681 // Bncc QBB
682 // with a conceptual B to IBB after that, which never actually exists.
683 // With those changes, we see whether the predecessors' tails match,
684 // and merge them if so. We change things out of canonical form and
685 // back to the way they were later in the process. (OptimizeBranches
686 // would undo some of this, but we can't use it, because we'd get into
687 // a compile-time infinite loop repeatedly doing and undoing the same
688 // transformations.)
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000689
690 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
Dale Johannesen1a90a5a2007-06-08 01:08:52 +0000691 if (!I->succ_empty() && I->pred_size() >= 2 &&
692 I->pred_size() < TailMergeThreshold) {
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000693 MachineBasicBlock *IBB = I;
694 MachineBasicBlock *PredBB = prior(I);
Dale Johannesen76b38fc2007-05-10 01:01:49 +0000695 MergePotentials.clear();
Dale Johannesen1a90a5a2007-06-08 01:08:52 +0000696 for (MachineBasicBlock::pred_iterator P = I->pred_begin(),
697 E2 = I->pred_end();
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000698 P != E2; ++P) {
699 MachineBasicBlock* PBB = *P;
Dale Johannesen76b38fc2007-05-10 01:01:49 +0000700 // Skip blocks that loop to themselves, can't tail merge these.
701 if (PBB==IBB)
702 continue;
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000703 MachineBasicBlock *TBB = 0, *FBB = 0;
704 std::vector<MachineOperand> Cond;
Dale Johannesen76b38fc2007-05-10 01:01:49 +0000705 if (!TII->AnalyzeBranch(*PBB, TBB, FBB, Cond)) {
706 // Failing case: IBB is the target of a cbr, and
707 // we cannot reverse the branch.
708 std::vector<MachineOperand> NewCond(Cond);
Dale Johannesen6b8583c2008-05-09 23:28:24 +0000709 if (!Cond.empty() && TBB==IBB) {
Dale Johannesen76b38fc2007-05-10 01:01:49 +0000710 if (TII->ReverseBranchCondition(NewCond))
711 continue;
712 // This is the QBB case described above
713 if (!FBB)
714 FBB = next(MachineFunction::iterator(PBB));
715 }
Dale Johannesenfe7e3972007-06-04 23:52:54 +0000716 // Failing case: the only way IBB can be reached from PBB is via
717 // exception handling. Happens for landing pads. Would be nice
718 // to have a bit in the edge so we didn't have to do all this.
719 if (IBB->isLandingPad()) {
720 MachineFunction::iterator IP = PBB; IP++;
721 MachineBasicBlock* PredNextBB = NULL;
722 if (IP!=MF.end())
723 PredNextBB = IP;
724 if (TBB==NULL) {
725 if (IBB!=PredNextBB) // fallthrough
726 continue;
727 } else if (FBB) {
728 if (TBB!=IBB && FBB!=IBB) // cbr then ubr
729 continue;
Dan Gohman30359592008-01-29 13:02:09 +0000730 } else if (Cond.empty()) {
Dale Johannesenfe7e3972007-06-04 23:52:54 +0000731 if (TBB!=IBB) // ubr
732 continue;
733 } else {
734 if (TBB!=IBB && IBB!=PredNextBB) // cbr
735 continue;
736 }
737 }
Dale Johannesen76b38fc2007-05-10 01:01:49 +0000738 // Remove the unconditional branch at the end, if any.
Dale Johannesen6b8583c2008-05-09 23:28:24 +0000739 if (TBB && (Cond.empty() || FBB)) {
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000740 TII->RemoveBranch(*PBB);
Dale Johannesen6b8583c2008-05-09 23:28:24 +0000741 if (!Cond.empty())
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000742 // reinsert conditional branch only, for now
Dale Johannesen76b38fc2007-05-10 01:01:49 +0000743 TII->InsertBranch(*PBB, (TBB==IBB) ? FBB : TBB, 0, NewCond);
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000744 }
Dale Johannesen7aea8322007-05-23 21:07:20 +0000745 MergePotentials.push_back(std::make_pair(HashEndOfMBB(PBB, 1U), *P));
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000746 }
747 }
748 if (MergePotentials.size() >= 2)
749 MadeChange |= TryMergeBlocks(I, PredBB);
750 // Reinsert an unconditional branch if needed.
Dale Johannesen51b2b9e2008-05-12 20:33:57 +0000751 // The 1 below can occur as a result of removing blocks in TryMergeBlocks.
Dale Johannesen1cf08c12007-05-18 01:28:58 +0000752 PredBB = prior(I); // this may have been changed in TryMergeBlocks
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000753 if (MergePotentials.size()==1 &&
Dale Johannesen51b2b9e2008-05-12 20:33:57 +0000754 MergePotentials.begin()->second != PredBB)
755 FixTail(MergePotentials.begin()->second, I, TII);
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000756 }
757 }
Dale Johannesen7d33b4c2007-05-07 20:57:21 +0000758 return MadeChange;
759}
Chris Lattner12143052006-10-21 00:47:49 +0000760
761//===----------------------------------------------------------------------===//
762// Branch Optimization
763//===----------------------------------------------------------------------===//
764
765bool BranchFolder::OptimizeBranches(MachineFunction &MF) {
766 MadeChange = false;
767
Dale Johannesen6b896ce2007-02-17 00:44:34 +0000768 // Make sure blocks are numbered in order
769 MF.RenumberBlocks();
770
Chris Lattner12143052006-10-21 00:47:49 +0000771 for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) {
772 MachineBasicBlock *MBB = I++;
773 OptimizeBlock(MBB);
774
775 // If it is dead, remove it.
Jim Laskey033c9712007-02-22 16:39:03 +0000776 if (MBB->pred_empty()) {
Chris Lattner12143052006-10-21 00:47:49 +0000777 RemoveDeadBlock(MBB);
778 MadeChange = true;
779 ++NumDeadBlocks;
780 }
781 }
782 return MadeChange;
783}
784
785
Chris Lattner6b0e3f82006-10-29 21:05:41 +0000786/// CanFallThrough - Return true if the specified block (with the specified
787/// branch condition) can implicitly transfer control to the block after it by
788/// falling off the end of it. This should return false if it can reach the
789/// block after it, but it uses an explicit branch to do so (e.g. a table jump).
790///
791/// True is a conservative answer.
792///
793bool BranchFolder::CanFallThrough(MachineBasicBlock *CurBB,
794 bool BranchUnAnalyzable,
Dale Johannesen51b2b9e2008-05-12 20:33:57 +0000795 MachineBasicBlock *TBB,
796 MachineBasicBlock *FBB,
Chris Lattner6b0e3f82006-10-29 21:05:41 +0000797 const std::vector<MachineOperand> &Cond) {
798 MachineFunction::iterator Fallthrough = CurBB;
799 ++Fallthrough;
800 // If FallthroughBlock is off the end of the function, it can't fall through.
801 if (Fallthrough == CurBB->getParent()->end())
802 return false;
803
804 // If FallthroughBlock isn't a successor of CurBB, no fallthrough is possible.
805 if (!CurBB->isSuccessor(Fallthrough))
806 return false;
807
808 // If we couldn't analyze the branch, assume it could fall through.
809 if (BranchUnAnalyzable) return true;
810
Chris Lattner7d097842006-10-24 01:12:32 +0000811 // If there is no branch, control always falls through.
812 if (TBB == 0) return true;
813
814 // If there is some explicit branch to the fallthrough block, it can obviously
815 // reach, even though the branch should get folded to fall through implicitly.
Chris Lattner6b0e3f82006-10-29 21:05:41 +0000816 if (MachineFunction::iterator(TBB) == Fallthrough ||
817 MachineFunction::iterator(FBB) == Fallthrough)
Chris Lattner7d097842006-10-24 01:12:32 +0000818 return true;
819
820 // If it's an unconditional branch to some block not the fall through, it
821 // doesn't fall through.
822 if (Cond.empty()) return false;
823
824 // Otherwise, if it is conditional and has no explicit false block, it falls
825 // through.
Chris Lattnerc2e91e32006-10-25 22:21:37 +0000826 return FBB == 0;
Chris Lattner7d097842006-10-24 01:12:32 +0000827}
828
Chris Lattner6b0e3f82006-10-29 21:05:41 +0000829/// CanFallThrough - Return true if the specified can implicitly transfer
830/// control to the block after it by falling off the end of it. This should
831/// return false if it can reach the block after it, but it uses an explicit
832/// branch to do so (e.g. a table jump).
833///
834/// True is a conservative answer.
835///
836bool BranchFolder::CanFallThrough(MachineBasicBlock *CurBB) {
837 MachineBasicBlock *TBB = 0, *FBB = 0;
838 std::vector<MachineOperand> Cond;
839 bool CurUnAnalyzable = TII->AnalyzeBranch(*CurBB, TBB, FBB, Cond);
840 return CanFallThrough(CurBB, CurUnAnalyzable, TBB, FBB, Cond);
841}
842
Chris Lattnera7bef4a2006-11-18 20:47:54 +0000843/// IsBetterFallthrough - Return true if it would be clearly better to
844/// fall-through to MBB1 than to fall through into MBB2. This has to return
845/// a strict ordering, returning true for both (MBB1,MBB2) and (MBB2,MBB1) will
846/// result in infinite loops.
847static bool IsBetterFallthrough(MachineBasicBlock *MBB1,
Chris Lattner69244302008-01-07 01:56:04 +0000848 MachineBasicBlock *MBB2) {
Chris Lattner154e1042006-11-18 21:30:35 +0000849 // Right now, we use a simple heuristic. If MBB2 ends with a call, and
850 // MBB1 doesn't, we prefer to fall through into MBB1. This allows us to
Chris Lattnera7bef4a2006-11-18 20:47:54 +0000851 // optimize branches that branch to either a return block or an assert block
852 // into a fallthrough to the return.
853 if (MBB1->empty() || MBB2->empty()) return false;
Christopher Lamb11a4f642007-12-10 07:24:06 +0000854
855 // If there is a clear successor ordering we make sure that one block
856 // will fall through to the next
857 if (MBB1->isSuccessor(MBB2)) return true;
858 if (MBB2->isSuccessor(MBB1)) return false;
Chris Lattnera7bef4a2006-11-18 20:47:54 +0000859
860 MachineInstr *MBB1I = --MBB1->end();
861 MachineInstr *MBB2I = --MBB2->end();
Chris Lattner749c6f62008-01-07 07:27:27 +0000862 return MBB2I->getDesc().isCall() && !MBB1I->getDesc().isCall();
Chris Lattnera7bef4a2006-11-18 20:47:54 +0000863}
864
Chris Lattner7821a8a2006-10-14 00:21:48 +0000865/// OptimizeBlock - Analyze and optimize control flow related to the specified
866/// block. This is never called on the entry block.
Chris Lattner7d097842006-10-24 01:12:32 +0000867void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) {
868 MachineFunction::iterator FallThrough = MBB;
869 ++FallThrough;
870
Chris Lattnereb15eee2006-10-13 20:43:10 +0000871 // If this block is empty, make everyone use its fall-through, not the block
Dale Johannesena52dd152007-05-31 21:54:00 +0000872 // explicitly. Landing pads should not do this since the landing-pad table
873 // points to this block.
874 if (MBB->empty() && !MBB->isLandingPad()) {
Chris Lattner386e2902006-10-21 05:08:28 +0000875 // Dead block? Leave for cleanup later.
Jim Laskey033c9712007-02-22 16:39:03 +0000876 if (MBB->pred_empty()) return;
Chris Lattner7821a8a2006-10-14 00:21:48 +0000877
Chris Lattnerc50ffcb2006-10-17 17:13:52 +0000878 if (FallThrough == MBB->getParent()->end()) {
879 // TODO: Simplify preds to not branch here if possible!
880 } else {
881 // Rewrite all predecessors of the old block to go to the fallthrough
882 // instead.
Jim Laskey033c9712007-02-22 16:39:03 +0000883 while (!MBB->pred_empty()) {
Chris Lattner7821a8a2006-10-14 00:21:48 +0000884 MachineBasicBlock *Pred = *(MBB->pred_end()-1);
Evan Cheng0370fad2007-06-04 06:44:01 +0000885 Pred->ReplaceUsesOfBlockWith(MBB, FallThrough);
Chris Lattner7821a8a2006-10-14 00:21:48 +0000886 }
Chris Lattnerc50ffcb2006-10-17 17:13:52 +0000887
888 // If MBB was the target of a jump table, update jump tables to go to the
889 // fallthrough instead.
Chris Lattner6acfe122006-10-28 18:34:47 +0000890 MBB->getParent()->getJumpTableInfo()->
891 ReplaceMBBInJumpTables(MBB, FallThrough);
Chris Lattner7821a8a2006-10-14 00:21:48 +0000892 MadeChange = true;
Chris Lattner21ab22e2004-07-31 10:01:27 +0000893 }
Chris Lattner7821a8a2006-10-14 00:21:48 +0000894 return;
Chris Lattner21ab22e2004-07-31 10:01:27 +0000895 }
896
Chris Lattner7821a8a2006-10-14 00:21:48 +0000897 // Check to see if we can simplify the terminator of the block before this
898 // one.
Chris Lattner7d097842006-10-24 01:12:32 +0000899 MachineBasicBlock &PrevBB = *prior(MachineFunction::iterator(MBB));
Chris Lattnerffddf6b2006-10-17 18:16:40 +0000900
Chris Lattner7821a8a2006-10-14 00:21:48 +0000901 MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0;
902 std::vector<MachineOperand> PriorCond;
Chris Lattner6b0e3f82006-10-29 21:05:41 +0000903 bool PriorUnAnalyzable =
904 TII->AnalyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond);
Chris Lattner386e2902006-10-21 05:08:28 +0000905 if (!PriorUnAnalyzable) {
906 // If the CFG for the prior block has extra edges, remove them.
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000907 MadeChange |= PrevBB.CorrectExtraCFGEdges(PriorTBB, PriorFBB,
908 !PriorCond.empty());
Chris Lattner386e2902006-10-21 05:08:28 +0000909
Chris Lattner7821a8a2006-10-14 00:21:48 +0000910 // If the previous branch is conditional and both conditions go to the same
Chris Lattner2d47bd92006-10-21 05:43:30 +0000911 // destination, remove the branch, replacing it with an unconditional one or
912 // a fall-through.
Chris Lattner7821a8a2006-10-14 00:21:48 +0000913 if (PriorTBB && PriorTBB == PriorFBB) {
Chris Lattner386e2902006-10-21 05:08:28 +0000914 TII->RemoveBranch(PrevBB);
Chris Lattner7821a8a2006-10-14 00:21:48 +0000915 PriorCond.clear();
Chris Lattner7d097842006-10-24 01:12:32 +0000916 if (PriorTBB != MBB)
Chris Lattner386e2902006-10-21 05:08:28 +0000917 TII->InsertBranch(PrevBB, PriorTBB, 0, PriorCond);
Chris Lattner7821a8a2006-10-14 00:21:48 +0000918 MadeChange = true;
Chris Lattner12143052006-10-21 00:47:49 +0000919 ++NumBranchOpts;
Chris Lattner7821a8a2006-10-14 00:21:48 +0000920 return OptimizeBlock(MBB);
921 }
922
923 // If the previous branch *only* branches to *this* block (conditional or
924 // not) remove the branch.
Chris Lattner7d097842006-10-24 01:12:32 +0000925 if (PriorTBB == MBB && PriorFBB == 0) {
Chris Lattner386e2902006-10-21 05:08:28 +0000926 TII->RemoveBranch(PrevBB);
Chris Lattner7821a8a2006-10-14 00:21:48 +0000927 MadeChange = true;
Chris Lattner12143052006-10-21 00:47:49 +0000928 ++NumBranchOpts;
Chris Lattner7821a8a2006-10-14 00:21:48 +0000929 return OptimizeBlock(MBB);
930 }
Chris Lattner2d47bd92006-10-21 05:43:30 +0000931
932 // If the prior block branches somewhere else on the condition and here if
933 // the condition is false, remove the uncond second branch.
Chris Lattner7d097842006-10-24 01:12:32 +0000934 if (PriorFBB == MBB) {
Chris Lattner2d47bd92006-10-21 05:43:30 +0000935 TII->RemoveBranch(PrevBB);
936 TII->InsertBranch(PrevBB, PriorTBB, 0, PriorCond);
937 MadeChange = true;
938 ++NumBranchOpts;
939 return OptimizeBlock(MBB);
940 }
Chris Lattnera2d79952006-10-21 05:54:00 +0000941
942 // If the prior block branches here on true and somewhere else on false, and
943 // if the branch condition is reversible, reverse the branch to create a
944 // fall-through.
Chris Lattner7d097842006-10-24 01:12:32 +0000945 if (PriorTBB == MBB) {
Chris Lattnera2d79952006-10-21 05:54:00 +0000946 std::vector<MachineOperand> NewPriorCond(PriorCond);
947 if (!TII->ReverseBranchCondition(NewPriorCond)) {
948 TII->RemoveBranch(PrevBB);
949 TII->InsertBranch(PrevBB, PriorFBB, 0, NewPriorCond);
950 MadeChange = true;
951 ++NumBranchOpts;
952 return OptimizeBlock(MBB);
953 }
954 }
Chris Lattnera7bef4a2006-11-18 20:47:54 +0000955
Chris Lattner154e1042006-11-18 21:30:35 +0000956 // If this block doesn't fall through (e.g. it ends with an uncond branch or
957 // has no successors) and if the pred falls through into this block, and if
958 // it would otherwise fall through into the block after this, move this
959 // block to the end of the function.
960 //
Chris Lattnera7bef4a2006-11-18 20:47:54 +0000961 // We consider it more likely that execution will stay in the function (e.g.
962 // due to loops) than it is to exit it. This asserts in loops etc, moving
963 // the assert condition out of the loop body.
Chris Lattner154e1042006-11-18 21:30:35 +0000964 if (!PriorCond.empty() && PriorFBB == 0 &&
965 MachineFunction::iterator(PriorTBB) == FallThrough &&
966 !CanFallThrough(MBB)) {
Chris Lattnerf10a56a2006-11-18 21:56:39 +0000967 bool DoTransform = true;
968
Chris Lattnera7bef4a2006-11-18 20:47:54 +0000969 // We have to be careful that the succs of PredBB aren't both no-successor
970 // blocks. If neither have successors and if PredBB is the second from
971 // last block in the function, we'd just keep swapping the two blocks for
972 // last. Only do the swap if one is clearly better to fall through than
973 // the other.
Chris Lattnerf10a56a2006-11-18 21:56:39 +0000974 if (FallThrough == --MBB->getParent()->end() &&
Chris Lattner69244302008-01-07 01:56:04 +0000975 !IsBetterFallthrough(PriorTBB, MBB))
Chris Lattnerf10a56a2006-11-18 21:56:39 +0000976 DoTransform = false;
977
978 // We don't want to do this transformation if we have control flow like:
979 // br cond BB2
980 // BB1:
981 // ..
982 // jmp BBX
983 // BB2:
984 // ..
985 // ret
986 //
987 // In this case, we could actually be moving the return block *into* a
988 // loop!
Chris Lattner4b105912006-11-18 22:25:39 +0000989 if (DoTransform && !MBB->succ_empty() &&
990 (!CanFallThrough(PriorTBB) || PriorTBB->empty()))
Chris Lattnerf10a56a2006-11-18 21:56:39 +0000991 DoTransform = false;
Chris Lattnera7bef4a2006-11-18 20:47:54 +0000992
Chris Lattnerf10a56a2006-11-18 21:56:39 +0000993
994 if (DoTransform) {
Chris Lattnera7bef4a2006-11-18 20:47:54 +0000995 // Reverse the branch so we will fall through on the previous true cond.
996 std::vector<MachineOperand> NewPriorCond(PriorCond);
997 if (!TII->ReverseBranchCondition(NewPriorCond)) {
Chris Lattnerf10a56a2006-11-18 21:56:39 +0000998 DOUT << "\nMoving MBB: " << *MBB;
999 DOUT << "To make fallthrough to: " << *PriorTBB << "\n";
1000
Chris Lattnera7bef4a2006-11-18 20:47:54 +00001001 TII->RemoveBranch(PrevBB);
1002 TII->InsertBranch(PrevBB, MBB, 0, NewPriorCond);
1003
1004 // Move this block to the end of the function.
1005 MBB->moveAfter(--MBB->getParent()->end());
1006 MadeChange = true;
1007 ++NumBranchOpts;
1008 return;
1009 }
1010 }
1011 }
Chris Lattner7821a8a2006-10-14 00:21:48 +00001012 }
Chris Lattner7821a8a2006-10-14 00:21:48 +00001013
Chris Lattner386e2902006-10-21 05:08:28 +00001014 // Analyze the branch in the current block.
1015 MachineBasicBlock *CurTBB = 0, *CurFBB = 0;
1016 std::vector<MachineOperand> CurCond;
Chris Lattner6b0e3f82006-10-29 21:05:41 +00001017 bool CurUnAnalyzable = TII->AnalyzeBranch(*MBB, CurTBB, CurFBB, CurCond);
1018 if (!CurUnAnalyzable) {
Chris Lattner386e2902006-10-21 05:08:28 +00001019 // If the CFG for the prior block has extra edges, remove them.
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001020 MadeChange |= MBB->CorrectExtraCFGEdges(CurTBB, CurFBB, !CurCond.empty());
Chris Lattnereb15eee2006-10-13 20:43:10 +00001021
Chris Lattner5d056952006-11-08 01:03:21 +00001022 // If this is a two-way branch, and the FBB branches to this block, reverse
1023 // the condition so the single-basic-block loop is faster. Instead of:
1024 // Loop: xxx; jcc Out; jmp Loop
1025 // we want:
1026 // Loop: xxx; jncc Loop; jmp Out
1027 if (CurTBB && CurFBB && CurFBB == MBB && CurTBB != MBB) {
1028 std::vector<MachineOperand> NewCond(CurCond);
1029 if (!TII->ReverseBranchCondition(NewCond)) {
1030 TII->RemoveBranch(*MBB);
1031 TII->InsertBranch(*MBB, CurFBB, CurTBB, NewCond);
1032 MadeChange = true;
1033 ++NumBranchOpts;
1034 return OptimizeBlock(MBB);
1035 }
1036 }
1037
1038
Chris Lattner386e2902006-10-21 05:08:28 +00001039 // If this branch is the only thing in its block, see if we can forward
1040 // other blocks across it.
1041 if (CurTBB && CurCond.empty() && CurFBB == 0 &&
Chris Lattner749c6f62008-01-07 07:27:27 +00001042 MBB->begin()->getDesc().isBranch() && CurTBB != MBB) {
Chris Lattner386e2902006-10-21 05:08:28 +00001043 // This block may contain just an unconditional branch. Because there can
1044 // be 'non-branch terminators' in the block, try removing the branch and
1045 // then seeing if the block is empty.
1046 TII->RemoveBranch(*MBB);
1047
1048 // If this block is just an unconditional branch to CurTBB, we can
1049 // usually completely eliminate the block. The only case we cannot
1050 // completely eliminate the block is when the block before this one
1051 // falls through into MBB and we can't understand the prior block's branch
1052 // condition.
Chris Lattnercf420cc2006-10-28 17:32:47 +00001053 if (MBB->empty()) {
1054 bool PredHasNoFallThrough = TII->BlockHasNoFallThrough(PrevBB);
1055 if (PredHasNoFallThrough || !PriorUnAnalyzable ||
1056 !PrevBB.isSuccessor(MBB)) {
1057 // If the prior block falls through into us, turn it into an
1058 // explicit branch to us to make updates simpler.
1059 if (!PredHasNoFallThrough && PrevBB.isSuccessor(MBB) &&
1060 PriorTBB != MBB && PriorFBB != MBB) {
1061 if (PriorTBB == 0) {
Chris Lattner6acfe122006-10-28 18:34:47 +00001062 assert(PriorCond.empty() && PriorFBB == 0 &&
1063 "Bad branch analysis");
Chris Lattnercf420cc2006-10-28 17:32:47 +00001064 PriorTBB = MBB;
1065 } else {
1066 assert(PriorFBB == 0 && "Machine CFG out of date!");
1067 PriorFBB = MBB;
1068 }
1069 TII->RemoveBranch(PrevBB);
1070 TII->InsertBranch(PrevBB, PriorTBB, PriorFBB, PriorCond);
Chris Lattner386e2902006-10-21 05:08:28 +00001071 }
Chris Lattner386e2902006-10-21 05:08:28 +00001072
Chris Lattnercf420cc2006-10-28 17:32:47 +00001073 // Iterate through all the predecessors, revectoring each in-turn.
David Greene8a46d342007-06-29 02:45:24 +00001074 size_t PI = 0;
Chris Lattnercf420cc2006-10-28 17:32:47 +00001075 bool DidChange = false;
1076 bool HasBranchToSelf = false;
David Greene8a46d342007-06-29 02:45:24 +00001077 while(PI != MBB->pred_size()) {
1078 MachineBasicBlock *PMBB = *(MBB->pred_begin() + PI);
1079 if (PMBB == MBB) {
Chris Lattnercf420cc2006-10-28 17:32:47 +00001080 // If this block has an uncond branch to itself, leave it.
1081 ++PI;
1082 HasBranchToSelf = true;
1083 } else {
1084 DidChange = true;
David Greene8a46d342007-06-29 02:45:24 +00001085 PMBB->ReplaceUsesOfBlockWith(MBB, CurTBB);
Chris Lattnercf420cc2006-10-28 17:32:47 +00001086 }
Chris Lattner4bc135e2006-10-21 06:11:43 +00001087 }
Chris Lattner386e2902006-10-21 05:08:28 +00001088
Chris Lattnercf420cc2006-10-28 17:32:47 +00001089 // Change any jumptables to go to the new MBB.
Chris Lattner6acfe122006-10-28 18:34:47 +00001090 MBB->getParent()->getJumpTableInfo()->
1091 ReplaceMBBInJumpTables(MBB, CurTBB);
Chris Lattnercf420cc2006-10-28 17:32:47 +00001092 if (DidChange) {
1093 ++NumBranchOpts;
1094 MadeChange = true;
1095 if (!HasBranchToSelf) return;
1096 }
Chris Lattner4bc135e2006-10-21 06:11:43 +00001097 }
Chris Lattnereb15eee2006-10-13 20:43:10 +00001098 }
Chris Lattnereb15eee2006-10-13 20:43:10 +00001099
Chris Lattner386e2902006-10-21 05:08:28 +00001100 // Add the branch back if the block is more than just an uncond branch.
1101 TII->InsertBranch(*MBB, CurTBB, 0, CurCond);
Chris Lattner21ab22e2004-07-31 10:01:27 +00001102 }
Chris Lattner6b0e3f82006-10-29 21:05:41 +00001103 }
1104
1105 // If the prior block doesn't fall through into this block, and if this
1106 // block doesn't fall through into some other block, see if we can find a
1107 // place to move this block where a fall-through will happen.
1108 if (!CanFallThrough(&PrevBB, PriorUnAnalyzable,
1109 PriorTBB, PriorFBB, PriorCond)) {
1110 // Now we know that there was no fall-through into this block, check to
1111 // see if it has a fall-through into its successor.
Dale Johannesen6b896ce2007-02-17 00:44:34 +00001112 bool CurFallsThru = CanFallThrough(MBB, CurUnAnalyzable, CurTBB, CurFBB,
Chris Lattner77edc4b2007-04-30 23:35:00 +00001113 CurCond);
Dale Johannesen6b896ce2007-02-17 00:44:34 +00001114
Jim Laskey02b3f5e2007-02-21 22:42:20 +00001115 if (!MBB->isLandingPad()) {
1116 // Check all the predecessors of this block. If one of them has no fall
1117 // throughs, move this block right after it.
1118 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
1119 E = MBB->pred_end(); PI != E; ++PI) {
1120 // Analyze the branch at the end of the pred.
1121 MachineBasicBlock *PredBB = *PI;
1122 MachineFunction::iterator PredFallthrough = PredBB; ++PredFallthrough;
1123 if (PredBB != MBB && !CanFallThrough(PredBB)
Dale Johannesen76b38fc2007-05-10 01:01:49 +00001124 && (!CurFallsThru || !CurTBB || !CurFBB)
Jim Laskey02b3f5e2007-02-21 22:42:20 +00001125 && (!CurFallsThru || MBB->getNumber() >= PredBB->getNumber())) {
1126 // If the current block doesn't fall through, just move it.
1127 // If the current block can fall through and does not end with a
1128 // conditional branch, we need to append an unconditional jump to
1129 // the (current) next block. To avoid a possible compile-time
1130 // infinite loop, move blocks only backward in this case.
Dale Johannesen76b38fc2007-05-10 01:01:49 +00001131 // Also, if there are already 2 branches here, we cannot add a third;
1132 // this means we have the case
1133 // Bcc next
1134 // B elsewhere
1135 // next:
Jim Laskey02b3f5e2007-02-21 22:42:20 +00001136 if (CurFallsThru) {
1137 MachineBasicBlock *NextBB = next(MachineFunction::iterator(MBB));
1138 CurCond.clear();
1139 TII->InsertBranch(*MBB, NextBB, 0, CurCond);
1140 }
1141 MBB->moveAfter(PredBB);
1142 MadeChange = true;
1143 return OptimizeBlock(MBB);
Chris Lattner7d097842006-10-24 01:12:32 +00001144 }
1145 }
Dale Johannesen6b896ce2007-02-17 00:44:34 +00001146 }
Chris Lattner6b0e3f82006-10-29 21:05:41 +00001147
Dale Johannesen6b896ce2007-02-17 00:44:34 +00001148 if (!CurFallsThru) {
Chris Lattner6b0e3f82006-10-29 21:05:41 +00001149 // Check all successors to see if we can move this block before it.
1150 for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
1151 E = MBB->succ_end(); SI != E; ++SI) {
1152 // Analyze the branch at the end of the block before the succ.
1153 MachineBasicBlock *SuccBB = *SI;
1154 MachineFunction::iterator SuccPrev = SuccBB; --SuccPrev;
Chris Lattner6b0e3f82006-10-29 21:05:41 +00001155 std::vector<MachineOperand> SuccPrevCond;
Chris Lattner77edc4b2007-04-30 23:35:00 +00001156
1157 // If this block doesn't already fall-through to that successor, and if
1158 // the succ doesn't already have a block that can fall through into it,
1159 // and if the successor isn't an EH destination, we can arrange for the
1160 // fallthrough to happen.
1161 if (SuccBB != MBB && !CanFallThrough(SuccPrev) &&
1162 !SuccBB->isLandingPad()) {
Chris Lattner6b0e3f82006-10-29 21:05:41 +00001163 MBB->moveBefore(SuccBB);
1164 MadeChange = true;
1165 return OptimizeBlock(MBB);
1166 }
1167 }
1168
1169 // Okay, there is no really great place to put this block. If, however,
1170 // the block before this one would be a fall-through if this block were
1171 // removed, move this block to the end of the function.
1172 if (FallThrough != MBB->getParent()->end() &&
1173 PrevBB.isSuccessor(FallThrough)) {
1174 MBB->moveAfter(--MBB->getParent()->end());
1175 MadeChange = true;
1176 return;
1177 }
Chris Lattner7d097842006-10-24 01:12:32 +00001178 }
Chris Lattner21ab22e2004-07-31 10:01:27 +00001179 }
Chris Lattner21ab22e2004-07-31 10:01:27 +00001180}