blob: 3b4b080c63ca48fb4b666a6dc72b2655b443eae7 [file] [log] [blame]
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +00001//===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===//
2//
3// 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.
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Collect the sequence of machine instructions for a basic block.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MachineBasicBlock.h"
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000015#include "llvm/BasicBlock.h"
Bill Wendling4bde1ab2009-12-11 01:49:14 +000016#include "llvm/ADT/SmallSet.h"
17#include "llvm/Assembly/Writer.h"
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000018#include "llvm/CodeGen/MachineFunction.h"
Owen Anderson07000c62006-05-12 06:33:49 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerf14cf852008-01-07 07:42:25 +000020#include "llvm/Target/TargetInstrDesc.h"
Jim Grosbach7707a0d2009-11-12 03:55:33 +000021#include "llvm/Target/TargetInstrInfo.h"
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +000022#include "llvm/Target/TargetMachine.h"
Bill Wendling4bde1ab2009-12-11 01:49:14 +000023#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000024#include "llvm/Support/LeakDetector.h"
Daniel Dunbar1cd1d982009-07-24 10:36:58 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattner52c09d72004-10-26 15:43:42 +000026#include <algorithm>
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000027using namespace llvm;
28
Dan Gohman8e5f2c62008-07-07 23:14:23 +000029MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb)
Dan Gohman8c2b5252009-10-30 01:27:03 +000030 : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false),
31 AddressTaken(false) {
Dan Gohmanfed90b62008-07-28 21:51:04 +000032 Insts.Parent = this;
Tanya Lattner17fb34b2004-05-24 07:14:35 +000033}
Tanya Lattner17fb34b2004-05-24 07:14:35 +000034
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000035MachineBasicBlock::~MachineBasicBlock() {
36 LeakDetector::removeGarbageObject(this);
37}
38
Chris Lattner6371ed52009-08-23 00:35:30 +000039raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) {
Daniel Dunbar1cd1d982009-07-24 10:36:58 +000040 MBB.print(OS);
41 return OS;
42}
Tanya Lattner17fb34b2004-05-24 07:14:35 +000043
Chris Lattner62ed6b92008-01-01 01:12:31 +000044/// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the
45/// parent pointer of the MBB, the MBB numbering, and any instructions in the
46/// MBB to be on the right operand list for registers.
47///
48/// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
49/// gets the next available unique MBB number. If it is removed from a
50/// MachineFunction, it goes back to being #-1.
Chris Lattner6371ed52009-08-23 00:35:30 +000051void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +000052 MachineFunction &MF = *N->getParent();
53 N->Number = MF.addToMBBNumbering(N);
Chris Lattner62ed6b92008-01-01 01:12:31 +000054
55 // Make sure the instructions have their operands in the reginfo lists.
Dan Gohman8e5f2c62008-07-07 23:14:23 +000056 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Chris Lattner62ed6b92008-01-01 01:12:31 +000057 for (MachineBasicBlock::iterator I = N->begin(), E = N->end(); I != E; ++I)
58 I->AddRegOperandsToUseLists(RegInfo);
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000059
60 LeakDetector::removeGarbageObject(N);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000061}
62
Chris Lattner6371ed52009-08-23 00:35:30 +000063void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) {
Chris Lattnerf20c1a42007-12-31 04:56:33 +000064 N->getParent()->removeFromMBBNumbering(N->Number);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000065 N->Number = -1;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000066 LeakDetector::addGarbageObject(N);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000067}
68
Chris Lattner5e61fa92004-02-19 16:13:54 +000069
Chris Lattner62ed6b92008-01-01 01:12:31 +000070/// addNodeToList (MI) - When we add an instruction to a basic block
71/// list, we update its parent pointer and add its operands from reg use/def
72/// lists if appropriate.
Chris Lattner6371ed52009-08-23 00:35:30 +000073void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) {
Chris Lattnerf20c1a42007-12-31 04:56:33 +000074 assert(N->getParent() == 0 && "machine instruction already in a basic block");
Dan Gohman8e5f2c62008-07-07 23:14:23 +000075 N->setParent(Parent);
Chris Lattner62ed6b92008-01-01 01:12:31 +000076
Dan Gohman8e5f2c62008-07-07 23:14:23 +000077 // Add the instruction's register operands to their corresponding
78 // use/def lists.
79 MachineFunction *MF = Parent->getParent();
80 N->AddRegOperandsToUseLists(MF->getRegInfo());
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000081
82 LeakDetector::removeGarbageObject(N);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000083}
84
Chris Lattner62ed6b92008-01-01 01:12:31 +000085/// removeNodeFromList (MI) - When we remove an instruction from a basic block
86/// list, we update its parent pointer and remove its operands from reg use/def
87/// lists if appropriate.
Chris Lattner6371ed52009-08-23 00:35:30 +000088void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) {
Chris Lattnerf20c1a42007-12-31 04:56:33 +000089 assert(N->getParent() != 0 && "machine instruction not in a basic block");
Dan Gohman8e5f2c62008-07-07 23:14:23 +000090
91 // Remove from the use/def lists.
92 N->RemoveRegOperandsFromUseLists();
Chris Lattner62ed6b92008-01-01 01:12:31 +000093
Chris Lattnerf20c1a42007-12-31 04:56:33 +000094 N->setParent(0);
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000095
96 LeakDetector::addGarbageObject(N);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000097}
98
Chris Lattner62ed6b92008-01-01 01:12:31 +000099/// transferNodesFromList (MI) - When moving a range of instructions from one
100/// MBB list to another, we need to update the parent pointers and the use/def
101/// lists.
Chris Lattner6371ed52009-08-23 00:35:30 +0000102void ilist_traits<MachineInstr>::
103transferNodesFromList(ilist_traits<MachineInstr> &fromList,
104 MachineBasicBlock::iterator first,
105 MachineBasicBlock::iterator last) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000106 assert(Parent->getParent() == fromList.Parent->getParent() &&
107 "MachineInstr parent mismatch!");
108
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000109 // Splice within the same MBB -> no change.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000110 if (Parent == fromList.Parent) return;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000111
112 // If splicing between two blocks within the same function, just update the
113 // parent pointers.
Dan Gohmanfed90b62008-07-28 21:51:04 +0000114 for (; first != last; ++first)
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000115 first->setParent(Parent);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000116}
117
Dan Gohmanfed90b62008-07-28 21:51:04 +0000118void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000119 assert(!MI->getParent() && "MI is still in a block!");
120 Parent->getParent()->DeleteMachineInstr(MI);
121}
122
Chris Lattner52c09d72004-10-26 15:43:42 +0000123MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +0000124 iterator I = end();
Chris Lattner749c6f62008-01-07 07:27:27 +0000125 while (I != begin() && (--I)->getDesc().isTerminator())
Anton Korobeynikov406452d2007-09-02 22:11:14 +0000126 ; /*noop */
Chris Lattner749c6f62008-01-07 07:27:27 +0000127 if (I != end() && !I->getDesc().isTerminator()) ++I;
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +0000128 return I;
129}
130
Chris Lattnerb49a30c2009-08-18 04:33:15 +0000131/// isOnlyReachableViaFallthough - Return true if this basic block has
132/// exactly one predecessor and the control transfer mechanism between
133/// the predecessor and this block is a fall-through.
Chris Lattnera006d4e2009-08-18 04:30:35 +0000134bool MachineBasicBlock::isOnlyReachableByFallthrough() const {
135 // If this is a landing pad, it isn't a fall through. If it has no preds,
136 // then nothing falls through to it.
137 if (isLandingPad() || pred_empty())
138 return false;
139
140 // If there isn't exactly one predecessor, it can't be a fall through.
141 const_pred_iterator PI = pred_begin(), PI2 = PI;
Chris Lattner1f50fc72009-08-18 04:34:36 +0000142 ++PI2;
143 if (PI2 != pred_end())
Chris Lattnera006d4e2009-08-18 04:30:35 +0000144 return false;
145
146 // The predecessor has to be immediately before this block.
147 const MachineBasicBlock *Pred = *PI;
148
149 if (!Pred->isLayoutSuccessor(this))
150 return false;
151
152 // If the block is completely empty, then it definitely does fall through.
153 if (Pred->empty())
154 return true;
155
156 // Otherwise, check the last instruction.
157 const MachineInstr &LastInst = Pred->back();
Chris Lattnerb49a30c2009-08-18 04:33:15 +0000158 return !LastInst.getDesc().isBarrier();
Dan Gohman968dc7a2009-03-31 18:39:13 +0000159}
160
Chris Lattner52c09d72004-10-26 15:43:42 +0000161void MachineBasicBlock::dump() const {
Chris Lattnercf143a42009-08-23 03:13:20 +0000162 print(errs());
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000163}
164
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000165static inline void OutputReg(raw_ostream &os, unsigned RegNo,
Dan Gohman6f0d0242008-02-10 18:45:23 +0000166 const TargetRegisterInfo *TRI = 0) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000167 if (RegNo != 0 && TargetRegisterInfo::isPhysicalRegister(RegNo)) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000168 if (TRI)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000169 os << " %" << TRI->get(RegNo).Name;
Evan Cheng13d82852007-02-10 02:38:19 +0000170 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000171 os << " %physreg" << RegNo;
Evan Cheng13d82852007-02-10 02:38:19 +0000172 } else
173 os << " %reg" << RegNo;
174}
175
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000176StringRef MachineBasicBlock::getName() const {
177 if (const BasicBlock *LBB = getBasicBlock())
178 return LBB->getName();
179 else
180 return "(null)";
181}
182
Chris Lattner2d8e3d22009-08-23 00:47:04 +0000183void MachineBasicBlock::print(raw_ostream &OS) const {
Evan Cheng13d82852007-02-10 02:38:19 +0000184 const MachineFunction *MF = getParent();
Chris Lattner6371ed52009-08-23 00:35:30 +0000185 if (!MF) {
Chris Lattner52c09d72004-10-26 15:43:42 +0000186 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
187 << " is null\n";
Tanya Lattner792699c2004-05-24 06:11:51 +0000188 return;
189 }
Alkis Evlogimenosa6382862004-09-05 18:39:20 +0000190
Dan Gohman0ba90f32009-10-31 20:19:03 +0000191 if (Alignment) { OS << "Alignment " << Alignment << "\n"; }
192
193 OS << "BB#" << getNumber() << ": ";
194
195 const char *Comma = "";
196 if (const BasicBlock *LBB = getBasicBlock()) {
197 OS << Comma << "derived from LLVM BB ";
198 WriteAsOperand(OS, LBB, /*PrintType=*/false);
199 Comma = ", ";
200 }
201 if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; }
202 if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; }
Chris Lattner6371ed52009-08-23 00:35:30 +0000203 OS << '\n';
Evan Cheng13d82852007-02-10 02:38:19 +0000204
Dan Gohman6f0d0242008-02-10 18:45:23 +0000205 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
Dan Gohmancb406c22007-10-03 19:26:29 +0000206 if (!livein_empty()) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000207 OS << " Live Ins:";
Evan Chengb371f452007-02-19 21:49:54 +0000208 for (const_livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I)
Dan Gohman6f0d0242008-02-10 18:45:23 +0000209 OutputReg(OS, *I, TRI);
Chris Lattner6371ed52009-08-23 00:35:30 +0000210 OS << '\n';
Evan Cheng13d82852007-02-10 02:38:19 +0000211 }
Chris Lattner681764b2006-09-26 03:41:59 +0000212 // Print the preds of this block according to the CFG.
213 if (!pred_empty()) {
214 OS << " Predecessors according to CFG:";
215 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000216 OS << " BB#" << (*PI)->getNumber();
Chris Lattner6371ed52009-08-23 00:35:30 +0000217 OS << '\n';
Chris Lattner681764b2006-09-26 03:41:59 +0000218 }
219
Alkis Evlogimenosa6382862004-09-05 18:39:20 +0000220 for (const_iterator I = begin(); I != end(); ++I) {
Chris Lattner2d8e3d22009-08-23 00:47:04 +0000221 OS << '\t';
Alkis Evlogimenosa6382862004-09-05 18:39:20 +0000222 I->print(OS, &getParent()->getTarget());
223 }
Chris Lattner380ae492005-04-01 06:48:38 +0000224
225 // Print the successors of this block according to the CFG.
226 if (!succ_empty()) {
227 OS << " Successors according to CFG:";
228 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000229 OS << " BB#" << (*SI)->getNumber();
Chris Lattner6371ed52009-08-23 00:35:30 +0000230 OS << '\n';
Chris Lattner380ae492005-04-01 06:48:38 +0000231 }
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000232}
Chris Lattner52c09d72004-10-26 15:43:42 +0000233
Evan Chengb371f452007-02-19 21:49:54 +0000234void MachineBasicBlock::removeLiveIn(unsigned Reg) {
235 livein_iterator I = std::find(livein_begin(), livein_end(), Reg);
236 assert(I != livein_end() && "Not a live in!");
237 LiveIns.erase(I);
238}
239
Evan Chenga971dbd2008-04-24 09:06:33 +0000240bool MachineBasicBlock::isLiveIn(unsigned Reg) const {
241 const_livein_iterator I = std::find(livein_begin(), livein_end(), Reg);
242 return I != livein_end();
243}
244
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000245void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000246 getParent()->splice(NewAfter, this);
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000247}
248
249void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000250 MachineFunction::iterator BBI = NewBefore;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000251 getParent()->splice(++BBI, this);
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000252}
253
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000254void MachineBasicBlock::updateTerminator() {
255 const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo();
256 // A block with no successors has no concerns with fall-through edges.
257 if (this->succ_empty()) return;
258
259 MachineBasicBlock *TBB = 0, *FBB = 0;
260 SmallVector<MachineOperand, 4> Cond;
261 bool B = TII->AnalyzeBranch(*this, TBB, FBB, Cond);
262 (void) B;
263 assert(!B && "UpdateTerminators requires analyzable predecessors!");
264 if (Cond.empty()) {
265 if (TBB) {
266 // The block has an unconditional branch. If its successor is now
267 // its layout successor, delete the branch.
268 if (isLayoutSuccessor(TBB))
269 TII->RemoveBranch(*this);
270 } else {
271 // The block has an unconditional fallthrough. If its successor is not
272 // its layout successor, insert a branch.
273 TBB = *succ_begin();
274 if (!isLayoutSuccessor(TBB))
275 TII->InsertBranch(*this, TBB, 0, Cond);
276 }
277 } else {
278 if (FBB) {
279 // The block has a non-fallthrough conditional branch. If one of its
280 // successors is its layout successor, rewrite it to a fallthrough
281 // conditional branch.
282 if (isLayoutSuccessor(TBB)) {
Jakob Stoklund Olesene0239932009-11-22 18:28:04 +0000283 if (TII->ReverseBranchCondition(Cond))
284 return;
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000285 TII->RemoveBranch(*this);
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000286 TII->InsertBranch(*this, FBB, 0, Cond);
287 } else if (isLayoutSuccessor(FBB)) {
288 TII->RemoveBranch(*this);
289 TII->InsertBranch(*this, TBB, 0, Cond);
290 }
291 } else {
292 // The block has a fallthrough conditional branch.
293 MachineBasicBlock *MBBA = *succ_begin();
Chris Lattner7896c9f2009-12-03 00:50:42 +0000294 MachineBasicBlock *MBBB = *llvm::next(succ_begin());
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000295 if (MBBA == TBB) std::swap(MBBB, MBBA);
296 if (isLayoutSuccessor(TBB)) {
Jakob Stoklund Olesene0239932009-11-22 18:28:04 +0000297 if (TII->ReverseBranchCondition(Cond)) {
298 // We can't reverse the condition, add an unconditional branch.
299 Cond.clear();
300 TII->InsertBranch(*this, MBBA, 0, Cond);
301 return;
302 }
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000303 TII->RemoveBranch(*this);
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000304 TII->InsertBranch(*this, MBBA, 0, Cond);
305 } else if (!isLayoutSuccessor(MBBA)) {
306 TII->RemoveBranch(*this);
307 TII->InsertBranch(*this, TBB, MBBA, Cond);
308 }
309 }
310 }
311}
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000312
Chris Lattner52c09d72004-10-26 15:43:42 +0000313void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
314 Successors.push_back(succ);
315 succ->addPredecessor(this);
316}
317
318void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
319 succ->removePredecessor(this);
320 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
321 assert(I != Successors.end() && "Not a current successor!");
322 Successors.erase(I);
323}
324
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000325MachineBasicBlock::succ_iterator
326MachineBasicBlock::removeSuccessor(succ_iterator I) {
Chris Lattner52c09d72004-10-26 15:43:42 +0000327 assert(I != Successors.end() && "Not a current successor!");
328 (*I)->removePredecessor(this);
Dan Gohman5d5ee802009-01-08 22:19:34 +0000329 return Successors.erase(I);
Chris Lattner52c09d72004-10-26 15:43:42 +0000330}
331
332void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
333 Predecessors.push_back(pred);
334}
335
336void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000337 std::vector<MachineBasicBlock *>::iterator I =
Chris Lattner52c09d72004-10-26 15:43:42 +0000338 std::find(Predecessors.begin(), Predecessors.end(), pred);
339 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
340 Predecessors.erase(I);
341}
Evan Cheng4f098782007-05-17 23:58:53 +0000342
Chris Lattner6371ed52009-08-23 00:35:30 +0000343void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) {
Mon P Wang63307c32008-05-05 19:05:59 +0000344 if (this == fromMBB)
345 return;
346
Chris Lattner6371ed52009-08-23 00:35:30 +0000347 for (MachineBasicBlock::succ_iterator I = fromMBB->succ_begin(),
348 E = fromMBB->succ_end(); I != E; ++I)
349 addSuccessor(*I);
350
351 while (!fromMBB->succ_empty())
Mon P Wang63307c32008-05-05 19:05:59 +0000352 fromMBB->removeSuccessor(fromMBB->succ_begin());
353}
354
Dan Gohman6d1b89e2009-03-30 20:06:29 +0000355bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const {
Evan Cheng4f098782007-05-17 23:58:53 +0000356 std::vector<MachineBasicBlock *>::const_iterator I =
357 std::find(Successors.begin(), Successors.end(), MBB);
358 return I != Successors.end();
359}
Evan Cheng0370fad2007-06-04 06:44:01 +0000360
Dan Gohman6d1b89e2009-03-30 20:06:29 +0000361bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const {
Dan Gohman6ade6f52008-10-02 22:09:09 +0000362 MachineFunction::const_iterator I(this);
Chris Lattner7896c9f2009-12-03 00:50:42 +0000363 return llvm::next(I) == MachineFunction::const_iterator(MBB);
Dan Gohman6ade6f52008-10-02 22:09:09 +0000364}
365
Bob Wilson15acadd2009-11-26 00:32:21 +0000366bool MachineBasicBlock::canFallThrough() {
Bob Wilson15acadd2009-11-26 00:32:21 +0000367 MachineFunction::iterator Fallthrough = this;
368 ++Fallthrough;
369 // If FallthroughBlock is off the end of the function, it can't fall through.
370 if (Fallthrough == getParent()->end())
371 return false;
372
373 // If FallthroughBlock isn't a successor, no fallthrough is possible.
374 if (!isSuccessor(Fallthrough))
375 return false;
376
Dan Gohman735985f2009-12-05 00:32:59 +0000377 // Analyze the branches, if any, at the end of the block.
378 MachineBasicBlock *TBB = 0, *FBB = 0;
379 SmallVector<MachineOperand, 4> Cond;
380 const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo();
381 if (TII->AnalyzeBranch(*this, TBB, FBB, Cond, true)) {
382 // If we couldn't analyze the branch, examine the last instruction.
383 // If the block doesn't end in a known control barrier, assume fallthrough
384 // is possible. The isPredicable check is needed because this code can be
385 // called during IfConversion, where an instruction which is normally a
386 // Barrier is predicated and thus no longer an actual control barrier. This
387 // is over-conservative though, because if an instruction isn't actually
388 // predicated we could still treat it like a barrier.
Bob Wilson15acadd2009-11-26 00:32:21 +0000389 return empty() || !back().getDesc().isBarrier() ||
390 back().getDesc().isPredicable();
Dan Gohman735985f2009-12-05 00:32:59 +0000391 }
Bob Wilson15acadd2009-11-26 00:32:21 +0000392
393 // If there is no branch, control always falls through.
394 if (TBB == 0) return true;
395
396 // If there is some explicit branch to the fallthrough block, it can obviously
397 // reach, even though the branch should get folded to fall through implicitly.
398 if (MachineFunction::iterator(TBB) == Fallthrough ||
399 MachineFunction::iterator(FBB) == Fallthrough)
400 return true;
401
402 // If it's an unconditional branch to some block not the fall through, it
403 // doesn't fall through.
404 if (Cond.empty()) return false;
405
406 // Otherwise, if it is conditional and has no explicit false block, it falls
407 // through.
408 return FBB == 0;
409}
410
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000411/// removeFromParent - This method unlinks 'this' from the containing function,
412/// and returns it, but does not delete it.
413MachineBasicBlock *MachineBasicBlock::removeFromParent() {
414 assert(getParent() && "Not embedded in a function!");
415 getParent()->remove(this);
416 return this;
417}
418
419
420/// eraseFromParent - This method unlinks 'this' from the containing function,
421/// and deletes it.
422void MachineBasicBlock::eraseFromParent() {
423 assert(getParent() && "Not embedded in a function!");
424 getParent()->erase(this);
425}
426
427
Evan Cheng0370fad2007-06-04 06:44:01 +0000428/// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
429/// 'Old', change the code and CFG so that it branches to 'New' instead.
430void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old,
431 MachineBasicBlock *New) {
432 assert(Old != New && "Cannot replace self with self!");
433
434 MachineBasicBlock::iterator I = end();
435 while (I != begin()) {
436 --I;
Chris Lattner749c6f62008-01-07 07:27:27 +0000437 if (!I->getDesc().isTerminator()) break;
Evan Cheng0370fad2007-06-04 06:44:01 +0000438
439 // Scan the operands of this machine instruction, replacing any uses of Old
440 // with New.
441 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
Dan Gohmand735b802008-10-03 15:45:36 +0000442 if (I->getOperand(i).isMBB() &&
Dan Gohman014278e2008-09-13 17:58:21 +0000443 I->getOperand(i).getMBB() == Old)
Chris Lattner8aa797a2007-12-30 23:10:15 +0000444 I->getOperand(i).setMBB(New);
Evan Cheng0370fad2007-06-04 06:44:01 +0000445 }
446
Dan Gohman5412d062009-05-05 21:10:19 +0000447 // Update the successor information.
Evan Cheng0370fad2007-06-04 06:44:01 +0000448 removeSuccessor(Old);
Dan Gohman5412d062009-05-05 21:10:19 +0000449 addSuccessor(New);
Evan Cheng0370fad2007-06-04 06:44:01 +0000450}
451
Bill Wendling64bdde22009-12-11 03:14:18 +0000452/// BranchesToLandingPad - The basic block is a landing pad or branches only to
453/// a landing pad. No other instructions are present other than the
454/// unconditional branch.
Bill Wendling4bde1ab2009-12-11 01:49:14 +0000455bool
456MachineBasicBlock::BranchesToLandingPad(const MachineBasicBlock *MBB) const {
457 SmallSet<const MachineBasicBlock*, 32> Visited;
458 const MachineBasicBlock *CurMBB = MBB;
459
Bill Wendlingc42a0b72009-12-11 21:47:36 +0000460 while (!CurMBB->isLandingPad()) {
461 if (CurMBB->succ_size() != 1) break;
462 if (!Visited.insert(CurMBB)) break;
Bill Wendling4bde1ab2009-12-11 01:49:14 +0000463 CurMBB = *CurMBB->succ_begin();
464 }
465
466 return CurMBB->isLandingPad();
467}
468
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000469/// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the
470/// CFG to be inserted. If we have proven that MBB can only branch to DestA and
471/// DestB, remove any other MBB successors from the CFG. DestA and DestB can
472/// be null.
Bill Wendling4bde1ab2009-12-11 01:49:14 +0000473///
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000474/// Besides DestA and DestB, retain other edges leading to LandingPads
475/// (currently there can be only one; we don't check or require that here).
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000476/// Note it is possible that DestA and/or DestB are LandingPads.
477bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA,
478 MachineBasicBlock *DestB,
479 bool isCond) {
480 bool MadeChange = false;
481 bool AddedFallThrough = false;
482
Chris Lattner7896c9f2009-12-03 00:50:42 +0000483 MachineFunction::iterator FallThru =
484 llvm::next(MachineFunction::iterator(this));
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000485
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000486 if (isCond) {
Bill Wendling85de1e52009-12-14 06:51:19 +0000487 // If this block ends with a conditional branch that falls through to its
488 // successor, set DestB as the successor.
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000489 if (DestB == 0 && FallThru != getParent()->end()) {
490 DestB = FallThru;
491 AddedFallThrough = true;
492 }
493 } else {
494 // If this is an unconditional branch with no explicit dest, it must just be
Bill Wendling85de1e52009-12-14 06:51:19 +0000495 // a fallthrough into DestA.
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000496 if (DestA == 0 && FallThru != getParent()->end()) {
497 DestA = FallThru;
498 AddedFallThrough = true;
499 }
500 }
501
502 MachineBasicBlock::succ_iterator SI = succ_begin();
Bill Wendling4bde1ab2009-12-11 01:49:14 +0000503 const MachineBasicBlock *OrigDestA = DestA, *OrigDestB = DestB;
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000504 while (SI != succ_end()) {
Bill Wendling4bde1ab2009-12-11 01:49:14 +0000505 const MachineBasicBlock *MBB = *SI;
506 if (MBB == DestA) {
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000507 DestA = 0;
508 ++SI;
Bill Wendling4bde1ab2009-12-11 01:49:14 +0000509 } else if (MBB == DestB) {
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000510 DestB = 0;
511 ++SI;
Bill Wendling64bdde22009-12-11 03:14:18 +0000512 } else if (MBB != OrigDestA && MBB != OrigDestB &&
513 BranchesToLandingPad(MBB)) {
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000514 ++SI;
515 } else {
516 // Otherwise, this is a superfluous edge, remove it.
David Greene8a46d342007-06-29 02:45:24 +0000517 SI = removeSuccessor(SI);
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000518 MadeChange = true;
519 }
520 }
Bill Wendling4bde1ab2009-12-11 01:49:14 +0000521
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000522 if (!AddedFallThrough) {
523 assert(DestA == 0 && DestB == 0 &&
524 "MachineCFG is missing edges!");
525 } else if (isCond) {
526 assert(DestA == 0 && "MachineCFG is missing edges!");
527 }
Bill Wendling4bde1ab2009-12-11 01:49:14 +0000528
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000529 return MadeChange;
530}
Evan Cheng2a085c32009-11-17 19:19:59 +0000531
532void llvm::WriteAsOperand(raw_ostream &OS, const MachineBasicBlock *MBB,
533 bool t) {
534 OS << "BB#" << MBB->getNumber();
535}