blob: 671ffd0d8f7dc080910d0abe63c69cb1cf23479e [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//
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 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
19#include "llvm/CodeGen/Passes.h"
Chris Lattner683747a2006-10-17 23:17:27 +000020#include "llvm/CodeGen/MachineDebugInfo.h"
Chris Lattner21ab22e2004-07-31 10:01:27 +000021#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerc50ffcb2006-10-17 17:13:52 +000022#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner21ab22e2004-07-31 10:01:27 +000023#include "llvm/Target/TargetInstrInfo.h"
24#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/ADT/STLExtras.h"
Chris Lattner21ab22e2004-07-31 10:01:27 +000026using namespace llvm;
27
28namespace {
29 struct BranchFolder : public MachineFunctionPass {
30 virtual bool runOnMachineFunction(MachineFunction &MF);
Chris Lattner7821a8a2006-10-14 00:21:48 +000031 virtual const char *getPassName() const { return "Control Flow Optimizer"; }
32 const TargetInstrInfo *TII;
Chris Lattner683747a2006-10-17 23:17:27 +000033 MachineDebugInfo *MDI;
Chris Lattner7821a8a2006-10-14 00:21:48 +000034 bool MadeChange;
Chris Lattner21ab22e2004-07-31 10:01:27 +000035 private:
Chris Lattner7821a8a2006-10-14 00:21:48 +000036 void OptimizeBlock(MachineFunction::iterator MBB);
Chris Lattner683747a2006-10-17 23:17:27 +000037 void RemoveDeadBlock(MachineBasicBlock *MBB);
Chris Lattner21ab22e2004-07-31 10:01:27 +000038 };
39}
40
41FunctionPass *llvm::createBranchFoldingPass() { return new BranchFolder(); }
42
Chris Lattnerc50ffcb2006-10-17 17:13:52 +000043/// RemoveDeadBlock - Remove the specified dead machine basic block from the
44/// function, updating the CFG.
Chris Lattner683747a2006-10-17 23:17:27 +000045void BranchFolder::RemoveDeadBlock(MachineBasicBlock *MBB) {
Chris Lattnerc50ffcb2006-10-17 17:13:52 +000046 assert(MBB->pred_empty() && "MBB must be dead!");
Chris Lattner683747a2006-10-17 23:17:27 +000047
Chris Lattnerc50ffcb2006-10-17 17:13:52 +000048 MachineFunction *MF = MBB->getParent();
49 // drop all successors.
50 while (!MBB->succ_empty())
51 MBB->removeSuccessor(MBB->succ_end()-1);
Chris Lattner683747a2006-10-17 23:17:27 +000052
53 // If there is DWARF info to active, check to see if there are any DWARF_LABEL
54 // records in the basic block. If so, unregister them from MachineDebugInfo.
55 if (MDI && !MBB->empty()) {
56 unsigned DWARF_LABELOpc = TII->getDWARF_LABELOpcode();
57 assert(DWARF_LABELOpc &&
58 "Target supports dwarf but didn't implement getDWARF_LABELOpcode!");
59
60 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
61 I != E; ++I) {
62 if ((unsigned)I->getOpcode() == DWARF_LABELOpc) {
63 // The label ID # is always operand #0, an immediate.
64 MDI->RemoveLabelInfo(I->getOperand(0).getImm());
65 }
66 }
67 }
68
Chris Lattnerc50ffcb2006-10-17 17:13:52 +000069 // Remove the block.
70 MF->getBasicBlockList().erase(MBB);
71}
72
Chris Lattner21ab22e2004-07-31 10:01:27 +000073bool BranchFolder::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner7821a8a2006-10-14 00:21:48 +000074 TII = MF.getTarget().getInstrInfo();
75 if (!TII) return false;
76
Chris Lattner683747a2006-10-17 23:17:27 +000077 MDI = getAnalysisToUpdate<MachineDebugInfo>();
Chris Lattner7821a8a2006-10-14 00:21:48 +000078
Chris Lattner21ab22e2004-07-31 10:01:27 +000079 bool EverMadeChange = false;
Chris Lattner7821a8a2006-10-14 00:21:48 +000080 MadeChange = true;
Chris Lattner21ab22e2004-07-31 10:01:27 +000081 while (MadeChange) {
82 MadeChange = false;
Chris Lattner7821a8a2006-10-14 00:21:48 +000083
Chris Lattnerc50ffcb2006-10-17 17:13:52 +000084 for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) {
85 MachineBasicBlock *MBB = I++;
86 OptimizeBlock(MBB);
87
88 // If it is dead, remove it.
89 if (MBB->pred_empty()) {
90 RemoveDeadBlock(MBB);
91 MadeChange = true;
Chris Lattner21ab22e2004-07-31 10:01:27 +000092 }
Chris Lattnerc50ffcb2006-10-17 17:13:52 +000093 }
Chris Lattner21ab22e2004-07-31 10:01:27 +000094 EverMadeChange |= MadeChange;
95 }
96
97 return EverMadeChange;
98}
99
100/// ReplaceUsesOfBlockWith - Given a machine basic block 'BB' that branched to
101/// 'Old', change the code and CFG so that it branches to 'New' instead.
102static void ReplaceUsesOfBlockWith(MachineBasicBlock *BB,
103 MachineBasicBlock *Old,
104 MachineBasicBlock *New,
Chris Lattner7821a8a2006-10-14 00:21:48 +0000105 const TargetInstrInfo *TII) {
Chris Lattner21ab22e2004-07-31 10:01:27 +0000106 assert(Old != New && "Cannot replace self with self!");
107
108 MachineBasicBlock::iterator I = BB->end();
109 while (I != BB->begin()) {
110 --I;
Chris Lattner7821a8a2006-10-14 00:21:48 +0000111 if (!TII->isTerminatorInstr(I->getOpcode())) break;
Chris Lattner21ab22e2004-07-31 10:01:27 +0000112
113 // Scan the operands of this machine instruction, replacing any uses of Old
114 // with New.
115 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
116 if (I->getOperand(i).isMachineBasicBlock() &&
117 I->getOperand(i).getMachineBasicBlock() == Old)
118 I->getOperand(i).setMachineBasicBlock(New);
119 }
120
Chris Lattnereb15eee2006-10-13 20:43:10 +0000121 // Update the successor information.
Chris Lattner21ab22e2004-07-31 10:01:27 +0000122 std::vector<MachineBasicBlock*> Succs(BB->succ_begin(), BB->succ_end());
123 for (int i = Succs.size()-1; i >= 0; --i)
124 if (Succs[i] == Old) {
125 BB->removeSuccessor(Old);
126 BB->addSuccessor(New);
127 }
128}
129
Chris Lattner7821a8a2006-10-14 00:21:48 +0000130/// OptimizeBlock - Analyze and optimize control flow related to the specified
131/// block. This is never called on the entry block.
132void BranchFolder::OptimizeBlock(MachineFunction::iterator MBB) {
Chris Lattnereb15eee2006-10-13 20:43:10 +0000133 // If this block is empty, make everyone use its fall-through, not the block
Chris Lattner21ab22e2004-07-31 10:01:27 +0000134 // explicitly.
135 if (MBB->empty()) {
Chris Lattner7821a8a2006-10-14 00:21:48 +0000136 if (MBB->pred_empty()) return; // dead block? Leave for cleanup later.
137
138 MachineFunction::iterator FallThrough = next(MBB);
139
Chris Lattnerc50ffcb2006-10-17 17:13:52 +0000140 if (FallThrough == MBB->getParent()->end()) {
141 // TODO: Simplify preds to not branch here if possible!
142 } else {
143 // Rewrite all predecessors of the old block to go to the fallthrough
144 // instead.
Chris Lattner7821a8a2006-10-14 00:21:48 +0000145 while (!MBB->pred_empty()) {
146 MachineBasicBlock *Pred = *(MBB->pred_end()-1);
147 ReplaceUsesOfBlockWith(Pred, MBB, FallThrough, TII);
148 }
Chris Lattnerc50ffcb2006-10-17 17:13:52 +0000149
150 // If MBB was the target of a jump table, update jump tables to go to the
151 // fallthrough instead.
152 MBB->getParent()->getJumpTableInfo()->ReplaceMBBInJumpTables(MBB,
153 FallThrough);
Chris Lattner7821a8a2006-10-14 00:21:48 +0000154 MadeChange = true;
Chris Lattner21ab22e2004-07-31 10:01:27 +0000155 }
Chris Lattner7821a8a2006-10-14 00:21:48 +0000156 return;
Chris Lattner21ab22e2004-07-31 10:01:27 +0000157 }
158
Chris Lattner7821a8a2006-10-14 00:21:48 +0000159 // Check to see if we can simplify the terminator of the block before this
160 // one.
Chris Lattnerffddf6b2006-10-17 18:16:40 +0000161 MachineBasicBlock &PrevBB = *prior(MBB);
162
Chris Lattner7821a8a2006-10-14 00:21:48 +0000163 MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0;
164 std::vector<MachineOperand> PriorCond;
Chris Lattnerffddf6b2006-10-17 18:16:40 +0000165 if (!TII->AnalyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond)) {
Chris Lattner7821a8a2006-10-14 00:21:48 +0000166 // If the previous branch is conditional and both conditions go to the same
167 // destination, remove the branch, replacing it with an unconditional one.
168 if (PriorTBB && PriorTBB == PriorFBB) {
169 TII->RemoveBranch(*prior(MBB));
170 PriorCond.clear();
171 if (PriorTBB != &*MBB)
172 TII->InsertBranch(*prior(MBB), PriorTBB, 0, PriorCond);
173 MadeChange = true;
174 return OptimizeBlock(MBB);
175 }
176
177 // If the previous branch *only* branches to *this* block (conditional or
178 // not) remove the branch.
179 if (PriorTBB == &*MBB && PriorFBB == 0) {
180 TII->RemoveBranch(*prior(MBB));
181 MadeChange = true;
182 return OptimizeBlock(MBB);
183 }
184 }
Chris Lattner7821a8a2006-10-14 00:21:48 +0000185
Chris Lattnereb15eee2006-10-13 20:43:10 +0000186#if 0
187
Chris Lattner21ab22e2004-07-31 10:01:27 +0000188 if (MBB->pred_size() == 1) {
189 // If this block has a single predecessor, and if that block has a single
190 // successor, merge this block into that block.
191 MachineBasicBlock *Pred = *MBB->pred_begin();
192 if (Pred->succ_size() == 1) {
193 // Delete all of the terminators from end of the pred block. NOTE, this
194 // assumes that terminators do not have side effects!
Chris Lattnereb15eee2006-10-13 20:43:10 +0000195 // FIXME: This doesn't work for FP_REG_KILL.
196
197 while (!Pred->empty() && TII.isTerminatorInstr(Pred->back().getOpcode()))
198 Pred->pop_back();
199
200 // Splice the instructions over.
201 Pred->splice(Pred->end(), MBB, MBB->begin(), MBB->end());
202
203 // If MBB does not end with a barrier, add a goto instruction to the end.
204 if (Pred->empty() || !TII.isBarrier(Pred->back().getOpcode()))
205 TII.insertGoto(*Pred, *next(MBB));
206
207 // Update the CFG now.
208 Pred->removeSuccessor(Pred->succ_begin());
209 while (!MBB->succ_empty()) {
210 Pred->addSuccessor(*(MBB->succ_end()-1));
211 MBB->removeSuccessor(MBB->succ_end()-1);
212 }
213 return true;
214 }
215 }
216
217 // If BB falls through into Old, insert an unconditional branch to New.
218 MachineFunction::iterator BBSucc = BB; ++BBSucc;
219 if (BBSucc != BB->getParent()->end() && &*BBSucc == Old)
220 TII.insertGoto(*BB, *New);
221
222
223 if (MBB->pred_size() == 1) {
224 // If this block has a single predecessor, and if that block has a single
225 // successor, merge this block into that block.
226 MachineBasicBlock *Pred = *MBB->pred_begin();
227 if (Pred->succ_size() == 1) {
228 // Delete all of the terminators from end of the pred block. NOTE, this
229 // assumes that terminators do not have side effects!
230 // FIXME: This doesn't work for FP_REG_KILL.
231
Chris Lattner21ab22e2004-07-31 10:01:27 +0000232 while (!Pred->empty() && TII.isTerminatorInstr(Pred->back().getOpcode()))
233 Pred->pop_back();
234
235 // Splice the instructions over.
236 Pred->splice(Pred->end(), MBB, MBB->begin(), MBB->end());
237
238 // If MBB does not end with a barrier, add a goto instruction to the end.
239 if (Pred->empty() || !TII.isBarrier(Pred->back().getOpcode()))
Alkis Evlogimenos9fd33232004-07-31 15:14:29 +0000240 TII.insertGoto(*Pred, *next(MBB));
Chris Lattner21ab22e2004-07-31 10:01:27 +0000241
242 // Update the CFG now.
243 Pred->removeSuccessor(Pred->succ_begin());
244 while (!MBB->succ_empty()) {
245 Pred->addSuccessor(*(MBB->succ_end()-1));
246 MBB->removeSuccessor(MBB->succ_end()-1);
247 }
248 return true;
249 }
250 }
251
252 // If the first instruction in this block is an unconditional branch, and if
253 // there are predecessors, fold the branch into the predecessors.
254 if (!MBB->pred_empty() && isUncondBranch(MBB->begin(), TII)) {
255 MachineInstr *Br = MBB->begin();
256 assert(Br->getNumOperands() == 1 && Br->getOperand(0).isMachineBasicBlock()
257 && "Uncond branch should take one MBB argument!");
258 MachineBasicBlock *Dest = Br->getOperand(0).getMachineBasicBlock();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000259
Chris Lattner21ab22e2004-07-31 10:01:27 +0000260 while (!MBB->pred_empty()) {
261 MachineBasicBlock *Pred = *(MBB->pred_end()-1);
262 ReplaceUsesOfBlockWith(Pred, MBB, Dest, TII);
263 }
264 return true;
265 }
266
267 // If the last instruction is an unconditional branch and the fall through
268 // block is the destination, just delete the branch.
269 if (isUncondBranch(--MBB->end(), TII)) {
270 MachineBasicBlock::iterator MI = --MBB->end();
271 MachineInstr *UncondBr = MI;
Alkis Evlogimenos9fd33232004-07-31 15:14:29 +0000272 MachineFunction::iterator FallThrough = next(MBB);
Chris Lattner21ab22e2004-07-31 10:01:27 +0000273
Alkis Evlogimenosdd045832004-07-31 15:03:52 +0000274 MachineFunction::iterator UncondDest =
275 MI->getOperand(0).getMachineBasicBlock();
276 if (UncondDest == FallThrough) {
Chris Lattner21ab22e2004-07-31 10:01:27 +0000277 // Just delete the branch. This does not effect the CFG.
278 MBB->erase(UncondBr);
279 return true;
280 }
281
282 // Okay, so we don't have a fall-through. Check to see if we have an
283 // conditional branch that would be a fall through if we reversed it. If
284 // so, invert the condition and delete the uncond branch.
285 if (MI != MBB->begin() && isCondBranch(--MI, TII)) {
286 // We assume that conditional branches always have the branch dest as the
287 // last operand. This could be generalized in the future if needed.
288 unsigned LastOpnd = MI->getNumOperands()-1;
Alkis Evlogimenos9fd33232004-07-31 15:14:29 +0000289 if (MachineFunction::iterator(
290 MI->getOperand(LastOpnd).getMachineBasicBlock()) == FallThrough) {
Chris Lattner21ab22e2004-07-31 10:01:27 +0000291 // Change the cond branch to go to the uncond dest, nuke the uncond,
292 // then reverse the condition.
293 MI->getOperand(LastOpnd).setMachineBasicBlock(UncondDest);
294 MBB->erase(UncondBr);
295 TII.reverseBranchCondition(MI);
296 return true;
297 }
298 }
299 }
Chris Lattnereb15eee2006-10-13 20:43:10 +0000300#endif
Chris Lattner21ab22e2004-07-31 10:01:27 +0000301}