blob: 3b84d688b874d6eb2ac51a077b89b2ca429c3b24 [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"
Evan Cheng13d82852007-02-10 02:38:19 +000017#include "llvm/Target/MRegisterInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000018#include "llvm/Target/TargetData.h"
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +000019#include "llvm/Target/TargetInstrInfo.h"
20#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/Support/LeakDetector.h"
Chris Lattner52c09d72004-10-26 15:43:42 +000022#include <algorithm>
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000023using namespace llvm;
24
Tanya Lattner17fb34b2004-05-24 07:14:35 +000025MachineBasicBlock::~MachineBasicBlock() {
26 LeakDetector::removeGarbageObject(this);
27}
Tanya Lattner17fb34b2004-05-24 07:14:35 +000028
Chris Lattner1ccc4682006-11-18 21:47:36 +000029std::ostream& llvm::operator<<(std::ostream &OS, const MachineBasicBlock &MBB) {
30 MBB.print(OS);
31 return OS;
32}
Tanya Lattner17fb34b2004-05-24 07:14:35 +000033
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000034// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000035// gets the next available unique MBB number. If it is removed from a
36// MachineFunction, it goes back to being #-1.
Chris Lattnerca48eb92004-07-01 06:02:27 +000037void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) {
Chris Lattnerf20c1a42007-12-31 04:56:33 +000038 assert(N->getParent() == 0 && "machine instruction already in a basic block");
39 N->setParent(Parent);
Chris Lattnerca48eb92004-07-01 06:02:27 +000040 N->Number = Parent->addToMBBNumbering(N);
Tanya Lattner792699c2004-05-24 06:11:51 +000041 LeakDetector::removeGarbageObject(N);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000042}
43
Chris Lattnerca48eb92004-07-01 06:02:27 +000044void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) {
Chris Lattnerf20c1a42007-12-31 04:56:33 +000045 assert(N->getParent() != 0 && "machine instruction not in a basic block");
46 N->getParent()->removeFromMBBNumbering(N->Number);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000047 N->Number = -1;
Chris Lattnerf20c1a42007-12-31 04:56:33 +000048 N->setParent(0);
Tanya Lattner792699c2004-05-24 06:11:51 +000049 LeakDetector::addGarbageObject(N);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000050}
51
Chris Lattner5e61fa92004-02-19 16:13:54 +000052
Chris Lattnerbca81442005-01-30 00:09:23 +000053MachineInstr* ilist_traits<MachineInstr>::createSentinel() {
Evan Chengc0f64ff2006-11-27 23:37:22 +000054 MachineInstr* dummy = new MachineInstr();
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000055 LeakDetector::removeGarbageObject(dummy);
56 return dummy;
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000057}
58
Chris Lattner52c09d72004-10-26 15:43:42 +000059void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) {
Chris Lattnerf20c1a42007-12-31 04:56:33 +000060 assert(N->getParent() == 0 && "machine instruction already in a basic block");
61 N->setParent(parent);
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000062 LeakDetector::removeGarbageObject(N);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000063}
64
Chris Lattner52c09d72004-10-26 15:43:42 +000065void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) {
Chris Lattnerf20c1a42007-12-31 04:56:33 +000066 assert(N->getParent() != 0 && "machine instruction not in a basic block");
67 N->setParent(0);
Alkis Evlogimenosa6382862004-09-05 18:39:20 +000068 LeakDetector::addGarbageObject(N);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000069}
70
71void ilist_traits<MachineInstr>::transferNodesFromList(
Chris Lattnerf20c1a42007-12-31 04:56:33 +000072 iplist<MachineInstr, ilist_traits<MachineInstr> >& fromList,
73 ilist_iterator<MachineInstr> first,
74 ilist_iterator<MachineInstr> last) {
75 // Splice within the same MBB -> no change.
76 if (parent == fromList.parent) return;
77
78 for (; first != last; ++first)
79 first->setParent(parent);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000080}
81
Chris Lattner52c09d72004-10-26 15:43:42 +000082MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
Chris Lattner9bcdcd12004-06-02 05:57:12 +000083 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo();
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +000084 iterator I = end();
Anton Korobeynikov406452d2007-09-02 22:11:14 +000085 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()))
86 ; /*noop */
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +000087 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
88 return I;
89}
90
Chris Lattner52c09d72004-10-26 15:43:42 +000091void MachineBasicBlock::dump() const {
Bill Wendlingbcd24982006-12-07 20:28:15 +000092 print(*cerr.stream());
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000093}
94
Evan Cheng13d82852007-02-10 02:38:19 +000095static inline void OutputReg(std::ostream &os, unsigned RegNo,
96 const MRegisterInfo *MRI = 0) {
97 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
98 if (MRI)
99 os << " %" << MRI->get(RegNo).Name;
100 else
101 os << " %mreg(" << RegNo << ")";
102 } else
103 os << " %reg" << RegNo;
104}
105
Chris Lattner52c09d72004-10-26 15:43:42 +0000106void MachineBasicBlock::print(std::ostream &OS) const {
Evan Cheng13d82852007-02-10 02:38:19 +0000107 const MachineFunction *MF = getParent();
108 if(!MF) {
Chris Lattner52c09d72004-10-26 15:43:42 +0000109 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
110 << " is null\n";
Tanya Lattner792699c2004-05-24 06:11:51 +0000111 return;
112 }
Alkis Evlogimenosa6382862004-09-05 18:39:20 +0000113
114 const BasicBlock *LBB = getBasicBlock();
Chris Lattnerdb3ea672006-10-06 21:28:17 +0000115 OS << "\n";
Chris Lattnerc11ce862007-04-30 23:12:53 +0000116 if (LBB) OS << LBB->getName() << ": ";
117 OS << (const void*)this
118 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber();
119 if (isLandingPad()) OS << ", EH LANDING PAD";
120 OS << ":\n";
Evan Cheng13d82852007-02-10 02:38:19 +0000121
122 const MRegisterInfo *MRI = MF->getTarget().getRegisterInfo();
Dan Gohmancb406c22007-10-03 19:26:29 +0000123 if (!livein_empty()) {
Evan Cheng13d82852007-02-10 02:38:19 +0000124 OS << "Live Ins:";
Evan Chengb371f452007-02-19 21:49:54 +0000125 for (const_livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I)
Evan Cheng13d82852007-02-10 02:38:19 +0000126 OutputReg(OS, *I, MRI);
127 OS << "\n";
128 }
Chris Lattner681764b2006-09-26 03:41:59 +0000129 // Print the preds of this block according to the CFG.
130 if (!pred_empty()) {
131 OS << " Predecessors according to CFG:";
132 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
Evan Cheng21970512007-03-09 08:29:08 +0000133 OS << " " << *PI << " (#" << (*PI)->getNumber() << ")";
Chris Lattner681764b2006-09-26 03:41:59 +0000134 OS << "\n";
135 }
136
Alkis Evlogimenosa6382862004-09-05 18:39:20 +0000137 for (const_iterator I = begin(); I != end(); ++I) {
138 OS << "\t";
139 I->print(OS, &getParent()->getTarget());
140 }
Chris Lattner380ae492005-04-01 06:48:38 +0000141
142 // Print the successors of this block according to the CFG.
143 if (!succ_empty()) {
144 OS << " Successors according to CFG:";
145 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI)
Evan Cheng21970512007-03-09 08:29:08 +0000146 OS << " " << *SI << " (#" << (*SI)->getNumber() << ")";
Chris Lattner380ae492005-04-01 06:48:38 +0000147 OS << "\n";
148 }
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000149}
Chris Lattner52c09d72004-10-26 15:43:42 +0000150
Evan Chengb371f452007-02-19 21:49:54 +0000151void MachineBasicBlock::removeLiveIn(unsigned Reg) {
152 livein_iterator I = std::find(livein_begin(), livein_end(), Reg);
153 assert(I != livein_end() && "Not a live in!");
154 LiveIns.erase(I);
155}
156
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000157void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
158 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
159 getParent()->getBasicBlockList().splice(NewAfter, BBList, this);
160}
161
162void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
163 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList();
164 MachineFunction::iterator BBI = NewBefore;
165 getParent()->getBasicBlockList().splice(++BBI, BBList, this);
166}
167
168
Chris Lattner52c09d72004-10-26 15:43:42 +0000169void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
170 Successors.push_back(succ);
171 succ->addPredecessor(this);
172}
173
174void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
175 succ->removePredecessor(this);
176 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
177 assert(I != Successors.end() && "Not a current successor!");
178 Successors.erase(I);
179}
180
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000181MachineBasicBlock::succ_iterator
182MachineBasicBlock::removeSuccessor(succ_iterator I) {
Chris Lattner52c09d72004-10-26 15:43:42 +0000183 assert(I != Successors.end() && "Not a current successor!");
184 (*I)->removePredecessor(this);
David Greene8a46d342007-06-29 02:45:24 +0000185 return(Successors.erase(I));
Chris Lattner52c09d72004-10-26 15:43:42 +0000186}
187
188void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
189 Predecessors.push_back(pred);
190}
191
192void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000193 std::vector<MachineBasicBlock *>::iterator I =
Chris Lattner52c09d72004-10-26 15:43:42 +0000194 std::find(Predecessors.begin(), Predecessors.end(), pred);
195 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
196 Predecessors.erase(I);
197}
Evan Cheng4f098782007-05-17 23:58:53 +0000198
199bool MachineBasicBlock::isSuccessor(MachineBasicBlock *MBB) const {
200 std::vector<MachineBasicBlock *>::const_iterator I =
201 std::find(Successors.begin(), Successors.end(), MBB);
202 return I != Successors.end();
203}
Evan Cheng0370fad2007-06-04 06:44:01 +0000204
205/// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
206/// 'Old', change the code and CFG so that it branches to 'New' instead.
207void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old,
208 MachineBasicBlock *New) {
209 assert(Old != New && "Cannot replace self with self!");
210
211 MachineBasicBlock::iterator I = end();
212 while (I != begin()) {
213 --I;
214 if (!(I->getInstrDescriptor()->Flags & M_TERMINATOR_FLAG)) break;
215
216 // Scan the operands of this machine instruction, replacing any uses of Old
217 // with New.
218 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
Chris Lattner8aa797a2007-12-30 23:10:15 +0000219 if (I->getOperand(i).isMBB() && I->getOperand(i).getMBB() == Old)
220 I->getOperand(i).setMBB(New);
Evan Cheng0370fad2007-06-04 06:44:01 +0000221 }
222
223 // Update the successor information. If New was already a successor, just
224 // remove the link to Old instead of creating another one. PR 1444.
225 removeSuccessor(Old);
226 if (!isSuccessor(New))
227 addSuccessor(New);
228}
229
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000230/// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the
231/// CFG to be inserted. If we have proven that MBB can only branch to DestA and
232/// DestB, remove any other MBB successors from the CFG. DestA and DestB can
233/// be null.
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000234/// Besides DestA and DestB, retain other edges leading to LandingPads
235/// (currently there can be only one; we don't check or require that here).
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000236/// Note it is possible that DestA and/or DestB are LandingPads.
237bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA,
238 MachineBasicBlock *DestB,
239 bool isCond) {
240 bool MadeChange = false;
241 bool AddedFallThrough = false;
242
243 MachineBasicBlock *FallThru = getNext();
244
245 // If this block ends with a conditional branch that falls through to its
246 // successor, set DestB as the successor.
247 if (isCond) {
248 if (DestB == 0 && FallThru != getParent()->end()) {
249 DestB = FallThru;
250 AddedFallThrough = true;
251 }
252 } else {
253 // If this is an unconditional branch with no explicit dest, it must just be
254 // a fallthrough into DestB.
255 if (DestA == 0 && FallThru != getParent()->end()) {
256 DestA = FallThru;
257 AddedFallThrough = true;
258 }
259 }
260
261 MachineBasicBlock::succ_iterator SI = succ_begin();
262 MachineBasicBlock *OrigDestA = DestA, *OrigDestB = DestB;
263 while (SI != succ_end()) {
264 if (*SI == DestA && DestA == DestB) {
265 DestA = DestB = 0;
266 ++SI;
267 } else if (*SI == DestA) {
268 DestA = 0;
269 ++SI;
270 } else if (*SI == DestB) {
271 DestB = 0;
272 ++SI;
273 } else if ((*SI)->isLandingPad() &&
274 *SI!=OrigDestA && *SI!=OrigDestB) {
275 ++SI;
276 } else {
277 // Otherwise, this is a superfluous edge, remove it.
David Greene8a46d342007-06-29 02:45:24 +0000278 SI = removeSuccessor(SI);
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000279 MadeChange = true;
280 }
281 }
282 if (!AddedFallThrough) {
283 assert(DestA == 0 && DestB == 0 &&
284 "MachineCFG is missing edges!");
285 } else if (isCond) {
286 assert(DestA == 0 && "MachineCFG is missing edges!");
287 }
288 return MadeChange;
289}