blob: 725317378c471481843cafc909150c5ea644ee1f [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"
20#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerc50ffcb2006-10-17 17:13:52 +000021#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner21ab22e2004-07-31 10:01:27 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/ADT/STLExtras.h"
Chris Lattner21ab22e2004-07-31 10:01:27 +000025using namespace llvm;
26
27namespace {
28 struct BranchFolder : public MachineFunctionPass {
29 virtual bool runOnMachineFunction(MachineFunction &MF);
Chris Lattner7821a8a2006-10-14 00:21:48 +000030 virtual const char *getPassName() const { return "Control Flow Optimizer"; }
31 const TargetInstrInfo *TII;
32 bool MadeChange;
Chris Lattner21ab22e2004-07-31 10:01:27 +000033 private:
Chris Lattner7821a8a2006-10-14 00:21:48 +000034 void OptimizeBlock(MachineFunction::iterator MBB);
Chris Lattner21ab22e2004-07-31 10:01:27 +000035 };
36}
37
38FunctionPass *llvm::createBranchFoldingPass() { return new BranchFolder(); }
39
Chris Lattnerc50ffcb2006-10-17 17:13:52 +000040/// RemoveDeadBlock - Remove the specified dead machine basic block from the
41/// function, updating the CFG.
42static void RemoveDeadBlock(MachineBasicBlock *MBB) {
43 assert(MBB->pred_empty() && "MBB must be dead!");
44 MachineFunction *MF = MBB->getParent();
45 // drop all successors.
46 while (!MBB->succ_empty())
47 MBB->removeSuccessor(MBB->succ_end()-1);
48 // Remove the block.
49 MF->getBasicBlockList().erase(MBB);
50}
51
Chris Lattner21ab22e2004-07-31 10:01:27 +000052bool BranchFolder::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner7821a8a2006-10-14 00:21:48 +000053 TII = MF.getTarget().getInstrInfo();
54 if (!TII) return false;
55
56 //MF.dump();
57
Chris Lattner21ab22e2004-07-31 10:01:27 +000058 bool EverMadeChange = false;
Chris Lattner7821a8a2006-10-14 00:21:48 +000059 MadeChange = true;
Chris Lattner21ab22e2004-07-31 10:01:27 +000060 while (MadeChange) {
61 MadeChange = false;
Chris Lattner7821a8a2006-10-14 00:21:48 +000062
Chris Lattnerc50ffcb2006-10-17 17:13:52 +000063 for (MachineFunction::iterator I = ++MF.begin(), E = MF.end(); I != E; ) {
64 MachineBasicBlock *MBB = I++;
65 OptimizeBlock(MBB);
66
67 // If it is dead, remove it.
68 if (MBB->pred_empty()) {
69 RemoveDeadBlock(MBB);
70 MadeChange = true;
Chris Lattner21ab22e2004-07-31 10:01:27 +000071 }
Chris Lattnerc50ffcb2006-10-17 17:13:52 +000072 }
Chris Lattner21ab22e2004-07-31 10:01:27 +000073 EverMadeChange |= MadeChange;
74 }
75
76 return EverMadeChange;
77}
78
79/// ReplaceUsesOfBlockWith - Given a machine basic block 'BB' that branched to
80/// 'Old', change the code and CFG so that it branches to 'New' instead.
81static void ReplaceUsesOfBlockWith(MachineBasicBlock *BB,
82 MachineBasicBlock *Old,
83 MachineBasicBlock *New,
Chris Lattner7821a8a2006-10-14 00:21:48 +000084 const TargetInstrInfo *TII) {
Chris Lattner21ab22e2004-07-31 10:01:27 +000085 assert(Old != New && "Cannot replace self with self!");
86
87 MachineBasicBlock::iterator I = BB->end();
88 while (I != BB->begin()) {
89 --I;
Chris Lattner7821a8a2006-10-14 00:21:48 +000090 if (!TII->isTerminatorInstr(I->getOpcode())) break;
Chris Lattner21ab22e2004-07-31 10:01:27 +000091
92 // Scan the operands of this machine instruction, replacing any uses of Old
93 // with New.
94 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
95 if (I->getOperand(i).isMachineBasicBlock() &&
96 I->getOperand(i).getMachineBasicBlock() == Old)
97 I->getOperand(i).setMachineBasicBlock(New);
98 }
99
Chris Lattnereb15eee2006-10-13 20:43:10 +0000100 // Update the successor information.
Chris Lattner21ab22e2004-07-31 10:01:27 +0000101 std::vector<MachineBasicBlock*> Succs(BB->succ_begin(), BB->succ_end());
102 for (int i = Succs.size()-1; i >= 0; --i)
103 if (Succs[i] == Old) {
104 BB->removeSuccessor(Old);
105 BB->addSuccessor(New);
106 }
107}
108
Chris Lattner7821a8a2006-10-14 00:21:48 +0000109/// OptimizeBlock - Analyze and optimize control flow related to the specified
110/// block. This is never called on the entry block.
111void BranchFolder::OptimizeBlock(MachineFunction::iterator MBB) {
Chris Lattnereb15eee2006-10-13 20:43:10 +0000112 // If this block is empty, make everyone use its fall-through, not the block
Chris Lattner21ab22e2004-07-31 10:01:27 +0000113 // explicitly.
114 if (MBB->empty()) {
Chris Lattner7821a8a2006-10-14 00:21:48 +0000115 if (MBB->pred_empty()) return; // dead block? Leave for cleanup later.
116
117 MachineFunction::iterator FallThrough = next(MBB);
118
Chris Lattnerc50ffcb2006-10-17 17:13:52 +0000119 if (FallThrough == MBB->getParent()->end()) {
120 // TODO: Simplify preds to not branch here if possible!
121 } else {
122 // Rewrite all predecessors of the old block to go to the fallthrough
123 // instead.
Chris Lattner7821a8a2006-10-14 00:21:48 +0000124 while (!MBB->pred_empty()) {
125 MachineBasicBlock *Pred = *(MBB->pred_end()-1);
126 ReplaceUsesOfBlockWith(Pred, MBB, FallThrough, TII);
127 }
Chris Lattnerc50ffcb2006-10-17 17:13:52 +0000128
129 // If MBB was the target of a jump table, update jump tables to go to the
130 // fallthrough instead.
131 MBB->getParent()->getJumpTableInfo()->ReplaceMBBInJumpTables(MBB,
132 FallThrough);
Chris Lattner7821a8a2006-10-14 00:21:48 +0000133 MadeChange = true;
Chris Lattner21ab22e2004-07-31 10:01:27 +0000134 }
Chris Lattner7821a8a2006-10-14 00:21:48 +0000135 return;
Chris Lattner21ab22e2004-07-31 10:01:27 +0000136 }
137
Chris Lattner7821a8a2006-10-14 00:21:48 +0000138 // Check to see if we can simplify the terminator of the block before this
139 // one.
Chris Lattnerffddf6b2006-10-17 18:16:40 +0000140 MachineBasicBlock &PrevBB = *prior(MBB);
141
Chris Lattner7821a8a2006-10-14 00:21:48 +0000142 MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0;
143 std::vector<MachineOperand> PriorCond;
Chris Lattnerffddf6b2006-10-17 18:16:40 +0000144 if (!TII->AnalyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond)) {
Chris Lattner7821a8a2006-10-14 00:21:48 +0000145 // If the previous branch is conditional and both conditions go to the same
146 // destination, remove the branch, replacing it with an unconditional one.
147 if (PriorTBB && PriorTBB == PriorFBB) {
148 TII->RemoveBranch(*prior(MBB));
149 PriorCond.clear();
150 if (PriorTBB != &*MBB)
151 TII->InsertBranch(*prior(MBB), PriorTBB, 0, PriorCond);
152 MadeChange = true;
153 return OptimizeBlock(MBB);
154 }
155
156 // If the previous branch *only* branches to *this* block (conditional or
157 // not) remove the branch.
158 if (PriorTBB == &*MBB && PriorFBB == 0) {
159 TII->RemoveBranch(*prior(MBB));
160 MadeChange = true;
161 return OptimizeBlock(MBB);
162 }
163 }
Chris Lattner7821a8a2006-10-14 00:21:48 +0000164
Chris Lattnereb15eee2006-10-13 20:43:10 +0000165#if 0
166
Chris Lattner21ab22e2004-07-31 10:01:27 +0000167 if (MBB->pred_size() == 1) {
168 // If this block has a single predecessor, and if that block has a single
169 // successor, merge this block into that block.
170 MachineBasicBlock *Pred = *MBB->pred_begin();
171 if (Pred->succ_size() == 1) {
172 // Delete all of the terminators from end of the pred block. NOTE, this
173 // assumes that terminators do not have side effects!
Chris Lattnereb15eee2006-10-13 20:43:10 +0000174 // FIXME: This doesn't work for FP_REG_KILL.
175
176 while (!Pred->empty() && TII.isTerminatorInstr(Pred->back().getOpcode()))
177 Pred->pop_back();
178
179 // Splice the instructions over.
180 Pred->splice(Pred->end(), MBB, MBB->begin(), MBB->end());
181
182 // If MBB does not end with a barrier, add a goto instruction to the end.
183 if (Pred->empty() || !TII.isBarrier(Pred->back().getOpcode()))
184 TII.insertGoto(*Pred, *next(MBB));
185
186 // Update the CFG now.
187 Pred->removeSuccessor(Pred->succ_begin());
188 while (!MBB->succ_empty()) {
189 Pred->addSuccessor(*(MBB->succ_end()-1));
190 MBB->removeSuccessor(MBB->succ_end()-1);
191 }
192 return true;
193 }
194 }
195
196 // If BB falls through into Old, insert an unconditional branch to New.
197 MachineFunction::iterator BBSucc = BB; ++BBSucc;
198 if (BBSucc != BB->getParent()->end() && &*BBSucc == Old)
199 TII.insertGoto(*BB, *New);
200
201
202 if (MBB->pred_size() == 1) {
203 // If this block has a single predecessor, and if that block has a single
204 // successor, merge this block into that block.
205 MachineBasicBlock *Pred = *MBB->pred_begin();
206 if (Pred->succ_size() == 1) {
207 // Delete all of the terminators from end of the pred block. NOTE, this
208 // assumes that terminators do not have side effects!
209 // FIXME: This doesn't work for FP_REG_KILL.
210
Chris Lattner21ab22e2004-07-31 10:01:27 +0000211 while (!Pred->empty() && TII.isTerminatorInstr(Pred->back().getOpcode()))
212 Pred->pop_back();
213
214 // Splice the instructions over.
215 Pred->splice(Pred->end(), MBB, MBB->begin(), MBB->end());
216
217 // If MBB does not end with a barrier, add a goto instruction to the end.
218 if (Pred->empty() || !TII.isBarrier(Pred->back().getOpcode()))
Alkis Evlogimenos9fd33232004-07-31 15:14:29 +0000219 TII.insertGoto(*Pred, *next(MBB));
Chris Lattner21ab22e2004-07-31 10:01:27 +0000220
221 // Update the CFG now.
222 Pred->removeSuccessor(Pred->succ_begin());
223 while (!MBB->succ_empty()) {
224 Pred->addSuccessor(*(MBB->succ_end()-1));
225 MBB->removeSuccessor(MBB->succ_end()-1);
226 }
227 return true;
228 }
229 }
230
231 // If the first instruction in this block is an unconditional branch, and if
232 // there are predecessors, fold the branch into the predecessors.
233 if (!MBB->pred_empty() && isUncondBranch(MBB->begin(), TII)) {
234 MachineInstr *Br = MBB->begin();
235 assert(Br->getNumOperands() == 1 && Br->getOperand(0).isMachineBasicBlock()
236 && "Uncond branch should take one MBB argument!");
237 MachineBasicBlock *Dest = Br->getOperand(0).getMachineBasicBlock();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000238
Chris Lattner21ab22e2004-07-31 10:01:27 +0000239 while (!MBB->pred_empty()) {
240 MachineBasicBlock *Pred = *(MBB->pred_end()-1);
241 ReplaceUsesOfBlockWith(Pred, MBB, Dest, TII);
242 }
243 return true;
244 }
245
246 // If the last instruction is an unconditional branch and the fall through
247 // block is the destination, just delete the branch.
248 if (isUncondBranch(--MBB->end(), TII)) {
249 MachineBasicBlock::iterator MI = --MBB->end();
250 MachineInstr *UncondBr = MI;
Alkis Evlogimenos9fd33232004-07-31 15:14:29 +0000251 MachineFunction::iterator FallThrough = next(MBB);
Chris Lattner21ab22e2004-07-31 10:01:27 +0000252
Alkis Evlogimenosdd045832004-07-31 15:03:52 +0000253 MachineFunction::iterator UncondDest =
254 MI->getOperand(0).getMachineBasicBlock();
255 if (UncondDest == FallThrough) {
Chris Lattner21ab22e2004-07-31 10:01:27 +0000256 // Just delete the branch. This does not effect the CFG.
257 MBB->erase(UncondBr);
258 return true;
259 }
260
261 // Okay, so we don't have a fall-through. Check to see if we have an
262 // conditional branch that would be a fall through if we reversed it. If
263 // so, invert the condition and delete the uncond branch.
264 if (MI != MBB->begin() && isCondBranch(--MI, TII)) {
265 // We assume that conditional branches always have the branch dest as the
266 // last operand. This could be generalized in the future if needed.
267 unsigned LastOpnd = MI->getNumOperands()-1;
Alkis Evlogimenos9fd33232004-07-31 15:14:29 +0000268 if (MachineFunction::iterator(
269 MI->getOperand(LastOpnd).getMachineBasicBlock()) == FallThrough) {
Chris Lattner21ab22e2004-07-31 10:01:27 +0000270 // Change the cond branch to go to the uncond dest, nuke the uncond,
271 // then reverse the condition.
272 MI->getOperand(LastOpnd).setMachineBasicBlock(UncondDest);
273 MBB->erase(UncondBr);
274 TII.reverseBranchCondition(MI);
275 return true;
276 }
277 }
278 }
Chris Lattnereb15eee2006-10-13 20:43:10 +0000279#endif
Chris Lattner21ab22e2004-07-31 10:01:27 +0000280}