1f22ef01cSRoman Divacky //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky //                     The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
10f22ef01cSRoman Divacky // Collect the sequence of machine instructions for a basic block.
11f22ef01cSRoman Divacky //
12f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
13f22ef01cSRoman Divacky 
14f22ef01cSRoman Divacky #include "llvm/CodeGen/MachineBasicBlock.h"
15f22ef01cSRoman Divacky #include "llvm/BasicBlock.h"
16ffd1746dSEd Schouten #include "llvm/CodeGen/LiveVariables.h"
17ffd1746dSEd Schouten #include "llvm/CodeGen/MachineDominators.h"
18f22ef01cSRoman Divacky #include "llvm/CodeGen/MachineFunction.h"
19ffd1746dSEd Schouten #include "llvm/CodeGen/MachineLoopInfo.h"
202754fe60SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h"
21f22ef01cSRoman Divacky #include "llvm/MC/MCAsmInfo.h"
22f22ef01cSRoman Divacky #include "llvm/MC/MCContext.h"
23f22ef01cSRoman Divacky #include "llvm/Target/TargetRegisterInfo.h"
24f22ef01cSRoman Divacky #include "llvm/Target/TargetData.h"
25f22ef01cSRoman Divacky #include "llvm/Target/TargetInstrDesc.h"
26f22ef01cSRoman Divacky #include "llvm/Target/TargetInstrInfo.h"
27f22ef01cSRoman Divacky #include "llvm/Target/TargetMachine.h"
28f22ef01cSRoman Divacky #include "llvm/Assembly/Writer.h"
29f22ef01cSRoman Divacky #include "llvm/ADT/SmallString.h"
30f22ef01cSRoman Divacky #include "llvm/ADT/SmallPtrSet.h"
31f22ef01cSRoman Divacky #include "llvm/Support/Debug.h"
32f22ef01cSRoman Divacky #include "llvm/Support/LeakDetector.h"
33f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h"
34f22ef01cSRoman Divacky #include <algorithm>
35f22ef01cSRoman Divacky using namespace llvm;
36f22ef01cSRoman Divacky 
37f22ef01cSRoman Divacky MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb)
38f22ef01cSRoman Divacky   : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false),
39f22ef01cSRoman Divacky     AddressTaken(false) {
40f22ef01cSRoman Divacky   Insts.Parent = this;
41f22ef01cSRoman Divacky }
42f22ef01cSRoman Divacky 
43f22ef01cSRoman Divacky MachineBasicBlock::~MachineBasicBlock() {
44f22ef01cSRoman Divacky   LeakDetector::removeGarbageObject(this);
45f22ef01cSRoman Divacky }
46f22ef01cSRoman Divacky 
47f22ef01cSRoman Divacky /// getSymbol - Return the MCSymbol for this basic block.
48f22ef01cSRoman Divacky ///
49f22ef01cSRoman Divacky MCSymbol *MachineBasicBlock::getSymbol() const {
50f22ef01cSRoman Divacky   const MachineFunction *MF = getParent();
51f22ef01cSRoman Divacky   MCContext &Ctx = MF->getContext();
52f22ef01cSRoman Divacky   const char *Prefix = Ctx.getAsmInfo().getPrivateGlobalPrefix();
53f22ef01cSRoman Divacky   return Ctx.GetOrCreateSymbol(Twine(Prefix) + "BB" +
54f22ef01cSRoman Divacky                                Twine(MF->getFunctionNumber()) + "_" +
55f22ef01cSRoman Divacky                                Twine(getNumber()));
56f22ef01cSRoman Divacky }
57f22ef01cSRoman Divacky 
58f22ef01cSRoman Divacky 
59f22ef01cSRoman Divacky raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) {
60f22ef01cSRoman Divacky   MBB.print(OS);
61f22ef01cSRoman Divacky   return OS;
62f22ef01cSRoman Divacky }
63f22ef01cSRoman Divacky 
64f22ef01cSRoman Divacky /// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the
65f22ef01cSRoman Divacky /// parent pointer of the MBB, the MBB numbering, and any instructions in the
66f22ef01cSRoman Divacky /// MBB to be on the right operand list for registers.
67f22ef01cSRoman Divacky ///
68f22ef01cSRoman Divacky /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
69f22ef01cSRoman Divacky /// gets the next available unique MBB number. If it is removed from a
70f22ef01cSRoman Divacky /// MachineFunction, it goes back to being #-1.
71f22ef01cSRoman Divacky void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) {
72f22ef01cSRoman Divacky   MachineFunction &MF = *N->getParent();
73f22ef01cSRoman Divacky   N->Number = MF.addToMBBNumbering(N);
74f22ef01cSRoman Divacky 
75f22ef01cSRoman Divacky   // Make sure the instructions have their operands in the reginfo lists.
76f22ef01cSRoman Divacky   MachineRegisterInfo &RegInfo = MF.getRegInfo();
77f22ef01cSRoman Divacky   for (MachineBasicBlock::iterator I = N->begin(), E = N->end(); I != E; ++I)
78f22ef01cSRoman Divacky     I->AddRegOperandsToUseLists(RegInfo);
79f22ef01cSRoman Divacky 
80f22ef01cSRoman Divacky   LeakDetector::removeGarbageObject(N);
81f22ef01cSRoman Divacky }
82f22ef01cSRoman Divacky 
83f22ef01cSRoman Divacky void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) {
84f22ef01cSRoman Divacky   N->getParent()->removeFromMBBNumbering(N->Number);
85f22ef01cSRoman Divacky   N->Number = -1;
86f22ef01cSRoman Divacky   LeakDetector::addGarbageObject(N);
87f22ef01cSRoman Divacky }
88f22ef01cSRoman Divacky 
89f22ef01cSRoman Divacky 
90f22ef01cSRoman Divacky /// addNodeToList (MI) - When we add an instruction to a basic block
91f22ef01cSRoman Divacky /// list, we update its parent pointer and add its operands from reg use/def
92f22ef01cSRoman Divacky /// lists if appropriate.
93f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) {
94f22ef01cSRoman Divacky   assert(N->getParent() == 0 && "machine instruction already in a basic block");
95f22ef01cSRoman Divacky   N->setParent(Parent);
96f22ef01cSRoman Divacky 
97f22ef01cSRoman Divacky   // Add the instruction's register operands to their corresponding
98f22ef01cSRoman Divacky   // use/def lists.
99f22ef01cSRoman Divacky   MachineFunction *MF = Parent->getParent();
100f22ef01cSRoman Divacky   N->AddRegOperandsToUseLists(MF->getRegInfo());
101f22ef01cSRoman Divacky 
102f22ef01cSRoman Divacky   LeakDetector::removeGarbageObject(N);
103f22ef01cSRoman Divacky }
104f22ef01cSRoman Divacky 
105f22ef01cSRoman Divacky /// removeNodeFromList (MI) - When we remove an instruction from a basic block
106f22ef01cSRoman Divacky /// list, we update its parent pointer and remove its operands from reg use/def
107f22ef01cSRoman Divacky /// lists if appropriate.
108f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) {
109f22ef01cSRoman Divacky   assert(N->getParent() != 0 && "machine instruction not in a basic block");
110f22ef01cSRoman Divacky 
111f22ef01cSRoman Divacky   // Remove from the use/def lists.
112f22ef01cSRoman Divacky   N->RemoveRegOperandsFromUseLists();
113f22ef01cSRoman Divacky 
114f22ef01cSRoman Divacky   N->setParent(0);
115f22ef01cSRoman Divacky 
116f22ef01cSRoman Divacky   LeakDetector::addGarbageObject(N);
117f22ef01cSRoman Divacky }
118f22ef01cSRoman Divacky 
119f22ef01cSRoman Divacky /// transferNodesFromList (MI) - When moving a range of instructions from one
120f22ef01cSRoman Divacky /// MBB list to another, we need to update the parent pointers and the use/def
121f22ef01cSRoman Divacky /// lists.
122f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::
123f22ef01cSRoman Divacky transferNodesFromList(ilist_traits<MachineInstr> &fromList,
124f22ef01cSRoman Divacky                       MachineBasicBlock::iterator first,
125f22ef01cSRoman Divacky                       MachineBasicBlock::iterator last) {
126f22ef01cSRoman Divacky   assert(Parent->getParent() == fromList.Parent->getParent() &&
127f22ef01cSRoman Divacky         "MachineInstr parent mismatch!");
128f22ef01cSRoman Divacky 
129f22ef01cSRoman Divacky   // Splice within the same MBB -> no change.
130f22ef01cSRoman Divacky   if (Parent == fromList.Parent) return;
131f22ef01cSRoman Divacky 
132f22ef01cSRoman Divacky   // If splicing between two blocks within the same function, just update the
133f22ef01cSRoman Divacky   // parent pointers.
134f22ef01cSRoman Divacky   for (; first != last; ++first)
135f22ef01cSRoman Divacky     first->setParent(Parent);
136f22ef01cSRoman Divacky }
137f22ef01cSRoman Divacky 
138f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) {
139f22ef01cSRoman Divacky   assert(!MI->getParent() && "MI is still in a block!");
140f22ef01cSRoman Divacky   Parent->getParent()->DeleteMachineInstr(MI);
141f22ef01cSRoman Divacky }
142f22ef01cSRoman Divacky 
143ffd1746dSEd Schouten MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() {
144ffd1746dSEd Schouten   iterator I = begin();
145ffd1746dSEd Schouten   while (I != end() && I->isPHI())
146ffd1746dSEd Schouten     ++I;
147ffd1746dSEd Schouten   return I;
148ffd1746dSEd Schouten }
149ffd1746dSEd Schouten 
1502754fe60SDimitry Andric MachineBasicBlock::iterator
1512754fe60SDimitry Andric MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) {
1522754fe60SDimitry Andric   while (I != end() && (I->isPHI() || I->isLabel() || I->isDebugValue()))
1532754fe60SDimitry Andric     ++I;
1542754fe60SDimitry Andric   return I;
1552754fe60SDimitry Andric }
1562754fe60SDimitry Andric 
157f22ef01cSRoman Divacky MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
158f22ef01cSRoman Divacky   iterator I = end();
1592754fe60SDimitry Andric   while (I != begin() && ((--I)->getDesc().isTerminator() || I->isDebugValue()))
160f22ef01cSRoman Divacky     ; /*noop */
1612754fe60SDimitry Andric   while (I != end() && !I->getDesc().isTerminator())
1622754fe60SDimitry Andric     ++I;
163f22ef01cSRoman Divacky   return I;
164f22ef01cSRoman Divacky }
165f22ef01cSRoman Divacky 
1662754fe60SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() {
1672754fe60SDimitry Andric   iterator B = begin(), I = end();
1682754fe60SDimitry Andric   while (I != B) {
1692754fe60SDimitry Andric     --I;
1702754fe60SDimitry Andric     if (I->isDebugValue())
1712754fe60SDimitry Andric       continue;
1722754fe60SDimitry Andric     return I;
1732754fe60SDimitry Andric   }
1742754fe60SDimitry Andric   // The block is all debug values.
1752754fe60SDimitry Andric   return end();
1762754fe60SDimitry Andric }
1772754fe60SDimitry Andric 
1782754fe60SDimitry Andric const MachineBasicBlock *MachineBasicBlock::getLandingPadSuccessor() const {
1792754fe60SDimitry Andric   // A block with a landing pad successor only has one other successor.
1802754fe60SDimitry Andric   if (succ_size() > 2)
1812754fe60SDimitry Andric     return 0;
1822754fe60SDimitry Andric   for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I)
1832754fe60SDimitry Andric     if ((*I)->isLandingPad())
1842754fe60SDimitry Andric       return *I;
1852754fe60SDimitry Andric   return 0;
1862754fe60SDimitry Andric }
1872754fe60SDimitry Andric 
188f22ef01cSRoman Divacky void MachineBasicBlock::dump() const {
189f22ef01cSRoman Divacky   print(dbgs());
190f22ef01cSRoman Divacky }
191f22ef01cSRoman Divacky 
192f22ef01cSRoman Divacky StringRef MachineBasicBlock::getName() const {
193f22ef01cSRoman Divacky   if (const BasicBlock *LBB = getBasicBlock())
194f22ef01cSRoman Divacky     return LBB->getName();
195f22ef01cSRoman Divacky   else
196f22ef01cSRoman Divacky     return "(null)";
197f22ef01cSRoman Divacky }
198f22ef01cSRoman Divacky 
1992754fe60SDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, SlotIndexes *Indexes) const {
200f22ef01cSRoman Divacky   const MachineFunction *MF = getParent();
201f22ef01cSRoman Divacky   if (!MF) {
202f22ef01cSRoman Divacky     OS << "Can't print out MachineBasicBlock because parent MachineFunction"
203f22ef01cSRoman Divacky        << " is null\n";
204f22ef01cSRoman Divacky     return;
205f22ef01cSRoman Divacky   }
206f22ef01cSRoman Divacky 
207f22ef01cSRoman Divacky   if (Alignment) { OS << "Alignment " << Alignment << "\n"; }
208f22ef01cSRoman Divacky 
2092754fe60SDimitry Andric   if (Indexes)
2102754fe60SDimitry Andric     OS << Indexes->getMBBStartIdx(this) << '\t';
2112754fe60SDimitry Andric 
212f22ef01cSRoman Divacky   OS << "BB#" << getNumber() << ": ";
213f22ef01cSRoman Divacky 
214f22ef01cSRoman Divacky   const char *Comma = "";
215f22ef01cSRoman Divacky   if (const BasicBlock *LBB = getBasicBlock()) {
216f22ef01cSRoman Divacky     OS << Comma << "derived from LLVM BB ";
217f22ef01cSRoman Divacky     WriteAsOperand(OS, LBB, /*PrintType=*/false);
218f22ef01cSRoman Divacky     Comma = ", ";
219f22ef01cSRoman Divacky   }
220f22ef01cSRoman Divacky   if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; }
221f22ef01cSRoman Divacky   if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; }
222f22ef01cSRoman Divacky   OS << '\n';
223f22ef01cSRoman Divacky 
224f22ef01cSRoman Divacky   const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
225f22ef01cSRoman Divacky   if (!livein_empty()) {
2262754fe60SDimitry Andric     if (Indexes) OS << '\t';
227f22ef01cSRoman Divacky     OS << "    Live Ins:";
228f22ef01cSRoman Divacky     for (livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I)
2292754fe60SDimitry Andric       OS << ' ' << PrintReg(*I, TRI);
230f22ef01cSRoman Divacky     OS << '\n';
231f22ef01cSRoman Divacky   }
232f22ef01cSRoman Divacky   // Print the preds of this block according to the CFG.
233f22ef01cSRoman Divacky   if (!pred_empty()) {
2342754fe60SDimitry Andric     if (Indexes) OS << '\t';
235f22ef01cSRoman Divacky     OS << "    Predecessors according to CFG:";
236f22ef01cSRoman Divacky     for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
237f22ef01cSRoman Divacky       OS << " BB#" << (*PI)->getNumber();
238f22ef01cSRoman Divacky     OS << '\n';
239f22ef01cSRoman Divacky   }
240f22ef01cSRoman Divacky 
241f22ef01cSRoman Divacky   for (const_iterator I = begin(); I != end(); ++I) {
2422754fe60SDimitry Andric     if (Indexes) {
2432754fe60SDimitry Andric       if (Indexes->hasIndex(I))
2442754fe60SDimitry Andric         OS << Indexes->getInstructionIndex(I);
2452754fe60SDimitry Andric       OS << '\t';
2462754fe60SDimitry Andric     }
247f22ef01cSRoman Divacky     OS << '\t';
248f22ef01cSRoman Divacky     I->print(OS, &getParent()->getTarget());
249f22ef01cSRoman Divacky   }
250f22ef01cSRoman Divacky 
251f22ef01cSRoman Divacky   // Print the successors of this block according to the CFG.
252f22ef01cSRoman Divacky   if (!succ_empty()) {
2532754fe60SDimitry Andric     if (Indexes) OS << '\t';
254f22ef01cSRoman Divacky     OS << "    Successors according to CFG:";
255f22ef01cSRoman Divacky     for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI)
256f22ef01cSRoman Divacky       OS << " BB#" << (*SI)->getNumber();
257f22ef01cSRoman Divacky     OS << '\n';
258f22ef01cSRoman Divacky   }
259f22ef01cSRoman Divacky }
260f22ef01cSRoman Divacky 
261f22ef01cSRoman Divacky void MachineBasicBlock::removeLiveIn(unsigned Reg) {
262f22ef01cSRoman Divacky   std::vector<unsigned>::iterator I =
263f22ef01cSRoman Divacky     std::find(LiveIns.begin(), LiveIns.end(), Reg);
264f22ef01cSRoman Divacky   assert(I != LiveIns.end() && "Not a live in!");
265f22ef01cSRoman Divacky   LiveIns.erase(I);
266f22ef01cSRoman Divacky }
267f22ef01cSRoman Divacky 
268f22ef01cSRoman Divacky bool MachineBasicBlock::isLiveIn(unsigned Reg) const {
269f22ef01cSRoman Divacky   livein_iterator I = std::find(livein_begin(), livein_end(), Reg);
270f22ef01cSRoman Divacky   return I != livein_end();
271f22ef01cSRoman Divacky }
272f22ef01cSRoman Divacky 
273f22ef01cSRoman Divacky void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
274f22ef01cSRoman Divacky   getParent()->splice(NewAfter, this);
275f22ef01cSRoman Divacky }
276f22ef01cSRoman Divacky 
277f22ef01cSRoman Divacky void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
278f22ef01cSRoman Divacky   MachineFunction::iterator BBI = NewBefore;
279f22ef01cSRoman Divacky   getParent()->splice(++BBI, this);
280f22ef01cSRoman Divacky }
281f22ef01cSRoman Divacky 
282f22ef01cSRoman Divacky void MachineBasicBlock::updateTerminator() {
283f22ef01cSRoman Divacky   const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo();
284f22ef01cSRoman Divacky   // A block with no successors has no concerns with fall-through edges.
285f22ef01cSRoman Divacky   if (this->succ_empty()) return;
286f22ef01cSRoman Divacky 
287f22ef01cSRoman Divacky   MachineBasicBlock *TBB = 0, *FBB = 0;
288f22ef01cSRoman Divacky   SmallVector<MachineOperand, 4> Cond;
289ffd1746dSEd Schouten   DebugLoc dl;  // FIXME: this is nowhere
290f22ef01cSRoman Divacky   bool B = TII->AnalyzeBranch(*this, TBB, FBB, Cond);
291f22ef01cSRoman Divacky   (void) B;
292f22ef01cSRoman Divacky   assert(!B && "UpdateTerminators requires analyzable predecessors!");
293f22ef01cSRoman Divacky   if (Cond.empty()) {
294f22ef01cSRoman Divacky     if (TBB) {
295f22ef01cSRoman Divacky       // The block has an unconditional branch. If its successor is now
296f22ef01cSRoman Divacky       // its layout successor, delete the branch.
297f22ef01cSRoman Divacky       if (isLayoutSuccessor(TBB))
298f22ef01cSRoman Divacky         TII->RemoveBranch(*this);
299f22ef01cSRoman Divacky     } else {
300f22ef01cSRoman Divacky       // The block has an unconditional fallthrough. If its successor is not
301f22ef01cSRoman Divacky       // its layout successor, insert a branch.
302f22ef01cSRoman Divacky       TBB = *succ_begin();
303f22ef01cSRoman Divacky       if (!isLayoutSuccessor(TBB))
304ffd1746dSEd Schouten         TII->InsertBranch(*this, TBB, 0, Cond, dl);
305f22ef01cSRoman Divacky     }
306f22ef01cSRoman Divacky   } else {
307f22ef01cSRoman Divacky     if (FBB) {
308f22ef01cSRoman Divacky       // The block has a non-fallthrough conditional branch. If one of its
309f22ef01cSRoman Divacky       // successors is its layout successor, rewrite it to a fallthrough
310f22ef01cSRoman Divacky       // conditional branch.
311f22ef01cSRoman Divacky       if (isLayoutSuccessor(TBB)) {
312f22ef01cSRoman Divacky         if (TII->ReverseBranchCondition(Cond))
313f22ef01cSRoman Divacky           return;
314f22ef01cSRoman Divacky         TII->RemoveBranch(*this);
315ffd1746dSEd Schouten         TII->InsertBranch(*this, FBB, 0, Cond, dl);
316f22ef01cSRoman Divacky       } else if (isLayoutSuccessor(FBB)) {
317f22ef01cSRoman Divacky         TII->RemoveBranch(*this);
318ffd1746dSEd Schouten         TII->InsertBranch(*this, TBB, 0, Cond, dl);
319f22ef01cSRoman Divacky       }
320f22ef01cSRoman Divacky     } else {
321f22ef01cSRoman Divacky       // The block has a fallthrough conditional branch.
322f22ef01cSRoman Divacky       MachineBasicBlock *MBBA = *succ_begin();
323f22ef01cSRoman Divacky       MachineBasicBlock *MBBB = *llvm::next(succ_begin());
324f22ef01cSRoman Divacky       if (MBBA == TBB) std::swap(MBBB, MBBA);
325f22ef01cSRoman Divacky       if (isLayoutSuccessor(TBB)) {
326f22ef01cSRoman Divacky         if (TII->ReverseBranchCondition(Cond)) {
327f22ef01cSRoman Divacky           // We can't reverse the condition, add an unconditional branch.
328f22ef01cSRoman Divacky           Cond.clear();
329ffd1746dSEd Schouten           TII->InsertBranch(*this, MBBA, 0, Cond, dl);
330f22ef01cSRoman Divacky           return;
331f22ef01cSRoman Divacky         }
332f22ef01cSRoman Divacky         TII->RemoveBranch(*this);
333ffd1746dSEd Schouten         TII->InsertBranch(*this, MBBA, 0, Cond, dl);
334f22ef01cSRoman Divacky       } else if (!isLayoutSuccessor(MBBA)) {
335f22ef01cSRoman Divacky         TII->RemoveBranch(*this);
336ffd1746dSEd Schouten         TII->InsertBranch(*this, TBB, MBBA, Cond, dl);
337f22ef01cSRoman Divacky       }
338f22ef01cSRoman Divacky     }
339f22ef01cSRoman Divacky   }
340f22ef01cSRoman Divacky }
341f22ef01cSRoman Divacky 
342f22ef01cSRoman Divacky void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) {
343f22ef01cSRoman Divacky   Successors.push_back(succ);
344f22ef01cSRoman Divacky   succ->addPredecessor(this);
345f22ef01cSRoman Divacky }
346f22ef01cSRoman Divacky 
347f22ef01cSRoman Divacky void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
348f22ef01cSRoman Divacky   succ->removePredecessor(this);
349f22ef01cSRoman Divacky   succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
350f22ef01cSRoman Divacky   assert(I != Successors.end() && "Not a current successor!");
351f22ef01cSRoman Divacky   Successors.erase(I);
352f22ef01cSRoman Divacky }
353f22ef01cSRoman Divacky 
354f22ef01cSRoman Divacky MachineBasicBlock::succ_iterator
355f22ef01cSRoman Divacky MachineBasicBlock::removeSuccessor(succ_iterator I) {
356f22ef01cSRoman Divacky   assert(I != Successors.end() && "Not a current successor!");
357f22ef01cSRoman Divacky   (*I)->removePredecessor(this);
358f22ef01cSRoman Divacky   return Successors.erase(I);
359f22ef01cSRoman Divacky }
360f22ef01cSRoman Divacky 
361f22ef01cSRoman Divacky void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
362f22ef01cSRoman Divacky   Predecessors.push_back(pred);
363f22ef01cSRoman Divacky }
364f22ef01cSRoman Divacky 
365f22ef01cSRoman Divacky void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
366f22ef01cSRoman Divacky   std::vector<MachineBasicBlock *>::iterator I =
367f22ef01cSRoman Divacky     std::find(Predecessors.begin(), Predecessors.end(), pred);
368f22ef01cSRoman Divacky   assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
369f22ef01cSRoman Divacky   Predecessors.erase(I);
370f22ef01cSRoman Divacky }
371f22ef01cSRoman Divacky 
372f22ef01cSRoman Divacky void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) {
373f22ef01cSRoman Divacky   if (this == fromMBB)
374f22ef01cSRoman Divacky     return;
375f22ef01cSRoman Divacky 
376ffd1746dSEd Schouten   while (!fromMBB->succ_empty()) {
377ffd1746dSEd Schouten     MachineBasicBlock *Succ = *fromMBB->succ_begin();
378ffd1746dSEd Schouten     addSuccessor(Succ);
379ffd1746dSEd Schouten     fromMBB->removeSuccessor(Succ);
380ffd1746dSEd Schouten   }
381ffd1746dSEd Schouten }
382f22ef01cSRoman Divacky 
383ffd1746dSEd Schouten void
384ffd1746dSEd Schouten MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB) {
385ffd1746dSEd Schouten   if (this == fromMBB)
386ffd1746dSEd Schouten     return;
387ffd1746dSEd Schouten 
388ffd1746dSEd Schouten   while (!fromMBB->succ_empty()) {
389ffd1746dSEd Schouten     MachineBasicBlock *Succ = *fromMBB->succ_begin();
390ffd1746dSEd Schouten     addSuccessor(Succ);
391ffd1746dSEd Schouten     fromMBB->removeSuccessor(Succ);
392ffd1746dSEd Schouten 
393ffd1746dSEd Schouten     // Fix up any PHI nodes in the successor.
394ffd1746dSEd Schouten     for (MachineBasicBlock::iterator MI = Succ->begin(), ME = Succ->end();
395ffd1746dSEd Schouten          MI != ME && MI->isPHI(); ++MI)
396ffd1746dSEd Schouten       for (unsigned i = 2, e = MI->getNumOperands()+1; i != e; i += 2) {
397ffd1746dSEd Schouten         MachineOperand &MO = MI->getOperand(i);
398ffd1746dSEd Schouten         if (MO.getMBB() == fromMBB)
399ffd1746dSEd Schouten           MO.setMBB(this);
400ffd1746dSEd Schouten       }
401ffd1746dSEd Schouten   }
402f22ef01cSRoman Divacky }
403f22ef01cSRoman Divacky 
404f22ef01cSRoman Divacky bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const {
405f22ef01cSRoman Divacky   std::vector<MachineBasicBlock *>::const_iterator I =
406f22ef01cSRoman Divacky     std::find(Successors.begin(), Successors.end(), MBB);
407f22ef01cSRoman Divacky   return I != Successors.end();
408f22ef01cSRoman Divacky }
409f22ef01cSRoman Divacky 
410f22ef01cSRoman Divacky bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const {
411f22ef01cSRoman Divacky   MachineFunction::const_iterator I(this);
412f22ef01cSRoman Divacky   return llvm::next(I) == MachineFunction::const_iterator(MBB);
413f22ef01cSRoman Divacky }
414f22ef01cSRoman Divacky 
415f22ef01cSRoman Divacky bool MachineBasicBlock::canFallThrough() {
416f22ef01cSRoman Divacky   MachineFunction::iterator Fallthrough = this;
417f22ef01cSRoman Divacky   ++Fallthrough;
418f22ef01cSRoman Divacky   // If FallthroughBlock is off the end of the function, it can't fall through.
419f22ef01cSRoman Divacky   if (Fallthrough == getParent()->end())
420f22ef01cSRoman Divacky     return false;
421f22ef01cSRoman Divacky 
422f22ef01cSRoman Divacky   // If FallthroughBlock isn't a successor, no fallthrough is possible.
423f22ef01cSRoman Divacky   if (!isSuccessor(Fallthrough))
424f22ef01cSRoman Divacky     return false;
425f22ef01cSRoman Divacky 
426f22ef01cSRoman Divacky   // Analyze the branches, if any, at the end of the block.
427f22ef01cSRoman Divacky   MachineBasicBlock *TBB = 0, *FBB = 0;
428f22ef01cSRoman Divacky   SmallVector<MachineOperand, 4> Cond;
429f22ef01cSRoman Divacky   const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo();
430f22ef01cSRoman Divacky   if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) {
431f22ef01cSRoman Divacky     // If we couldn't analyze the branch, examine the last instruction.
432f22ef01cSRoman Divacky     // If the block doesn't end in a known control barrier, assume fallthrough
433f22ef01cSRoman Divacky     // is possible. The isPredicable check is needed because this code can be
434f22ef01cSRoman Divacky     // called during IfConversion, where an instruction which is normally a
435f22ef01cSRoman Divacky     // Barrier is predicated and thus no longer an actual control barrier. This
436f22ef01cSRoman Divacky     // is over-conservative though, because if an instruction isn't actually
437f22ef01cSRoman Divacky     // predicated we could still treat it like a barrier.
438f22ef01cSRoman Divacky     return empty() || !back().getDesc().isBarrier() ||
439f22ef01cSRoman Divacky            back().getDesc().isPredicable();
440f22ef01cSRoman Divacky   }
441f22ef01cSRoman Divacky 
442f22ef01cSRoman Divacky   // If there is no branch, control always falls through.
443f22ef01cSRoman Divacky   if (TBB == 0) return true;
444f22ef01cSRoman Divacky 
445f22ef01cSRoman Divacky   // If there is some explicit branch to the fallthrough block, it can obviously
446f22ef01cSRoman Divacky   // reach, even though the branch should get folded to fall through implicitly.
447f22ef01cSRoman Divacky   if (MachineFunction::iterator(TBB) == Fallthrough ||
448f22ef01cSRoman Divacky       MachineFunction::iterator(FBB) == Fallthrough)
449f22ef01cSRoman Divacky     return true;
450f22ef01cSRoman Divacky 
451f22ef01cSRoman Divacky   // If it's an unconditional branch to some block not the fall through, it
452f22ef01cSRoman Divacky   // doesn't fall through.
453f22ef01cSRoman Divacky   if (Cond.empty()) return false;
454f22ef01cSRoman Divacky 
455f22ef01cSRoman Divacky   // Otherwise, if it is conditional and has no explicit false block, it falls
456f22ef01cSRoman Divacky   // through.
457f22ef01cSRoman Divacky   return FBB == 0;
458f22ef01cSRoman Divacky }
459f22ef01cSRoman Divacky 
460ffd1746dSEd Schouten MachineBasicBlock *
461ffd1746dSEd Schouten MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P) {
462ffd1746dSEd Schouten   MachineFunction *MF = getParent();
463ffd1746dSEd Schouten   DebugLoc dl;  // FIXME: this is nowhere
464ffd1746dSEd Schouten 
4652754fe60SDimitry Andric   // We may need to update this's terminator, but we can't do that if
4662754fe60SDimitry Andric   // AnalyzeBranch fails. If this uses a jump table, we won't touch it.
467ffd1746dSEd Schouten   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
468ffd1746dSEd Schouten   MachineBasicBlock *TBB = 0, *FBB = 0;
469ffd1746dSEd Schouten   SmallVector<MachineOperand, 4> Cond;
470ffd1746dSEd Schouten   if (TII->AnalyzeBranch(*this, TBB, FBB, Cond))
471ffd1746dSEd Schouten     return NULL;
472ffd1746dSEd Schouten 
4732754fe60SDimitry Andric   // Avoid bugpoint weirdness: A block may end with a conditional branch but
4742754fe60SDimitry Andric   // jumps to the same MBB is either case. We have duplicate CFG edges in that
4752754fe60SDimitry Andric   // case that we can't handle. Since this never happens in properly optimized
4762754fe60SDimitry Andric   // code, just skip those edges.
4772754fe60SDimitry Andric   if (TBB && TBB == FBB) {
4782754fe60SDimitry Andric     DEBUG(dbgs() << "Won't split critical edge after degenerate BB#"
4792754fe60SDimitry Andric                  << getNumber() << '\n');
4802754fe60SDimitry Andric     return NULL;
4812754fe60SDimitry Andric   }
4822754fe60SDimitry Andric 
483ffd1746dSEd Schouten   MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock();
484ffd1746dSEd Schouten   MF->insert(llvm::next(MachineFunction::iterator(this)), NMBB);
485e580952dSDimitry Andric   DEBUG(dbgs() << "Splitting critical edge:"
486ffd1746dSEd Schouten         " BB#" << getNumber()
487ffd1746dSEd Schouten         << " -- BB#" << NMBB->getNumber()
488ffd1746dSEd Schouten         << " -- BB#" << Succ->getNumber() << '\n');
489ffd1746dSEd Schouten 
490ffd1746dSEd Schouten   ReplaceUsesOfBlockWith(Succ, NMBB);
491ffd1746dSEd Schouten   updateTerminator();
492ffd1746dSEd Schouten 
493ffd1746dSEd Schouten   // Insert unconditional "jump Succ" instruction in NMBB if necessary.
494ffd1746dSEd Schouten   NMBB->addSuccessor(Succ);
495ffd1746dSEd Schouten   if (!NMBB->isLayoutSuccessor(Succ)) {
496ffd1746dSEd Schouten     Cond.clear();
497ffd1746dSEd Schouten     MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, Succ, NULL, Cond, dl);
498ffd1746dSEd Schouten   }
499ffd1746dSEd Schouten 
500ffd1746dSEd Schouten   // Fix PHI nodes in Succ so they refer to NMBB instead of this
501ffd1746dSEd Schouten   for (MachineBasicBlock::iterator i = Succ->begin(), e = Succ->end();
502ffd1746dSEd Schouten        i != e && i->isPHI(); ++i)
503ffd1746dSEd Schouten     for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2)
504ffd1746dSEd Schouten       if (i->getOperand(ni+1).getMBB() == this)
505ffd1746dSEd Schouten         i->getOperand(ni+1).setMBB(NMBB);
506ffd1746dSEd Schouten 
507ffd1746dSEd Schouten   if (LiveVariables *LV =
508ffd1746dSEd Schouten         P->getAnalysisIfAvailable<LiveVariables>())
509ffd1746dSEd Schouten     LV->addNewBlock(NMBB, this, Succ);
510ffd1746dSEd Schouten 
511ffd1746dSEd Schouten   if (MachineDominatorTree *MDT =
512e580952dSDimitry Andric       P->getAnalysisIfAvailable<MachineDominatorTree>()) {
513e580952dSDimitry Andric     // Update dominator information.
514e580952dSDimitry Andric     MachineDomTreeNode *SucccDTNode = MDT->getNode(Succ);
515ffd1746dSEd Schouten 
516e580952dSDimitry Andric     bool IsNewIDom = true;
517e580952dSDimitry Andric     for (const_pred_iterator PI = Succ->pred_begin(), E = Succ->pred_end();
518e580952dSDimitry Andric          PI != E; ++PI) {
519e580952dSDimitry Andric       MachineBasicBlock *PredBB = *PI;
520e580952dSDimitry Andric       if (PredBB == NMBB)
521e580952dSDimitry Andric         continue;
522e580952dSDimitry Andric       if (!MDT->dominates(SucccDTNode, MDT->getNode(PredBB))) {
523e580952dSDimitry Andric         IsNewIDom = false;
524e580952dSDimitry Andric         break;
525e580952dSDimitry Andric       }
526e580952dSDimitry Andric     }
527e580952dSDimitry Andric 
528e580952dSDimitry Andric     // We know "this" dominates the newly created basic block.
529e580952dSDimitry Andric     MachineDomTreeNode *NewDTNode = MDT->addNewBlock(NMBB, this);
530e580952dSDimitry Andric 
531e580952dSDimitry Andric     // If all the other predecessors of "Succ" are dominated by "Succ" itself
532e580952dSDimitry Andric     // then the new block is the new immediate dominator of "Succ". Otherwise,
533e580952dSDimitry Andric     // the new block doesn't dominate anything.
534e580952dSDimitry Andric     if (IsNewIDom)
535e580952dSDimitry Andric       MDT->changeImmediateDominator(SucccDTNode, NewDTNode);
536e580952dSDimitry Andric   }
537e580952dSDimitry Andric 
538e580952dSDimitry Andric   if (MachineLoopInfo *MLI = P->getAnalysisIfAvailable<MachineLoopInfo>())
539ffd1746dSEd Schouten     if (MachineLoop *TIL = MLI->getLoopFor(this)) {
540ffd1746dSEd Schouten       // If one or the other blocks were not in a loop, the new block is not
541ffd1746dSEd Schouten       // either, and thus LI doesn't need to be updated.
542ffd1746dSEd Schouten       if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) {
543ffd1746dSEd Schouten         if (TIL == DestLoop) {
544ffd1746dSEd Schouten           // Both in the same loop, the NMBB joins loop.
545ffd1746dSEd Schouten           DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase());
546ffd1746dSEd Schouten         } else if (TIL->contains(DestLoop)) {
547ffd1746dSEd Schouten           // Edge from an outer loop to an inner loop.  Add to the outer loop.
548ffd1746dSEd Schouten           TIL->addBasicBlockToLoop(NMBB, MLI->getBase());
549ffd1746dSEd Schouten         } else if (DestLoop->contains(TIL)) {
550ffd1746dSEd Schouten           // Edge from an inner loop to an outer loop.  Add to the outer loop.
551ffd1746dSEd Schouten           DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase());
552ffd1746dSEd Schouten         } else {
553ffd1746dSEd Schouten           // Edge from two loops with no containment relation.  Because these
554ffd1746dSEd Schouten           // are natural loops, we know that the destination block must be the
555ffd1746dSEd Schouten           // header of its loop (adding a branch into a loop elsewhere would
556ffd1746dSEd Schouten           // create an irreducible loop).
557ffd1746dSEd Schouten           assert(DestLoop->getHeader() == Succ &&
558ffd1746dSEd Schouten                  "Should not create irreducible loops!");
559ffd1746dSEd Schouten           if (MachineLoop *P = DestLoop->getParentLoop())
560ffd1746dSEd Schouten             P->addBasicBlockToLoop(NMBB, MLI->getBase());
561ffd1746dSEd Schouten         }
562ffd1746dSEd Schouten       }
563ffd1746dSEd Schouten     }
564ffd1746dSEd Schouten 
565ffd1746dSEd Schouten   return NMBB;
566ffd1746dSEd Schouten }
567ffd1746dSEd Schouten 
568f22ef01cSRoman Divacky /// removeFromParent - This method unlinks 'this' from the containing function,
569f22ef01cSRoman Divacky /// and returns it, but does not delete it.
570f22ef01cSRoman Divacky MachineBasicBlock *MachineBasicBlock::removeFromParent() {
571f22ef01cSRoman Divacky   assert(getParent() && "Not embedded in a function!");
572f22ef01cSRoman Divacky   getParent()->remove(this);
573f22ef01cSRoman Divacky   return this;
574f22ef01cSRoman Divacky }
575f22ef01cSRoman Divacky 
576f22ef01cSRoman Divacky 
577f22ef01cSRoman Divacky /// eraseFromParent - This method unlinks 'this' from the containing function,
578f22ef01cSRoman Divacky /// and deletes it.
579f22ef01cSRoman Divacky void MachineBasicBlock::eraseFromParent() {
580f22ef01cSRoman Divacky   assert(getParent() && "Not embedded in a function!");
581f22ef01cSRoman Divacky   getParent()->erase(this);
582f22ef01cSRoman Divacky }
583f22ef01cSRoman Divacky 
584f22ef01cSRoman Divacky 
585f22ef01cSRoman Divacky /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
586f22ef01cSRoman Divacky /// 'Old', change the code and CFG so that it branches to 'New' instead.
587f22ef01cSRoman Divacky void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old,
588f22ef01cSRoman Divacky                                                MachineBasicBlock *New) {
589f22ef01cSRoman Divacky   assert(Old != New && "Cannot replace self with self!");
590f22ef01cSRoman Divacky 
591f22ef01cSRoman Divacky   MachineBasicBlock::iterator I = end();
592f22ef01cSRoman Divacky   while (I != begin()) {
593f22ef01cSRoman Divacky     --I;
594f22ef01cSRoman Divacky     if (!I->getDesc().isTerminator()) break;
595f22ef01cSRoman Divacky 
596f22ef01cSRoman Divacky     // Scan the operands of this machine instruction, replacing any uses of Old
597f22ef01cSRoman Divacky     // with New.
598f22ef01cSRoman Divacky     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
599f22ef01cSRoman Divacky       if (I->getOperand(i).isMBB() &&
600f22ef01cSRoman Divacky           I->getOperand(i).getMBB() == Old)
601f22ef01cSRoman Divacky         I->getOperand(i).setMBB(New);
602f22ef01cSRoman Divacky   }
603f22ef01cSRoman Divacky 
604f22ef01cSRoman Divacky   // Update the successor information.
605f22ef01cSRoman Divacky   removeSuccessor(Old);
606f22ef01cSRoman Divacky   addSuccessor(New);
607f22ef01cSRoman Divacky }
608f22ef01cSRoman Divacky 
609f22ef01cSRoman Divacky /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the
610f22ef01cSRoman Divacky /// CFG to be inserted.  If we have proven that MBB can only branch to DestA and
611f22ef01cSRoman Divacky /// DestB, remove any other MBB successors from the CFG.  DestA and DestB can be
612f22ef01cSRoman Divacky /// null.
613f22ef01cSRoman Divacky ///
614f22ef01cSRoman Divacky /// Besides DestA and DestB, retain other edges leading to LandingPads
615f22ef01cSRoman Divacky /// (currently there can be only one; we don't check or require that here).
616f22ef01cSRoman Divacky /// Note it is possible that DestA and/or DestB are LandingPads.
617f22ef01cSRoman Divacky bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA,
618f22ef01cSRoman Divacky                                              MachineBasicBlock *DestB,
619f22ef01cSRoman Divacky                                              bool isCond) {
620f22ef01cSRoman Divacky   // The values of DestA and DestB frequently come from a call to the
621f22ef01cSRoman Divacky   // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial
622f22ef01cSRoman Divacky   // values from there.
623f22ef01cSRoman Divacky   //
624f22ef01cSRoman Divacky   // 1. If both DestA and DestB are null, then the block ends with no branches
625f22ef01cSRoman Divacky   //    (it falls through to its successor).
626f22ef01cSRoman Divacky   // 2. If DestA is set, DestB is null, and isCond is false, then the block ends
627f22ef01cSRoman Divacky   //    with only an unconditional branch.
628f22ef01cSRoman Divacky   // 3. If DestA is set, DestB is null, and isCond is true, then the block ends
629f22ef01cSRoman Divacky   //    with a conditional branch that falls through to a successor (DestB).
630f22ef01cSRoman Divacky   // 4. If DestA and DestB is set and isCond is true, then the block ends with a
631f22ef01cSRoman Divacky   //    conditional branch followed by an unconditional branch. DestA is the
632f22ef01cSRoman Divacky   //    'true' destination and DestB is the 'false' destination.
633f22ef01cSRoman Divacky 
634f22ef01cSRoman Divacky   bool Changed = false;
635f22ef01cSRoman Divacky 
636f22ef01cSRoman Divacky   MachineFunction::iterator FallThru =
637f22ef01cSRoman Divacky     llvm::next(MachineFunction::iterator(this));
638f22ef01cSRoman Divacky 
639f22ef01cSRoman Divacky   if (DestA == 0 && DestB == 0) {
640f22ef01cSRoman Divacky     // Block falls through to successor.
641f22ef01cSRoman Divacky     DestA = FallThru;
642f22ef01cSRoman Divacky     DestB = FallThru;
643f22ef01cSRoman Divacky   } else if (DestA != 0 && DestB == 0) {
644f22ef01cSRoman Divacky     if (isCond)
645f22ef01cSRoman Divacky       // Block ends in conditional jump that falls through to successor.
646f22ef01cSRoman Divacky       DestB = FallThru;
647f22ef01cSRoman Divacky   } else {
648f22ef01cSRoman Divacky     assert(DestA && DestB && isCond &&
649f22ef01cSRoman Divacky            "CFG in a bad state. Cannot correct CFG edges");
650f22ef01cSRoman Divacky   }
651f22ef01cSRoman Divacky 
652f22ef01cSRoman Divacky   // Remove superfluous edges. I.e., those which aren't destinations of this
653f22ef01cSRoman Divacky   // basic block, duplicate edges, or landing pads.
654f22ef01cSRoman Divacky   SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs;
655f22ef01cSRoman Divacky   MachineBasicBlock::succ_iterator SI = succ_begin();
656f22ef01cSRoman Divacky   while (SI != succ_end()) {
657f22ef01cSRoman Divacky     const MachineBasicBlock *MBB = *SI;
658f22ef01cSRoman Divacky     if (!SeenMBBs.insert(MBB) ||
659f22ef01cSRoman Divacky         (MBB != DestA && MBB != DestB && !MBB->isLandingPad())) {
660f22ef01cSRoman Divacky       // This is a superfluous edge, remove it.
661f22ef01cSRoman Divacky       SI = removeSuccessor(SI);
662f22ef01cSRoman Divacky       Changed = true;
663f22ef01cSRoman Divacky     } else {
664f22ef01cSRoman Divacky       ++SI;
665f22ef01cSRoman Divacky     }
666f22ef01cSRoman Divacky   }
667f22ef01cSRoman Divacky 
668f22ef01cSRoman Divacky   return Changed;
669f22ef01cSRoman Divacky }
670f22ef01cSRoman Divacky 
671f22ef01cSRoman Divacky /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
672f22ef01cSRoman Divacky /// any DBG_VALUE instructions.  Return UnknownLoc if there is none.
673f22ef01cSRoman Divacky DebugLoc
674f22ef01cSRoman Divacky MachineBasicBlock::findDebugLoc(MachineBasicBlock::iterator &MBBI) {
675f22ef01cSRoman Divacky   DebugLoc DL;
676f22ef01cSRoman Divacky   MachineBasicBlock::iterator E = end();
677f22ef01cSRoman Divacky   if (MBBI != E) {
678f22ef01cSRoman Divacky     // Skip debug declarations, we don't want a DebugLoc from them.
679f22ef01cSRoman Divacky     MachineBasicBlock::iterator MBBI2 = MBBI;
680f22ef01cSRoman Divacky     while (MBBI2 != E && MBBI2->isDebugValue())
681f22ef01cSRoman Divacky       MBBI2++;
682f22ef01cSRoman Divacky     if (MBBI2 != E)
683f22ef01cSRoman Divacky       DL = MBBI2->getDebugLoc();
684f22ef01cSRoman Divacky   }
685f22ef01cSRoman Divacky   return DL;
686f22ef01cSRoman Divacky }
687f22ef01cSRoman Divacky 
688f22ef01cSRoman Divacky void llvm::WriteAsOperand(raw_ostream &OS, const MachineBasicBlock *MBB,
689f22ef01cSRoman Divacky                           bool t) {
690f22ef01cSRoman Divacky   OS << "BB#" << MBB->getNumber();
691f22ef01cSRoman Divacky }
692f22ef01cSRoman Divacky 
693