1 //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collect the sequence of machine instructions for a basic block.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/MachineBasicBlock.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/Target/TargetRegisterInfo.h"
18 #include "llvm/Target/TargetData.h"
19 #include "llvm/Target/TargetInstrDesc.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Support/LeakDetector.h"
22 #include "llvm/Support/raw_ostream.h"
26 MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb)
27 : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false),
32 MachineBasicBlock::~MachineBasicBlock() {
33 LeakDetector::removeGarbageObject(this);
36 raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) {
41 /// 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.
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.
48 void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) {
49 MachineFunction &MF = *N->getParent();
50 N->Number = MF.addToMBBNumbering(N);
52 // Make sure the instructions have their operands in the reginfo lists.
53 MachineRegisterInfo &RegInfo = MF.getRegInfo();
54 for (MachineBasicBlock::iterator I = N->begin(), E = N->end(); I != E; ++I)
55 I->AddRegOperandsToUseLists(RegInfo);
57 LeakDetector::removeGarbageObject(N);
60 void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) {
61 N->getParent()->removeFromMBBNumbering(N->Number);
63 LeakDetector::addGarbageObject(N);
67 /// 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.
70 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) {
71 assert(N->getParent() == 0 && "machine instruction already in a basic block");
74 // Add the instruction's register operands to their corresponding
76 MachineFunction *MF = Parent->getParent();
77 N->AddRegOperandsToUseLists(MF->getRegInfo());
79 LeakDetector::removeGarbageObject(N);
82 /// 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.
85 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) {
86 assert(N->getParent() != 0 && "machine instruction not in a basic block");
88 // Remove from the use/def lists.
89 N->RemoveRegOperandsFromUseLists();
93 LeakDetector::addGarbageObject(N);
96 /// 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
99 void ilist_traits<MachineInstr>::
100 transferNodesFromList(ilist_traits<MachineInstr> &fromList,
101 MachineBasicBlock::iterator first,
102 MachineBasicBlock::iterator last) {
103 assert(Parent->getParent() == fromList.Parent->getParent() &&
104 "MachineInstr parent mismatch!");
106 // Splice within the same MBB -> no change.
107 if (Parent == fromList.Parent) return;
109 // If splicing between two blocks within the same function, just update the
111 for (; first != last; ++first)
112 first->setParent(Parent);
115 void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) {
116 assert(!MI->getParent() && "MI is still in a block!");
117 Parent->getParent()->DeleteMachineInstr(MI);
120 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
122 while (I != begin() && (--I)->getDesc().isTerminator())
124 if (I != end() && !I->getDesc().isTerminator()) ++I;
128 /// 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.
131 bool 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())
137 // If there isn't exactly one predecessor, it can't be a fall through.
138 const_pred_iterator PI = pred_begin(), PI2 = PI;
140 if (PI2 != pred_end())
143 // The predecessor has to be immediately before this block.
144 const MachineBasicBlock *Pred = *PI;
146 if (!Pred->isLayoutSuccessor(this))
149 // If the block is completely empty, then it definitely does fall through.
153 // Otherwise, check the last instruction.
154 const MachineInstr &LastInst = Pred->back();
155 return !LastInst.getDesc().isBarrier();
158 void MachineBasicBlock::dump() const {
162 static inline void OutputReg(raw_ostream &os, unsigned RegNo,
163 const TargetRegisterInfo *TRI = 0) {
164 if (!RegNo || TargetRegisterInfo::isPhysicalRegister(RegNo)) {
166 os << " %" << TRI->get(RegNo).Name;
168 os << " %mreg(" << RegNo << ")";
170 os << " %reg" << RegNo;
173 void MachineBasicBlock::print(raw_ostream &OS) const {
174 const MachineFunction *MF = getParent();
176 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
181 const BasicBlock *LBB = getBasicBlock();
183 if (LBB) OS << LBB->getName() << ": ";
184 OS << (const void*)this
185 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber();
186 if (Alignment) OS << ", Alignment " << Alignment;
187 if (isLandingPad()) OS << ", EH LANDING PAD";
190 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
191 if (!livein_empty()) {
193 for (const_livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I)
194 OutputReg(OS, *I, TRI);
197 // Print the preds of this block according to the CFG.
199 OS << " Predecessors according to CFG:";
200 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
201 OS << ' ' << *PI << " (#" << (*PI)->getNumber() << ')';
205 for (const_iterator I = begin(); I != end(); ++I) {
207 I->print(OS, &getParent()->getTarget());
210 // Print the successors of this block according to the CFG.
212 OS << " Successors according to CFG:";
213 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI)
214 OS << ' ' << *SI << " (#" << (*SI)->getNumber() << ')';
219 void MachineBasicBlock::removeLiveIn(unsigned Reg) {
220 livein_iterator I = std::find(livein_begin(), livein_end(), Reg);
221 assert(I != livein_end() && "Not a live in!");
225 bool MachineBasicBlock::isLiveIn(unsigned Reg) const {
226 const_livein_iterator I = std::find(livein_begin(), livein_end(), Reg);
227 return I != livein_end();
230 void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
231 getParent()->splice(NewAfter, this);
234 void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
235 MachineFunction::iterator BBI = NewBefore;
236 getParent()->splice(++BBI, this);
240 void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
241 Successors.push_back(succ);
242 succ->addPredecessor(this);
245 void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
246 succ->removePredecessor(this);
247 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
248 assert(I != Successors.end() && "Not a current successor!");
252 MachineBasicBlock::succ_iterator
253 MachineBasicBlock::removeSuccessor(succ_iterator I) {
254 assert(I != Successors.end() && "Not a current successor!");
255 (*I)->removePredecessor(this);
256 return Successors.erase(I);
259 void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
260 Predecessors.push_back(pred);
263 void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
264 std::vector<MachineBasicBlock *>::iterator I =
265 std::find(Predecessors.begin(), Predecessors.end(), pred);
266 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
267 Predecessors.erase(I);
270 void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) {
274 for (MachineBasicBlock::succ_iterator I = fromMBB->succ_begin(),
275 E = fromMBB->succ_end(); I != E; ++I)
278 while (!fromMBB->succ_empty())
279 fromMBB->removeSuccessor(fromMBB->succ_begin());
282 bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const {
283 std::vector<MachineBasicBlock *>::const_iterator I =
284 std::find(Successors.begin(), Successors.end(), MBB);
285 return I != Successors.end();
288 bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const {
289 MachineFunction::const_iterator I(this);
290 return next(I) == MachineFunction::const_iterator(MBB);
293 /// removeFromParent - This method unlinks 'this' from the containing function,
294 /// and returns it, but does not delete it.
295 MachineBasicBlock *MachineBasicBlock::removeFromParent() {
296 assert(getParent() && "Not embedded in a function!");
297 getParent()->remove(this);
302 /// eraseFromParent - This method unlinks 'this' from the containing function,
304 void MachineBasicBlock::eraseFromParent() {
305 assert(getParent() && "Not embedded in a function!");
306 getParent()->erase(this);
310 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
311 /// 'Old', change the code and CFG so that it branches to 'New' instead.
312 void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old,
313 MachineBasicBlock *New) {
314 assert(Old != New && "Cannot replace self with self!");
316 MachineBasicBlock::iterator I = end();
317 while (I != begin()) {
319 if (!I->getDesc().isTerminator()) break;
321 // Scan the operands of this machine instruction, replacing any uses of Old
323 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
324 if (I->getOperand(i).isMBB() &&
325 I->getOperand(i).getMBB() == Old)
326 I->getOperand(i).setMBB(New);
329 // Update the successor information.
330 removeSuccessor(Old);
334 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the
335 /// CFG to be inserted. If we have proven that MBB can only branch to DestA and
336 /// DestB, remove any other MBB successors from the CFG. DestA and DestB can
338 /// Besides DestA and DestB, retain other edges leading to LandingPads
339 /// (currently there can be only one; we don't check or require that here).
340 /// Note it is possible that DestA and/or DestB are LandingPads.
341 bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA,
342 MachineBasicBlock *DestB,
344 bool MadeChange = false;
345 bool AddedFallThrough = false;
347 MachineFunction::iterator FallThru = next(MachineFunction::iterator(this));
349 // If this block ends with a conditional branch that falls through to its
350 // successor, set DestB as the successor.
352 if (DestB == 0 && FallThru != getParent()->end()) {
354 AddedFallThrough = true;
357 // If this is an unconditional branch with no explicit dest, it must just be
358 // a fallthrough into DestB.
359 if (DestA == 0 && FallThru != getParent()->end()) {
361 AddedFallThrough = true;
365 MachineBasicBlock::succ_iterator SI = succ_begin();
366 MachineBasicBlock *OrigDestA = DestA, *OrigDestB = DestB;
367 while (SI != succ_end()) {
368 if (*SI == DestA && DestA == DestB) {
371 } else if (*SI == DestA) {
374 } else if (*SI == DestB) {
377 } else if ((*SI)->isLandingPad() &&
378 *SI!=OrigDestA && *SI!=OrigDestB) {
381 // Otherwise, this is a superfluous edge, remove it.
382 SI = removeSuccessor(SI);
386 if (!AddedFallThrough) {
387 assert(DestA == 0 && DestB == 0 &&
388 "MachineCFG is missing edges!");
390 assert(DestA == 0 && "MachineCFG is missing edges!");