blob: 4d1370aed9827230e372df060405881a31d61894 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Collect the sequence of machine instructions for a basic block.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MachineBasicBlock.h"
15#include "llvm/BasicBlock.h"
16#include "llvm/CodeGen/MachineFunction.h"
Dan Gohman1e57df32008-02-10 18:45:23 +000017#include "llvm/Target/TargetRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Target/TargetData.h"
Chris Lattner8eaa5a92008-01-07 07:42:25 +000019#include "llvm/Target/TargetInstrDesc.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/Target/TargetMachine.h"
Dan Gohman8b3b5172008-07-17 23:49:46 +000021#include "llvm/Support/LeakDetector.h"
Daniel Dunbarf55f61f2009-07-24 10:36:58 +000022#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include <algorithm>
24using namespace llvm;
25
Dan Gohman221a4372008-07-07 23:14:23 +000026MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb)
Dan Gohman91057512009-10-30 01:27:03 +000027 : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false),
28 AddressTaken(false) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +000029 Insts.Parent = this;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030}
31
Dan Gohman8b3b5172008-07-17 23:49:46 +000032MachineBasicBlock::~MachineBasicBlock() {
33 LeakDetector::removeGarbageObject(this);
34}
35
Chris Lattner50777b42009-08-23 00:35:30 +000036raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) {
Daniel Dunbarf55f61f2009-07-24 10:36:58 +000037 MBB.print(OS);
38 return OS;
39}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040
Chris Lattnere45742f2008-01-01 01:12:31 +000041/// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the
42/// parent pointer of the MBB, the MBB numbering, and any instructions in the
43/// MBB to be on the right operand list for registers.
44///
45/// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
46/// gets the next available unique MBB number. If it is removed from a
47/// MachineFunction, it goes back to being #-1.
Chris Lattner50777b42009-08-23 00:35:30 +000048void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) {
Dan Gohman221a4372008-07-07 23:14:23 +000049 MachineFunction &MF = *N->getParent();
50 N->Number = MF.addToMBBNumbering(N);
Chris Lattnere45742f2008-01-01 01:12:31 +000051
52 // Make sure the instructions have their operands in the reginfo lists.
Dan Gohman221a4372008-07-07 23:14:23 +000053 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Chris Lattnere45742f2008-01-01 01:12:31 +000054 for (MachineBasicBlock::iterator I = N->begin(), E = N->end(); I != E; ++I)
55 I->AddRegOperandsToUseLists(RegInfo);
Dan Gohman8b3b5172008-07-17 23:49:46 +000056
57 LeakDetector::removeGarbageObject(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058}
59
Chris Lattner50777b42009-08-23 00:35:30 +000060void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) {
Chris Lattner7ce487f2007-12-31 04:56:33 +000061 N->getParent()->removeFromMBBNumbering(N->Number);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 N->Number = -1;
Dan Gohman8b3b5172008-07-17 23:49:46 +000063 LeakDetector::addGarbageObject(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064}
65
66
Chris Lattnere45742f2008-01-01 01:12:31 +000067/// addNodeToList (MI) - When we add an instruction to a basic block
68/// list, we update its parent pointer and add its operands from reg use/def
69/// lists if appropriate.
Chris Lattner50777b42009-08-23 00:35:30 +000070void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) {
Chris Lattner7ce487f2007-12-31 04:56:33 +000071 assert(N->getParent() == 0 && "machine instruction already in a basic block");
Dan Gohman221a4372008-07-07 23:14:23 +000072 N->setParent(Parent);
Chris Lattnere45742f2008-01-01 01:12:31 +000073
Dan Gohman221a4372008-07-07 23:14:23 +000074 // Add the instruction's register operands to their corresponding
75 // use/def lists.
76 MachineFunction *MF = Parent->getParent();
77 N->AddRegOperandsToUseLists(MF->getRegInfo());
Dan Gohman8b3b5172008-07-17 23:49:46 +000078
79 LeakDetector::removeGarbageObject(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080}
81
Chris Lattnere45742f2008-01-01 01:12:31 +000082/// removeNodeFromList (MI) - When we remove an instruction from a basic block
83/// list, we update its parent pointer and remove its operands from reg use/def
84/// lists if appropriate.
Chris Lattner50777b42009-08-23 00:35:30 +000085void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) {
Chris Lattner7ce487f2007-12-31 04:56:33 +000086 assert(N->getParent() != 0 && "machine instruction not in a basic block");
Dan Gohman221a4372008-07-07 23:14:23 +000087
88 // Remove from the use/def lists.
89 N->RemoveRegOperandsFromUseLists();
Chris Lattnere45742f2008-01-01 01:12:31 +000090
Chris Lattner7ce487f2007-12-31 04:56:33 +000091 N->setParent(0);
Dan Gohman8b3b5172008-07-17 23:49:46 +000092
93 LeakDetector::addGarbageObject(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094}
95
Chris Lattnere45742f2008-01-01 01:12:31 +000096/// transferNodesFromList (MI) - When moving a range of instructions from one
97/// MBB list to another, we need to update the parent pointers and the use/def
98/// lists.
Chris Lattner50777b42009-08-23 00:35:30 +000099void ilist_traits<MachineInstr>::
100transferNodesFromList(ilist_traits<MachineInstr> &fromList,
101 MachineBasicBlock::iterator first,
102 MachineBasicBlock::iterator last) {
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000103 assert(Parent->getParent() == fromList.Parent->getParent() &&
104 "MachineInstr parent mismatch!");
105
Chris Lattner7ce487f2007-12-31 04:56:33 +0000106 // Splice within the same MBB -> no change.
Dan Gohman221a4372008-07-07 23:14:23 +0000107 if (Parent == fromList.Parent) return;
Chris Lattnere45742f2008-01-01 01:12:31 +0000108
109 // If splicing between two blocks within the same function, just update the
110 // parent pointers.
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000111 for (; first != last; ++first)
Dan Gohman221a4372008-07-07 23:14:23 +0000112 first->setParent(Parent);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113}
114
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000115void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) {
Dan Gohman221a4372008-07-07 23:14:23 +0000116 assert(!MI->getParent() && "MI is still in a block!");
117 Parent->getParent()->DeleteMachineInstr(MI);
118}
119
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 iterator I = end();
Chris Lattner5b930372008-01-07 07:27:27 +0000122 while (I != begin() && (--I)->getDesc().isTerminator())
Anton Korobeynikova55c9f22007-09-02 22:11:14 +0000123 ; /*noop */
Chris Lattner5b930372008-01-07 07:27:27 +0000124 if (I != end() && !I->getDesc().isTerminator()) ++I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 return I;
126}
127
Chris Lattner1378a3a2009-08-18 04:33:15 +0000128/// isOnlyReachableViaFallthough - Return true if this basic block has
129/// exactly one predecessor and the control transfer mechanism between
130/// the predecessor and this block is a fall-through.
Chris Lattner01303c82009-08-18 04:30:35 +0000131bool MachineBasicBlock::isOnlyReachableByFallthrough() const {
132 // If this is a landing pad, it isn't a fall through. If it has no preds,
133 // then nothing falls through to it.
134 if (isLandingPad() || pred_empty())
135 return false;
136
137 // If there isn't exactly one predecessor, it can't be a fall through.
138 const_pred_iterator PI = pred_begin(), PI2 = PI;
Chris Lattnera59f39b2009-08-18 04:34:36 +0000139 ++PI2;
140 if (PI2 != pred_end())
Chris Lattner01303c82009-08-18 04:30:35 +0000141 return false;
142
143 // The predecessor has to be immediately before this block.
144 const MachineBasicBlock *Pred = *PI;
145
146 if (!Pred->isLayoutSuccessor(this))
147 return false;
148
149 // If the block is completely empty, then it definitely does fall through.
150 if (Pred->empty())
151 return true;
152
153 // Otherwise, check the last instruction.
154 const MachineInstr &LastInst = Pred->back();
Chris Lattner1378a3a2009-08-18 04:33:15 +0000155 return !LastInst.getDesc().isBarrier();
Dan Gohmand38f8762009-03-31 18:39:13 +0000156}
157
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158void MachineBasicBlock::dump() const {
Chris Lattner0bde4e32009-08-23 03:13:20 +0000159 print(errs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160}
161
Daniel Dunbarf55f61f2009-07-24 10:36:58 +0000162static inline void OutputReg(raw_ostream &os, unsigned RegNo,
Dan Gohman1e57df32008-02-10 18:45:23 +0000163 const TargetRegisterInfo *TRI = 0) {
164 if (!RegNo || TargetRegisterInfo::isPhysicalRegister(RegNo)) {
165 if (TRI)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000166 os << " %" << TRI->get(RegNo).Name;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 else
168 os << " %mreg(" << RegNo << ")";
169 } else
170 os << " %reg" << RegNo;
171}
172
Chris Lattnerb3fff702009-08-23 00:47:04 +0000173void MachineBasicBlock::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 const MachineFunction *MF = getParent();
Chris Lattner50777b42009-08-23 00:35:30 +0000175 if (!MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
177 << " is null\n";
178 return;
179 }
180
181 const BasicBlock *LBB = getBasicBlock();
Chris Lattner50777b42009-08-23 00:35:30 +0000182 OS << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 if (LBB) OS << LBB->getName() << ": ";
184 OS << (const void*)this
185 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber();
Evan Cheng45c1edb2008-02-28 00:43:03 +0000186 if (Alignment) OS << ", Alignment " << Alignment;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 if (isLandingPad()) OS << ", EH LANDING PAD";
Dan Gohman8f38bef2009-10-30 02:08:26 +0000188 if (hasAddressTaken()) OS << ", ADDRESS TAKEN";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 OS << ":\n";
190
Dan Gohman1e57df32008-02-10 18:45:23 +0000191 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000192 if (!livein_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 OS << "Live Ins:";
194 for (const_livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I)
Dan Gohman1e57df32008-02-10 18:45:23 +0000195 OutputReg(OS, *I, TRI);
Chris Lattner50777b42009-08-23 00:35:30 +0000196 OS << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 }
198 // Print the preds of this block according to the CFG.
199 if (!pred_empty()) {
200 OS << " Predecessors according to CFG:";
201 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
Chris Lattner50777b42009-08-23 00:35:30 +0000202 OS << ' ' << *PI << " (#" << (*PI)->getNumber() << ')';
203 OS << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 }
205
206 for (const_iterator I = begin(); I != end(); ++I) {
Chris Lattnerb3fff702009-08-23 00:47:04 +0000207 OS << '\t';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 I->print(OS, &getParent()->getTarget());
209 }
210
211 // Print the successors of this block according to the CFG.
212 if (!succ_empty()) {
213 OS << " Successors according to CFG:";
214 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI)
Chris Lattner50777b42009-08-23 00:35:30 +0000215 OS << ' ' << *SI << " (#" << (*SI)->getNumber() << ')';
216 OS << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 }
218}
219
220void MachineBasicBlock::removeLiveIn(unsigned Reg) {
221 livein_iterator I = std::find(livein_begin(), livein_end(), Reg);
222 assert(I != livein_end() && "Not a live in!");
223 LiveIns.erase(I);
224}
225
Evan Cheng194fe7c2008-04-24 09:06:33 +0000226bool MachineBasicBlock::isLiveIn(unsigned Reg) const {
227 const_livein_iterator I = std::find(livein_begin(), livein_end(), Reg);
228 return I != livein_end();
229}
230
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
Dan Gohman221a4372008-07-07 23:14:23 +0000232 getParent()->splice(NewAfter, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233}
234
235void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 MachineFunction::iterator BBI = NewBefore;
Dan Gohman221a4372008-07-07 23:14:23 +0000237 getParent()->splice(++BBI, this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238}
239
240
241void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
242 Successors.push_back(succ);
243 succ->addPredecessor(this);
244}
245
246void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
247 succ->removePredecessor(this);
248 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
249 assert(I != Successors.end() && "Not a current successor!");
250 Successors.erase(I);
251}
252
Chris Lattner7ce487f2007-12-31 04:56:33 +0000253MachineBasicBlock::succ_iterator
254MachineBasicBlock::removeSuccessor(succ_iterator I) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 assert(I != Successors.end() && "Not a current successor!");
256 (*I)->removePredecessor(this);
Dan Gohmand2826ae2009-01-08 22:19:34 +0000257 return Successors.erase(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258}
259
260void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
261 Predecessors.push_back(pred);
262}
263
264void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
265 std::vector<MachineBasicBlock *>::iterator I =
266 std::find(Predecessors.begin(), Predecessors.end(), pred);
267 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
268 Predecessors.erase(I);
269}
270
Chris Lattner50777b42009-08-23 00:35:30 +0000271void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) {
Mon P Wang078a62d2008-05-05 19:05:59 +0000272 if (this == fromMBB)
273 return;
274
Chris Lattner50777b42009-08-23 00:35:30 +0000275 for (MachineBasicBlock::succ_iterator I = fromMBB->succ_begin(),
276 E = fromMBB->succ_end(); I != E; ++I)
277 addSuccessor(*I);
278
279 while (!fromMBB->succ_empty())
Mon P Wang078a62d2008-05-05 19:05:59 +0000280 fromMBB->removeSuccessor(fromMBB->succ_begin());
281}
282
Dan Gohmanbca75222009-03-30 20:06:29 +0000283bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 std::vector<MachineBasicBlock *>::const_iterator I =
285 std::find(Successors.begin(), Successors.end(), MBB);
286 return I != Successors.end();
287}
288
Dan Gohmanbca75222009-03-30 20:06:29 +0000289bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const {
Dan Gohman901cf7c2008-10-02 22:09:09 +0000290 MachineFunction::const_iterator I(this);
291 return next(I) == MachineFunction::const_iterator(MBB);
292}
293
Dan Gohman221a4372008-07-07 23:14:23 +0000294/// removeFromParent - This method unlinks 'this' from the containing function,
295/// and returns it, but does not delete it.
296MachineBasicBlock *MachineBasicBlock::removeFromParent() {
297 assert(getParent() && "Not embedded in a function!");
298 getParent()->remove(this);
299 return this;
300}
301
302
303/// eraseFromParent - This method unlinks 'this' from the containing function,
304/// and deletes it.
305void MachineBasicBlock::eraseFromParent() {
306 assert(getParent() && "Not embedded in a function!");
307 getParent()->erase(this);
308}
309
310
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311/// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
312/// 'Old', change the code and CFG so that it branches to 'New' instead.
313void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old,
314 MachineBasicBlock *New) {
315 assert(Old != New && "Cannot replace self with self!");
316
317 MachineBasicBlock::iterator I = end();
318 while (I != begin()) {
319 --I;
Chris Lattner5b930372008-01-07 07:27:27 +0000320 if (!I->getDesc().isTerminator()) break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321
322 // Scan the operands of this machine instruction, replacing any uses of Old
323 // with New.
324 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000325 if (I->getOperand(i).isMBB() &&
Dan Gohman7f7f3652008-09-13 17:58:21 +0000326 I->getOperand(i).getMBB() == Old)
Chris Lattner6017d482007-12-30 23:10:15 +0000327 I->getOperand(i).setMBB(New);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 }
329
Dan Gohman710011c2009-05-05 21:10:19 +0000330 // Update the successor information.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 removeSuccessor(Old);
Dan Gohman710011c2009-05-05 21:10:19 +0000332 addSuccessor(New);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333}
334
335/// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the
336/// CFG to be inserted. If we have proven that MBB can only branch to DestA and
337/// DestB, remove any other MBB successors from the CFG. DestA and DestB can
338/// be null.
Chris Lattner7ce487f2007-12-31 04:56:33 +0000339/// Besides DestA and DestB, retain other edges leading to LandingPads
340/// (currently there can be only one; we don't check or require that here).
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341/// Note it is possible that DestA and/or DestB are LandingPads.
342bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA,
343 MachineBasicBlock *DestB,
344 bool isCond) {
345 bool MadeChange = false;
346 bool AddedFallThrough = false;
347
Dan Gohman221a4372008-07-07 23:14:23 +0000348 MachineFunction::iterator FallThru = next(MachineFunction::iterator(this));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349
350 // If this block ends with a conditional branch that falls through to its
351 // successor, set DestB as the successor.
352 if (isCond) {
353 if (DestB == 0 && FallThru != getParent()->end()) {
354 DestB = FallThru;
355 AddedFallThrough = true;
356 }
357 } else {
358 // If this is an unconditional branch with no explicit dest, it must just be
359 // a fallthrough into DestB.
360 if (DestA == 0 && FallThru != getParent()->end()) {
361 DestA = FallThru;
362 AddedFallThrough = true;
363 }
364 }
365
366 MachineBasicBlock::succ_iterator SI = succ_begin();
367 MachineBasicBlock *OrigDestA = DestA, *OrigDestB = DestB;
368 while (SI != succ_end()) {
369 if (*SI == DestA && DestA == DestB) {
370 DestA = DestB = 0;
371 ++SI;
372 } else if (*SI == DestA) {
373 DestA = 0;
374 ++SI;
375 } else if (*SI == DestB) {
376 DestB = 0;
377 ++SI;
378 } else if ((*SI)->isLandingPad() &&
379 *SI!=OrigDestA && *SI!=OrigDestB) {
380 ++SI;
381 } else {
382 // Otherwise, this is a superfluous edge, remove it.
383 SI = removeSuccessor(SI);
384 MadeChange = true;
385 }
386 }
387 if (!AddedFallThrough) {
388 assert(DestA == 0 && DestB == 0 &&
389 "MachineCFG is missing edges!");
390 } else if (isCond) {
391 assert(DestA == 0 && "MachineCFG is missing edges!");
392 }
393 return MadeChange;
394}