blob: e5be62df368a1be438180afb52eebbfec99a7b30 [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"
16#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/CodeGen/MachineInstr.h"
Evan Cheng13d82852007-02-10 02:38:19 +000018#include "llvm/Target/MRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000019#include "llvm/Target/TargetData.h"
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +000020#include "llvm/Target/TargetInstrInfo.h"
21#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/LeakDetector.h"
Chris Lattner52c09d72004-10-26 15:43:42 +000023#include <algorithm>
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000024using namespace llvm;
25
Tanya Lattner17fb34b2004-05-24 07:14:35 +000026MachineBasicBlock::~MachineBasicBlock() {
27 LeakDetector::removeGarbageObject(this);
28}
Tanya Lattner17fb34b2004-05-24 07:14:35 +000029
Chris Lattner1ccc4682006-11-18 21:47:36 +000030std::ostream& llvm::operator<<(std::ostream &OS, const MachineBasicBlock &MBB) {
31 MBB.print(OS);
32 return OS;
33}
Tanya Lattner17fb34b2004-05-24 07:14:35 +000034
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000035// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000036// gets the next available unique MBB number. If it is removed from a
37// MachineFunction, it goes back to being #-1.
Chris Lattnerca48eb92004-07-01 06:02:27 +000038void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) {
Tanya Lattner792699c2004-05-24 06:11:51 +000039 assert(N->Parent == 0 && "machine instruction already in a basic block");
Tanya Lattner17fb34b2004-05-24 07:14:35 +000040 N->Parent = Parent;
Chris Lattnerca48eb92004-07-01 06:02:27 +000041 N->Number = Parent->addToMBBNumbering(N);
Tanya Lattner792699c2004-05-24 06:11:51 +000042 LeakDetector::removeGarbageObject(N);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000043}
44
Chris Lattnerca48eb92004-07-01 06:02:27 +000045void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) {
Tanya Lattner792699c2004-05-24 06:11:51 +000046 assert(N->Parent != 0 && "machine instruction not in a basic block");
Chris Lattnerca48eb92004-07-01 06:02:27 +000047 N->Parent->removeFromMBBNumbering(N->Number);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000048 N->Number = -1;
Chris Lattnerca48eb92004-07-01 06:02:27 +000049 N->Parent = 0;
Tanya Lattner792699c2004-05-24 06:11:51 +000050 LeakDetector::addGarbageObject(N);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000051}
52
Chris Lattner5e61fa92004-02-19 16:13:54 +000053
Chris Lattnerbca81442005-01-30 00:09:23 +000054MachineInstr* ilist_traits<MachineInstr>::createSentinel() {
Evan Chengc0f64ff2006-11-27 23:37:22 +000055 MachineInstr* dummy = new MachineInstr();
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000056 LeakDetector::removeGarbageObject(dummy);
57 return dummy;
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000058}
59
Chris Lattner52c09d72004-10-26 15:43:42 +000060void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) {
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000061 assert(N->parent == 0 && "machine instruction already in a basic block");
62 N->parent = parent;
63 LeakDetector::removeGarbageObject(N);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000064}
65
Chris Lattner52c09d72004-10-26 15:43:42 +000066void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) {
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000067 assert(N->parent != 0 && "machine instruction not in a basic block");
68 N->parent = 0;
69 LeakDetector::addGarbageObject(N);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000070}
71
72void ilist_traits<MachineInstr>::transferNodesFromList(
Chris Lattner55046022006-10-06 01:12:44 +000073 iplist<MachineInstr, ilist_traits<MachineInstr> >& fromList,
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000074 ilist_iterator<MachineInstr> first,
Chris Lattner52c09d72004-10-26 15:43:42 +000075 ilist_iterator<MachineInstr> last) {
Chris Lattner55046022006-10-06 01:12:44 +000076 if (parent != fromList.parent)
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000077 for (; first != last; ++first)
Chris Lattner55046022006-10-06 01:12:44 +000078 first->parent = parent;
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000079}
80
Chris Lattner52c09d72004-10-26 15:43:42 +000081MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
Chris Lattner9bcdcd12004-06-02 05:57:12 +000082 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo();
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +000083 iterator I = end();
Anton Korobeynikov406452d2007-09-02 22:11:14 +000084 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()))
85 ; /*noop */
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +000086 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
87 return I;
88}
89
Chris Lattner52c09d72004-10-26 15:43:42 +000090void MachineBasicBlock::dump() const {
Bill Wendlingbcd24982006-12-07 20:28:15 +000091 print(*cerr.stream());
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000092}
93
Evan Cheng13d82852007-02-10 02:38:19 +000094static inline void OutputReg(std::ostream &os, unsigned RegNo,
95 const MRegisterInfo *MRI = 0) {
96 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
97 if (MRI)
98 os << " %" << MRI->get(RegNo).Name;
99 else
100 os << " %mreg(" << RegNo << ")";
101 } else
102 os << " %reg" << RegNo;
103}
104
Chris Lattner52c09d72004-10-26 15:43:42 +0000105void MachineBasicBlock::print(std::ostream &OS) const {
Evan Cheng13d82852007-02-10 02:38:19 +0000106 const MachineFunction *MF = getParent();
107 if(!MF) {
Chris Lattner52c09d72004-10-26 15:43:42 +0000108 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
109 << " is null\n";
Tanya Lattner792699c2004-05-24 06:11:51 +0000110 return;
111 }
Alkis Evlogimenosa6382862004-09-05 18:39:20 +0000112
113 const BasicBlock *LBB = getBasicBlock();
Chris Lattnerdb3ea672006-10-06 21:28:17 +0000114 OS << "\n";
Chris Lattnerc11ce862007-04-30 23:12:53 +0000115 if (LBB) OS << LBB->getName() << ": ";
116 OS << (const void*)this
117 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber();
118 if (isLandingPad()) OS << ", EH LANDING PAD";
119 OS << ":\n";
Evan Cheng13d82852007-02-10 02:38:19 +0000120
121 const MRegisterInfo *MRI = MF->getTarget().getRegisterInfo();
Dan Gohmancb406c22007-10-03 19:26:29 +0000122 if (!livein_empty()) {
Evan Cheng13d82852007-02-10 02:38:19 +0000123 OS << "Live Ins:";
Evan Chengb371f452007-02-19 21:49:54 +0000124 for (const_livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I)
Evan Cheng13d82852007-02-10 02:38:19 +0000125 OutputReg(OS, *I, MRI);
126 OS << "\n";
127 }
Chris Lattner681764b2006-09-26 03:41:59 +0000128 // Print the preds of this block according to the CFG.
129 if (!pred_empty()) {
130 OS << " Predecessors according to CFG:";
131 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
Evan Cheng21970512007-03-09 08:29:08 +0000132 OS << " " << *PI << " (#" << (*PI)->getNumber() << ")";
Chris Lattner681764b2006-09-26 03:41:59 +0000133 OS << "\n";
134 }
135
Alkis Evlogimenosa6382862004-09-05 18:39:20 +0000136 for (const_iterator I = begin(); I != end(); ++I) {
137 OS << "\t";
138 I->print(OS, &getParent()->getTarget());
139 }
Chris Lattner380ae492005-04-01 06:48:38 +0000140
141 // Print the successors of this block according to the CFG.
142 if (!succ_empty()) {
143 OS << " Successors according to CFG:";
144 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI)
Evan Cheng21970512007-03-09 08:29:08 +0000145 OS << " " << *SI << " (#" << (*SI)->getNumber() << ")";
Chris Lattner380ae492005-04-01 06:48:38 +0000146 OS << "\n";
147 }
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000148}
Chris Lattner52c09d72004-10-26 15:43:42 +0000149
Evan Chengb371f452007-02-19 21:49:54 +0000150void MachineBasicBlock::removeLiveIn(unsigned Reg) {
151 livein_iterator I = std::find(livein_begin(), livein_end(), Reg);
152 assert(I != livein_end() && "Not a live in!");
153 LiveIns.erase(I);
154}
155
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000156void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
157 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
158 getParent()->getBasicBlockList().splice(NewAfter, BBList, this);
159}
160
161void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
162 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
163 MachineFunction::iterator BBI = NewBefore;
164 getParent()->getBasicBlockList().splice(++BBI, BBList, this);
165}
166
167
Chris Lattner52c09d72004-10-26 15:43:42 +0000168void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
169 Successors.push_back(succ);
170 succ->addPredecessor(this);
171}
172
173void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
174 succ->removePredecessor(this);
175 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
176 assert(I != Successors.end() && "Not a current successor!");
177 Successors.erase(I);
178}
179
David Greene8a46d342007-06-29 02:45:24 +0000180MachineBasicBlock::succ_iterator MachineBasicBlock::removeSuccessor(succ_iterator I) {
Chris Lattner52c09d72004-10-26 15:43:42 +0000181 assert(I != Successors.end() && "Not a current successor!");
182 (*I)->removePredecessor(this);
David Greene8a46d342007-06-29 02:45:24 +0000183 return(Successors.erase(I));
Chris Lattner52c09d72004-10-26 15:43:42 +0000184}
185
186void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
187 Predecessors.push_back(pred);
188}
189
190void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000191 std::vector<MachineBasicBlock *>::iterator I =
Chris Lattner52c09d72004-10-26 15:43:42 +0000192 std::find(Predecessors.begin(), Predecessors.end(), pred);
193 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
194 Predecessors.erase(I);
195}
Evan Cheng4f098782007-05-17 23:58:53 +0000196
197bool MachineBasicBlock::isSuccessor(MachineBasicBlock *MBB) const {
198 std::vector<MachineBasicBlock *>::const_iterator I =
199 std::find(Successors.begin(), Successors.end(), MBB);
200 return I != Successors.end();
201}
Evan Cheng0370fad2007-06-04 06:44:01 +0000202
203/// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
204/// 'Old', change the code and CFG so that it branches to 'New' instead.
205void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old,
206 MachineBasicBlock *New) {
207 assert(Old != New && "Cannot replace self with self!");
208
209 MachineBasicBlock::iterator I = end();
210 while (I != begin()) {
211 --I;
212 if (!(I->getInstrDescriptor()->Flags & M_TERMINATOR_FLAG)) break;
213
214 // Scan the operands of this machine instruction, replacing any uses of Old
215 // with New.
216 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
217 if (I->getOperand(i).isMachineBasicBlock() &&
218 I->getOperand(i).getMachineBasicBlock() == Old)
219 I->getOperand(i).setMachineBasicBlock(New);
220 }
221
222 // Update the successor information. If New was already a successor, just
223 // remove the link to Old instead of creating another one. PR 1444.
224 removeSuccessor(Old);
225 if (!isSuccessor(New))
226 addSuccessor(New);
227}
228
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000229/// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the
230/// CFG to be inserted. If we have proven that MBB can only branch to DestA and
231/// DestB, remove any other MBB successors from the CFG. DestA and DestB can
232/// be null.
233/// Besides DestA and DestB, retain other edges leading to LandingPads (currently
234/// there can be only one; we don't check or require that here).
235/// Note it is possible that DestA and/or DestB are LandingPads.
236bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA,
237 MachineBasicBlock *DestB,
238 bool isCond) {
239 bool MadeChange = false;
240 bool AddedFallThrough = false;
241
242 MachineBasicBlock *FallThru = getNext();
243
244 // If this block ends with a conditional branch that falls through to its
245 // successor, set DestB as the successor.
246 if (isCond) {
247 if (DestB == 0 && FallThru != getParent()->end()) {
248 DestB = FallThru;
249 AddedFallThrough = true;
250 }
251 } else {
252 // If this is an unconditional branch with no explicit dest, it must just be
253 // a fallthrough into DestB.
254 if (DestA == 0 && FallThru != getParent()->end()) {
255 DestA = FallThru;
256 AddedFallThrough = true;
257 }
258 }
259
260 MachineBasicBlock::succ_iterator SI = succ_begin();
261 MachineBasicBlock *OrigDestA = DestA, *OrigDestB = DestB;
262 while (SI != succ_end()) {
263 if (*SI == DestA && DestA == DestB) {
264 DestA = DestB = 0;
265 ++SI;
266 } else if (*SI == DestA) {
267 DestA = 0;
268 ++SI;
269 } else if (*SI == DestB) {
270 DestB = 0;
271 ++SI;
272 } else if ((*SI)->isLandingPad() &&
273 *SI!=OrigDestA && *SI!=OrigDestB) {
274 ++SI;
275 } else {
276 // Otherwise, this is a superfluous edge, remove it.
David Greene8a46d342007-06-29 02:45:24 +0000277 SI = removeSuccessor(SI);
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000278 MadeChange = true;
279 }
280 }
281 if (!AddedFallThrough) {
282 assert(DestA == 0 && DestB == 0 &&
283 "MachineCFG is missing edges!");
284 } else if (isCond) {
285 assert(DestA == 0 && "MachineCFG is missing edges!");
286 }
287 return MadeChange;
288}