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"
243861d79fSDimitry Andric #include "llvm/DataLayout.h"
25f22ef01cSRoman Divacky #include "llvm/Target/TargetInstrInfo.h"
26f22ef01cSRoman Divacky #include "llvm/Target/TargetMachine.h"
27f22ef01cSRoman Divacky #include "llvm/Assembly/Writer.h"
28f22ef01cSRoman Divacky #include "llvm/ADT/SmallString.h"
29f22ef01cSRoman Divacky #include "llvm/ADT/SmallPtrSet.h"
30f22ef01cSRoman Divacky #include "llvm/Support/Debug.h"
31f22ef01cSRoman Divacky #include "llvm/Support/LeakDetector.h"
32f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h"
33f22ef01cSRoman Divacky #include <algorithm>
34f22ef01cSRoman Divacky using namespace llvm;
35f22ef01cSRoman Divacky 
36f22ef01cSRoman Divacky MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb)
37f22ef01cSRoman Divacky   : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false),
38f22ef01cSRoman Divacky     AddressTaken(false) {
39f22ef01cSRoman Divacky   Insts.Parent = this;
40f22ef01cSRoman Divacky }
41f22ef01cSRoman Divacky 
42f22ef01cSRoman Divacky MachineBasicBlock::~MachineBasicBlock() {
43f22ef01cSRoman Divacky   LeakDetector::removeGarbageObject(this);
44f22ef01cSRoman Divacky }
45f22ef01cSRoman Divacky 
46f22ef01cSRoman Divacky /// getSymbol - Return the MCSymbol for this basic block.
47f22ef01cSRoman Divacky ///
48f22ef01cSRoman Divacky MCSymbol *MachineBasicBlock::getSymbol() const {
49f22ef01cSRoman Divacky   const MachineFunction *MF = getParent();
50f22ef01cSRoman Divacky   MCContext &Ctx = MF->getContext();
51f22ef01cSRoman Divacky   const char *Prefix = Ctx.getAsmInfo().getPrivateGlobalPrefix();
52f22ef01cSRoman Divacky   return Ctx.GetOrCreateSymbol(Twine(Prefix) + "BB" +
53f22ef01cSRoman Divacky                                Twine(MF->getFunctionNumber()) + "_" +
54f22ef01cSRoman Divacky                                Twine(getNumber()));
55f22ef01cSRoman Divacky }
56f22ef01cSRoman Divacky 
57f22ef01cSRoman Divacky 
58f22ef01cSRoman Divacky raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) {
59f22ef01cSRoman Divacky   MBB.print(OS);
60f22ef01cSRoman Divacky   return OS;
61f22ef01cSRoman Divacky }
62f22ef01cSRoman Divacky 
63f22ef01cSRoman Divacky /// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the
64f22ef01cSRoman Divacky /// parent pointer of the MBB, the MBB numbering, and any instructions in the
65f22ef01cSRoman Divacky /// MBB to be on the right operand list for registers.
66f22ef01cSRoman Divacky ///
67f22ef01cSRoman Divacky /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
68f22ef01cSRoman Divacky /// gets the next available unique MBB number. If it is removed from a
69f22ef01cSRoman Divacky /// MachineFunction, it goes back to being #-1.
70f22ef01cSRoman Divacky void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) {
71f22ef01cSRoman Divacky   MachineFunction &MF = *N->getParent();
72f22ef01cSRoman Divacky   N->Number = MF.addToMBBNumbering(N);
73f22ef01cSRoman Divacky 
74f22ef01cSRoman Divacky   // Make sure the instructions have their operands in the reginfo lists.
75f22ef01cSRoman Divacky   MachineRegisterInfo &RegInfo = MF.getRegInfo();
76dff0c46cSDimitry Andric   for (MachineBasicBlock::instr_iterator
77dff0c46cSDimitry Andric          I = N->instr_begin(), E = N->instr_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.
1127ae0e2c9SDimitry Andric   if (MachineFunction *MF = N->getParent()->getParent())
1137ae0e2c9SDimitry Andric     N->RemoveRegOperandsFromUseLists(MF->getRegInfo());
114f22ef01cSRoman Divacky 
115f22ef01cSRoman Divacky   N->setParent(0);
116f22ef01cSRoman Divacky 
117f22ef01cSRoman Divacky   LeakDetector::addGarbageObject(N);
118f22ef01cSRoman Divacky }
119f22ef01cSRoman Divacky 
120f22ef01cSRoman Divacky /// transferNodesFromList (MI) - When moving a range of instructions from one
121f22ef01cSRoman Divacky /// MBB list to another, we need to update the parent pointers and the use/def
122f22ef01cSRoman Divacky /// lists.
123f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::
124f22ef01cSRoman Divacky transferNodesFromList(ilist_traits<MachineInstr> &fromList,
125dff0c46cSDimitry Andric                       ilist_iterator<MachineInstr> first,
126dff0c46cSDimitry Andric                       ilist_iterator<MachineInstr> last) {
127f22ef01cSRoman Divacky   assert(Parent->getParent() == fromList.Parent->getParent() &&
128f22ef01cSRoman Divacky         "MachineInstr parent mismatch!");
129f22ef01cSRoman Divacky 
130f22ef01cSRoman Divacky   // Splice within the same MBB -> no change.
131f22ef01cSRoman Divacky   if (Parent == fromList.Parent) return;
132f22ef01cSRoman Divacky 
133f22ef01cSRoman Divacky   // If splicing between two blocks within the same function, just update the
134f22ef01cSRoman Divacky   // parent pointers.
135f22ef01cSRoman Divacky   for (; first != last; ++first)
136f22ef01cSRoman Divacky     first->setParent(Parent);
137f22ef01cSRoman Divacky }
138f22ef01cSRoman Divacky 
139f22ef01cSRoman Divacky void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) {
140f22ef01cSRoman Divacky   assert(!MI->getParent() && "MI is still in a block!");
141f22ef01cSRoman Divacky   Parent->getParent()->DeleteMachineInstr(MI);
142f22ef01cSRoman Divacky }
143f22ef01cSRoman Divacky 
144ffd1746dSEd Schouten MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() {
145dff0c46cSDimitry Andric   instr_iterator I = instr_begin(), E = instr_end();
146dff0c46cSDimitry Andric   while (I != E && I->isPHI())
147ffd1746dSEd Schouten     ++I;
1483861d79fSDimitry Andric   assert((I == E || !I->isInsideBundle()) &&
1493861d79fSDimitry Andric          "First non-phi MI cannot be inside a bundle!");
150ffd1746dSEd Schouten   return I;
151ffd1746dSEd Schouten }
152ffd1746dSEd Schouten 
1532754fe60SDimitry Andric MachineBasicBlock::iterator
1542754fe60SDimitry Andric MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) {
155dff0c46cSDimitry Andric   iterator E = end();
156dff0c46cSDimitry Andric   while (I != E && (I->isPHI() || I->isLabel() || I->isDebugValue()))
1572754fe60SDimitry Andric     ++I;
158dff0c46cSDimitry Andric   // FIXME: This needs to change if we wish to bundle labels / dbg_values
159dff0c46cSDimitry Andric   // inside the bundle.
1603861d79fSDimitry Andric   assert((I == E || !I->isInsideBundle()) &&
161dff0c46cSDimitry Andric          "First non-phi / non-label instruction is inside a bundle!");
1622754fe60SDimitry Andric   return I;
1632754fe60SDimitry Andric }
1642754fe60SDimitry Andric 
165f22ef01cSRoman Divacky MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
166dff0c46cSDimitry Andric   iterator B = begin(), E = end(), I = E;
167dff0c46cSDimitry Andric   while (I != B && ((--I)->isTerminator() || I->isDebugValue()))
168f22ef01cSRoman Divacky     ; /*noop */
169dff0c46cSDimitry Andric   while (I != E && !I->isTerminator())
170dff0c46cSDimitry Andric     ++I;
171dff0c46cSDimitry Andric   return I;
172dff0c46cSDimitry Andric }
173dff0c46cSDimitry Andric 
174dff0c46cSDimitry Andric MachineBasicBlock::const_iterator
175dff0c46cSDimitry Andric MachineBasicBlock::getFirstTerminator() const {
176dff0c46cSDimitry Andric   const_iterator B = begin(), E = end(), I = E;
177dff0c46cSDimitry Andric   while (I != B && ((--I)->isTerminator() || I->isDebugValue()))
178dff0c46cSDimitry Andric     ; /*noop */
179dff0c46cSDimitry Andric   while (I != E && !I->isTerminator())
180dff0c46cSDimitry Andric     ++I;
181dff0c46cSDimitry Andric   return I;
182dff0c46cSDimitry Andric }
183dff0c46cSDimitry Andric 
184dff0c46cSDimitry Andric MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() {
185dff0c46cSDimitry Andric   instr_iterator B = instr_begin(), E = instr_end(), I = E;
186dff0c46cSDimitry Andric   while (I != B && ((--I)->isTerminator() || I->isDebugValue()))
187dff0c46cSDimitry Andric     ; /*noop */
188dff0c46cSDimitry Andric   while (I != E && !I->isTerminator())
1892754fe60SDimitry Andric     ++I;
190f22ef01cSRoman Divacky   return I;
191f22ef01cSRoman Divacky }
192f22ef01cSRoman Divacky 
1932754fe60SDimitry Andric MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() {
194dff0c46cSDimitry Andric   // Skip over end-of-block dbg_value instructions.
195dff0c46cSDimitry Andric   instr_iterator B = instr_begin(), I = instr_end();
1962754fe60SDimitry Andric   while (I != B) {
1972754fe60SDimitry Andric     --I;
198dff0c46cSDimitry Andric     // Return instruction that starts a bundle.
199dff0c46cSDimitry Andric     if (I->isDebugValue() || I->isInsideBundle())
200dff0c46cSDimitry Andric       continue;
201dff0c46cSDimitry Andric     return I;
202dff0c46cSDimitry Andric   }
203dff0c46cSDimitry Andric   // The block is all debug values.
204dff0c46cSDimitry Andric   return end();
205dff0c46cSDimitry Andric }
206dff0c46cSDimitry Andric 
207dff0c46cSDimitry Andric MachineBasicBlock::const_iterator
208dff0c46cSDimitry Andric MachineBasicBlock::getLastNonDebugInstr() const {
209dff0c46cSDimitry Andric   // Skip over end-of-block dbg_value instructions.
210dff0c46cSDimitry Andric   const_instr_iterator B = instr_begin(), I = instr_end();
211dff0c46cSDimitry Andric   while (I != B) {
212dff0c46cSDimitry Andric     --I;
213dff0c46cSDimitry Andric     // Return instruction that starts a bundle.
214dff0c46cSDimitry Andric     if (I->isDebugValue() || I->isInsideBundle())
2152754fe60SDimitry Andric       continue;
2162754fe60SDimitry Andric     return I;
2172754fe60SDimitry Andric   }
2182754fe60SDimitry Andric   // The block is all debug values.
2192754fe60SDimitry Andric   return end();
2202754fe60SDimitry Andric }
2212754fe60SDimitry Andric 
2222754fe60SDimitry Andric const MachineBasicBlock *MachineBasicBlock::getLandingPadSuccessor() const {
2232754fe60SDimitry Andric   // A block with a landing pad successor only has one other successor.
2242754fe60SDimitry Andric   if (succ_size() > 2)
2252754fe60SDimitry Andric     return 0;
2262754fe60SDimitry Andric   for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I)
2272754fe60SDimitry Andric     if ((*I)->isLandingPad())
2282754fe60SDimitry Andric       return *I;
2292754fe60SDimitry Andric   return 0;
2302754fe60SDimitry Andric }
2312754fe60SDimitry Andric 
2323861d79fSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
233f22ef01cSRoman Divacky void MachineBasicBlock::dump() const {
234f22ef01cSRoman Divacky   print(dbgs());
235f22ef01cSRoman Divacky }
2363861d79fSDimitry Andric #endif
237f22ef01cSRoman Divacky 
238f22ef01cSRoman Divacky StringRef MachineBasicBlock::getName() const {
239f22ef01cSRoman Divacky   if (const BasicBlock *LBB = getBasicBlock())
240f22ef01cSRoman Divacky     return LBB->getName();
241f22ef01cSRoman Divacky   else
242f22ef01cSRoman Divacky     return "(null)";
243f22ef01cSRoman Divacky }
244f22ef01cSRoman Divacky 
245dff0c46cSDimitry Andric /// Return a hopefully unique identifier for this block.
246dff0c46cSDimitry Andric std::string MachineBasicBlock::getFullName() const {
247dff0c46cSDimitry Andric   std::string Name;
248dff0c46cSDimitry Andric   if (getParent())
2493861d79fSDimitry Andric     Name = (getParent()->getName() + ":").str();
250dff0c46cSDimitry Andric   if (getBasicBlock())
251dff0c46cSDimitry Andric     Name += getBasicBlock()->getName();
252dff0c46cSDimitry Andric   else
253dff0c46cSDimitry Andric     Name += (Twine("BB") + Twine(getNumber())).str();
254dff0c46cSDimitry Andric   return Name;
255dff0c46cSDimitry Andric }
256dff0c46cSDimitry Andric 
2572754fe60SDimitry Andric void MachineBasicBlock::print(raw_ostream &OS, SlotIndexes *Indexes) const {
258f22ef01cSRoman Divacky   const MachineFunction *MF = getParent();
259f22ef01cSRoman Divacky   if (!MF) {
260f22ef01cSRoman Divacky     OS << "Can't print out MachineBasicBlock because parent MachineFunction"
261f22ef01cSRoman Divacky        << " is null\n";
262f22ef01cSRoman Divacky     return;
263f22ef01cSRoman Divacky   }
264f22ef01cSRoman Divacky 
2652754fe60SDimitry Andric   if (Indexes)
2662754fe60SDimitry Andric     OS << Indexes->getMBBStartIdx(this) << '\t';
2672754fe60SDimitry Andric 
268f22ef01cSRoman Divacky   OS << "BB#" << getNumber() << ": ";
269f22ef01cSRoman Divacky 
270f22ef01cSRoman Divacky   const char *Comma = "";
271f22ef01cSRoman Divacky   if (const BasicBlock *LBB = getBasicBlock()) {
272f22ef01cSRoman Divacky     OS << Comma << "derived from LLVM BB ";
273f22ef01cSRoman Divacky     WriteAsOperand(OS, LBB, /*PrintType=*/false);
274f22ef01cSRoman Divacky     Comma = ", ";
275f22ef01cSRoman Divacky   }
276f22ef01cSRoman Divacky   if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; }
277f22ef01cSRoman Divacky   if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; }
2787ae0e2c9SDimitry Andric   if (Alignment)
279dff0c46cSDimitry Andric     OS << Comma << "Align " << Alignment << " (" << (1u << Alignment)
280dff0c46cSDimitry Andric        << " bytes)";
281dff0c46cSDimitry Andric 
282f22ef01cSRoman Divacky   OS << '\n';
283f22ef01cSRoman Divacky 
284f22ef01cSRoman Divacky   const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
285f22ef01cSRoman Divacky   if (!livein_empty()) {
2862754fe60SDimitry Andric     if (Indexes) OS << '\t';
287f22ef01cSRoman Divacky     OS << "    Live Ins:";
288f22ef01cSRoman Divacky     for (livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I)
2892754fe60SDimitry Andric       OS << ' ' << PrintReg(*I, TRI);
290f22ef01cSRoman Divacky     OS << '\n';
291f22ef01cSRoman Divacky   }
292f22ef01cSRoman Divacky   // Print the preds of this block according to the CFG.
293f22ef01cSRoman Divacky   if (!pred_empty()) {
2942754fe60SDimitry Andric     if (Indexes) OS << '\t';
295f22ef01cSRoman Divacky     OS << "    Predecessors according to CFG:";
296f22ef01cSRoman Divacky     for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
297f22ef01cSRoman Divacky       OS << " BB#" << (*PI)->getNumber();
298f22ef01cSRoman Divacky     OS << '\n';
299f22ef01cSRoman Divacky   }
300f22ef01cSRoman Divacky 
301dff0c46cSDimitry Andric   for (const_instr_iterator I = instr_begin(); I != instr_end(); ++I) {
3022754fe60SDimitry Andric     if (Indexes) {
3032754fe60SDimitry Andric       if (Indexes->hasIndex(I))
3042754fe60SDimitry Andric         OS << Indexes->getInstructionIndex(I);
3052754fe60SDimitry Andric       OS << '\t';
3062754fe60SDimitry Andric     }
307f22ef01cSRoman Divacky     OS << '\t';
308dff0c46cSDimitry Andric     if (I->isInsideBundle())
309dff0c46cSDimitry Andric       OS << "  * ";
310f22ef01cSRoman Divacky     I->print(OS, &getParent()->getTarget());
311f22ef01cSRoman Divacky   }
312f22ef01cSRoman Divacky 
313f22ef01cSRoman Divacky   // Print the successors of this block according to the CFG.
314f22ef01cSRoman Divacky   if (!succ_empty()) {
3152754fe60SDimitry Andric     if (Indexes) OS << '\t';
316f22ef01cSRoman Divacky     OS << "    Successors according to CFG:";
3177ae0e2c9SDimitry Andric     for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) {
318f22ef01cSRoman Divacky       OS << " BB#" << (*SI)->getNumber();
3197ae0e2c9SDimitry Andric       if (!Weights.empty())
3207ae0e2c9SDimitry Andric         OS << '(' << *getWeightIterator(SI) << ')';
3217ae0e2c9SDimitry Andric     }
322f22ef01cSRoman Divacky     OS << '\n';
323f22ef01cSRoman Divacky   }
324f22ef01cSRoman Divacky }
325f22ef01cSRoman Divacky 
326f22ef01cSRoman Divacky void MachineBasicBlock::removeLiveIn(unsigned Reg) {
327f22ef01cSRoman Divacky   std::vector<unsigned>::iterator I =
328f22ef01cSRoman Divacky     std::find(LiveIns.begin(), LiveIns.end(), Reg);
329dff0c46cSDimitry Andric   if (I != LiveIns.end())
330f22ef01cSRoman Divacky     LiveIns.erase(I);
331f22ef01cSRoman Divacky }
332f22ef01cSRoman Divacky 
333f22ef01cSRoman Divacky bool MachineBasicBlock::isLiveIn(unsigned Reg) const {
334f22ef01cSRoman Divacky   livein_iterator I = std::find(livein_begin(), livein_end(), Reg);
335f22ef01cSRoman Divacky   return I != livein_end();
336f22ef01cSRoman Divacky }
337f22ef01cSRoman Divacky 
338f22ef01cSRoman Divacky void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
339f22ef01cSRoman Divacky   getParent()->splice(NewAfter, this);
340f22ef01cSRoman Divacky }
341f22ef01cSRoman Divacky 
342f22ef01cSRoman Divacky void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
343f22ef01cSRoman Divacky   MachineFunction::iterator BBI = NewBefore;
344f22ef01cSRoman Divacky   getParent()->splice(++BBI, this);
345f22ef01cSRoman Divacky }
346f22ef01cSRoman Divacky 
347f22ef01cSRoman Divacky void MachineBasicBlock::updateTerminator() {
348f22ef01cSRoman Divacky   const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo();
349f22ef01cSRoman Divacky   // A block with no successors has no concerns with fall-through edges.
350f22ef01cSRoman Divacky   if (this->succ_empty()) return;
351f22ef01cSRoman Divacky 
352f22ef01cSRoman Divacky   MachineBasicBlock *TBB = 0, *FBB = 0;
353f22ef01cSRoman Divacky   SmallVector<MachineOperand, 4> Cond;
354ffd1746dSEd Schouten   DebugLoc dl;  // FIXME: this is nowhere
355f22ef01cSRoman Divacky   bool B = TII->AnalyzeBranch(*this, TBB, FBB, Cond);
356f22ef01cSRoman Divacky   (void) B;
357f22ef01cSRoman Divacky   assert(!B && "UpdateTerminators requires analyzable predecessors!");
358f22ef01cSRoman Divacky   if (Cond.empty()) {
359f22ef01cSRoman Divacky     if (TBB) {
360f22ef01cSRoman Divacky       // The block has an unconditional branch. If its successor is now
361f22ef01cSRoman Divacky       // its layout successor, delete the branch.
362f22ef01cSRoman Divacky       if (isLayoutSuccessor(TBB))
363f22ef01cSRoman Divacky         TII->RemoveBranch(*this);
364f22ef01cSRoman Divacky     } else {
365f22ef01cSRoman Divacky       // The block has an unconditional fallthrough. If its successor is not
366dff0c46cSDimitry Andric       // its layout successor, insert a branch. First we have to locate the
367dff0c46cSDimitry Andric       // only non-landing-pad successor, as that is the fallthrough block.
368dff0c46cSDimitry Andric       for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) {
369dff0c46cSDimitry Andric         if ((*SI)->isLandingPad())
370dff0c46cSDimitry Andric           continue;
371dff0c46cSDimitry Andric         assert(!TBB && "Found more than one non-landing-pad successor!");
372dff0c46cSDimitry Andric         TBB = *SI;
373dff0c46cSDimitry Andric       }
374dff0c46cSDimitry Andric 
375dff0c46cSDimitry Andric       // If there is no non-landing-pad successor, the block has no
376dff0c46cSDimitry Andric       // fall-through edges to be concerned with.
377dff0c46cSDimitry Andric       if (!TBB)
378dff0c46cSDimitry Andric         return;
379dff0c46cSDimitry Andric 
380dff0c46cSDimitry Andric       // Finally update the unconditional successor to be reached via a branch
381dff0c46cSDimitry Andric       // if it would not be reached by fallthrough.
382f22ef01cSRoman Divacky       if (!isLayoutSuccessor(TBB))
383ffd1746dSEd Schouten         TII->InsertBranch(*this, TBB, 0, Cond, dl);
384f22ef01cSRoman Divacky     }
385f22ef01cSRoman Divacky   } else {
386f22ef01cSRoman Divacky     if (FBB) {
387f22ef01cSRoman Divacky       // The block has a non-fallthrough conditional branch. If one of its
388f22ef01cSRoman Divacky       // successors is its layout successor, rewrite it to a fallthrough
389f22ef01cSRoman Divacky       // conditional branch.
390f22ef01cSRoman Divacky       if (isLayoutSuccessor(TBB)) {
391f22ef01cSRoman Divacky         if (TII->ReverseBranchCondition(Cond))
392f22ef01cSRoman Divacky           return;
393f22ef01cSRoman Divacky         TII->RemoveBranch(*this);
394ffd1746dSEd Schouten         TII->InsertBranch(*this, FBB, 0, Cond, dl);
395f22ef01cSRoman Divacky       } else if (isLayoutSuccessor(FBB)) {
396f22ef01cSRoman Divacky         TII->RemoveBranch(*this);
397ffd1746dSEd Schouten         TII->InsertBranch(*this, TBB, 0, Cond, dl);
398f22ef01cSRoman Divacky       }
399f22ef01cSRoman Divacky     } else {
400cb4dff85SDimitry Andric       // Walk through the successors and find the successor which is not
401cb4dff85SDimitry Andric       // a landing pad and is not the conditional branch destination (in TBB)
402cb4dff85SDimitry Andric       // as the fallthrough successor.
403cb4dff85SDimitry Andric       MachineBasicBlock *FallthroughBB = 0;
404cb4dff85SDimitry Andric       for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) {
405cb4dff85SDimitry Andric         if ((*SI)->isLandingPad() || *SI == TBB)
406cb4dff85SDimitry Andric           continue;
407cb4dff85SDimitry Andric         assert(!FallthroughBB && "Found more than one fallthrough successor.");
408cb4dff85SDimitry Andric         FallthroughBB = *SI;
409cb4dff85SDimitry Andric       }
410cb4dff85SDimitry Andric       if (!FallthroughBB && canFallThrough()) {
411cb4dff85SDimitry Andric         // We fallthrough to the same basic block as the conditional jump
412cb4dff85SDimitry Andric         // targets. Remove the conditional jump, leaving unconditional
413cb4dff85SDimitry Andric         // fallthrough.
414cb4dff85SDimitry Andric         // FIXME: This does not seem like a reasonable pattern to support, but it
415cb4dff85SDimitry Andric         // has been seen in the wild coming out of degenerate ARM test cases.
416cb4dff85SDimitry Andric         TII->RemoveBranch(*this);
417cb4dff85SDimitry Andric 
418cb4dff85SDimitry Andric         // Finally update the unconditional successor to be reached via a branch
419cb4dff85SDimitry Andric         // if it would not be reached by fallthrough.
420cb4dff85SDimitry Andric         if (!isLayoutSuccessor(TBB))
421cb4dff85SDimitry Andric           TII->InsertBranch(*this, TBB, 0, Cond, dl);
422cb4dff85SDimitry Andric         return;
423cb4dff85SDimitry Andric       }
424cb4dff85SDimitry Andric 
425f22ef01cSRoman Divacky       // The block has a fallthrough conditional branch.
426f22ef01cSRoman Divacky       if (isLayoutSuccessor(TBB)) {
427f22ef01cSRoman Divacky         if (TII->ReverseBranchCondition(Cond)) {
428f22ef01cSRoman Divacky           // We can't reverse the condition, add an unconditional branch.
429f22ef01cSRoman Divacky           Cond.clear();
430cb4dff85SDimitry Andric           TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl);
431f22ef01cSRoman Divacky           return;
432f22ef01cSRoman Divacky         }
433f22ef01cSRoman Divacky         TII->RemoveBranch(*this);
434cb4dff85SDimitry Andric         TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl);
435cb4dff85SDimitry Andric       } else if (!isLayoutSuccessor(FallthroughBB)) {
436f22ef01cSRoman Divacky         TII->RemoveBranch(*this);
437cb4dff85SDimitry Andric         TII->InsertBranch(*this, TBB, FallthroughBB, Cond, dl);
438f22ef01cSRoman Divacky       }
439f22ef01cSRoman Divacky     }
440f22ef01cSRoman Divacky   }
441f22ef01cSRoman Divacky }
442f22ef01cSRoman Divacky 
44317a519f9SDimitry Andric void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ, uint32_t weight) {
44417a519f9SDimitry Andric 
44517a519f9SDimitry Andric   // If we see non-zero value for the first time it means we actually use Weight
44617a519f9SDimitry Andric   // list, so we fill all Weights with 0's.
44717a519f9SDimitry Andric   if (weight != 0 && Weights.empty())
44817a519f9SDimitry Andric     Weights.resize(Successors.size());
44917a519f9SDimitry Andric 
45017a519f9SDimitry Andric   if (weight != 0 || !Weights.empty())
45117a519f9SDimitry Andric     Weights.push_back(weight);
45217a519f9SDimitry Andric 
453f22ef01cSRoman Divacky    Successors.push_back(succ);
454f22ef01cSRoman Divacky    succ->addPredecessor(this);
455f22ef01cSRoman Divacky  }
456f22ef01cSRoman Divacky 
457f22ef01cSRoman Divacky void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
458f22ef01cSRoman Divacky   succ->removePredecessor(this);
459f22ef01cSRoman Divacky   succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
460f22ef01cSRoman Divacky   assert(I != Successors.end() && "Not a current successor!");
46117a519f9SDimitry Andric 
46217a519f9SDimitry Andric   // If Weight list is empty it means we don't use it (disabled optimization).
46317a519f9SDimitry Andric   if (!Weights.empty()) {
46417a519f9SDimitry Andric     weight_iterator WI = getWeightIterator(I);
46517a519f9SDimitry Andric     Weights.erase(WI);
46617a519f9SDimitry Andric   }
46717a519f9SDimitry Andric 
468f22ef01cSRoman Divacky   Successors.erase(I);
469f22ef01cSRoman Divacky }
470f22ef01cSRoman Divacky 
471f22ef01cSRoman Divacky MachineBasicBlock::succ_iterator
472f22ef01cSRoman Divacky MachineBasicBlock::removeSuccessor(succ_iterator I) {
473f22ef01cSRoman Divacky   assert(I != Successors.end() && "Not a current successor!");
47417a519f9SDimitry Andric 
47517a519f9SDimitry Andric   // If Weight list is empty it means we don't use it (disabled optimization).
47617a519f9SDimitry Andric   if (!Weights.empty()) {
47717a519f9SDimitry Andric     weight_iterator WI = getWeightIterator(I);
47817a519f9SDimitry Andric     Weights.erase(WI);
47917a519f9SDimitry Andric   }
48017a519f9SDimitry Andric 
481f22ef01cSRoman Divacky   (*I)->removePredecessor(this);
482f22ef01cSRoman Divacky   return Successors.erase(I);
483f22ef01cSRoman Divacky }
484f22ef01cSRoman Divacky 
48517a519f9SDimitry Andric void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old,
48617a519f9SDimitry Andric                                          MachineBasicBlock *New) {
4877ae0e2c9SDimitry Andric   if (Old == New)
4887ae0e2c9SDimitry Andric     return;
48917a519f9SDimitry Andric 
4907ae0e2c9SDimitry Andric   succ_iterator E = succ_end();
4917ae0e2c9SDimitry Andric   succ_iterator NewI = E;
4927ae0e2c9SDimitry Andric   succ_iterator OldI = E;
4937ae0e2c9SDimitry Andric   for (succ_iterator I = succ_begin(); I != E; ++I) {
4947ae0e2c9SDimitry Andric     if (*I == Old) {
4957ae0e2c9SDimitry Andric       OldI = I;
4967ae0e2c9SDimitry Andric       if (NewI != E)
4977ae0e2c9SDimitry Andric         break;
4987ae0e2c9SDimitry Andric     }
4997ae0e2c9SDimitry Andric     if (*I == New) {
5007ae0e2c9SDimitry Andric       NewI = I;
5017ae0e2c9SDimitry Andric       if (OldI != E)
5027ae0e2c9SDimitry Andric         break;
5037ae0e2c9SDimitry Andric     }
5047ae0e2c9SDimitry Andric   }
5057ae0e2c9SDimitry Andric   assert(OldI != E && "Old is not a successor of this block");
5067ae0e2c9SDimitry Andric   Old->removePredecessor(this);
5077ae0e2c9SDimitry Andric 
5087ae0e2c9SDimitry Andric   // If New isn't already a successor, let it take Old's place.
5097ae0e2c9SDimitry Andric   if (NewI == E) {
5107ae0e2c9SDimitry Andric     New->addPredecessor(this);
5117ae0e2c9SDimitry Andric     *OldI = New;
5127ae0e2c9SDimitry Andric     return;
51317a519f9SDimitry Andric   }
51417a519f9SDimitry Andric 
5157ae0e2c9SDimitry Andric   // New is already a successor.
5167ae0e2c9SDimitry Andric   // Update its weight instead of adding a duplicate edge.
5177ae0e2c9SDimitry Andric   if (!Weights.empty()) {
5187ae0e2c9SDimitry Andric     weight_iterator OldWI = getWeightIterator(OldI);
5197ae0e2c9SDimitry Andric     *getWeightIterator(NewI) += *OldWI;
5207ae0e2c9SDimitry Andric     Weights.erase(OldWI);
5217ae0e2c9SDimitry Andric   }
5227ae0e2c9SDimitry Andric   Successors.erase(OldI);
52317a519f9SDimitry Andric }
52417a519f9SDimitry Andric 
525f22ef01cSRoman Divacky void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
526f22ef01cSRoman Divacky   Predecessors.push_back(pred);
527f22ef01cSRoman Divacky }
528f22ef01cSRoman Divacky 
529f22ef01cSRoman Divacky void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
5303b0f4066SDimitry Andric   pred_iterator I = std::find(Predecessors.begin(), Predecessors.end(), pred);
531f22ef01cSRoman Divacky   assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
532f22ef01cSRoman Divacky   Predecessors.erase(I);
533f22ef01cSRoman Divacky }
534f22ef01cSRoman Divacky 
535f22ef01cSRoman Divacky void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) {
536f22ef01cSRoman Divacky   if (this == fromMBB)
537f22ef01cSRoman Divacky     return;
538f22ef01cSRoman Divacky 
539ffd1746dSEd Schouten   while (!fromMBB->succ_empty()) {
540ffd1746dSEd Schouten     MachineBasicBlock *Succ = *fromMBB->succ_begin();
5417ae0e2c9SDimitry Andric     uint32_t Weight = 0;
54217a519f9SDimitry Andric 
54317a519f9SDimitry Andric     // If Weight list is empty it means we don't use it (disabled optimization).
54417a519f9SDimitry Andric     if (!fromMBB->Weights.empty())
5457ae0e2c9SDimitry Andric       Weight = *fromMBB->Weights.begin();
54617a519f9SDimitry Andric 
5477ae0e2c9SDimitry Andric     addSuccessor(Succ, Weight);
548ffd1746dSEd Schouten     fromMBB->removeSuccessor(Succ);
549ffd1746dSEd Schouten   }
550ffd1746dSEd Schouten }
551f22ef01cSRoman Divacky 
552ffd1746dSEd Schouten void
553ffd1746dSEd Schouten MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB) {
554ffd1746dSEd Schouten   if (this == fromMBB)
555ffd1746dSEd Schouten     return;
556ffd1746dSEd Schouten 
557ffd1746dSEd Schouten   while (!fromMBB->succ_empty()) {
558ffd1746dSEd Schouten     MachineBasicBlock *Succ = *fromMBB->succ_begin();
5597ae0e2c9SDimitry Andric     uint32_t Weight = 0;
5607ae0e2c9SDimitry Andric     if (!fromMBB->Weights.empty())
5617ae0e2c9SDimitry Andric       Weight = *fromMBB->Weights.begin();
5627ae0e2c9SDimitry Andric     addSuccessor(Succ, Weight);
563ffd1746dSEd Schouten     fromMBB->removeSuccessor(Succ);
564ffd1746dSEd Schouten 
565ffd1746dSEd Schouten     // Fix up any PHI nodes in the successor.
566dff0c46cSDimitry Andric     for (MachineBasicBlock::instr_iterator MI = Succ->instr_begin(),
567dff0c46cSDimitry Andric            ME = Succ->instr_end(); MI != ME && MI->isPHI(); ++MI)
568ffd1746dSEd Schouten       for (unsigned i = 2, e = MI->getNumOperands()+1; i != e; i += 2) {
569ffd1746dSEd Schouten         MachineOperand &MO = MI->getOperand(i);
570ffd1746dSEd Schouten         if (MO.getMBB() == fromMBB)
571ffd1746dSEd Schouten           MO.setMBB(this);
572ffd1746dSEd Schouten       }
573ffd1746dSEd Schouten   }
574f22ef01cSRoman Divacky }
575f22ef01cSRoman Divacky 
5767ae0e2c9SDimitry Andric bool MachineBasicBlock::isPredecessor(const MachineBasicBlock *MBB) const {
5777ae0e2c9SDimitry Andric   return std::find(pred_begin(), pred_end(), MBB) != pred_end();
5787ae0e2c9SDimitry Andric }
5797ae0e2c9SDimitry Andric 
580f22ef01cSRoman Divacky bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const {
5817ae0e2c9SDimitry Andric   return std::find(succ_begin(), succ_end(), MBB) != succ_end();
582f22ef01cSRoman Divacky }
583f22ef01cSRoman Divacky 
584f22ef01cSRoman Divacky bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const {
585f22ef01cSRoman Divacky   MachineFunction::const_iterator I(this);
586f22ef01cSRoman Divacky   return llvm::next(I) == MachineFunction::const_iterator(MBB);
587f22ef01cSRoman Divacky }
588f22ef01cSRoman Divacky 
589f22ef01cSRoman Divacky bool MachineBasicBlock::canFallThrough() {
590f22ef01cSRoman Divacky   MachineFunction::iterator Fallthrough = this;
591f22ef01cSRoman Divacky   ++Fallthrough;
592f22ef01cSRoman Divacky   // If FallthroughBlock is off the end of the function, it can't fall through.
593f22ef01cSRoman Divacky   if (Fallthrough == getParent()->end())
594f22ef01cSRoman Divacky     return false;
595f22ef01cSRoman Divacky 
596f22ef01cSRoman Divacky   // If FallthroughBlock isn't a successor, no fallthrough is possible.
597f22ef01cSRoman Divacky   if (!isSuccessor(Fallthrough))
598f22ef01cSRoman Divacky     return false;
599f22ef01cSRoman Divacky 
600f22ef01cSRoman Divacky   // Analyze the branches, if any, at the end of the block.
601f22ef01cSRoman Divacky   MachineBasicBlock *TBB = 0, *FBB = 0;
602f22ef01cSRoman Divacky   SmallVector<MachineOperand, 4> Cond;
603f22ef01cSRoman Divacky   const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo();
604f22ef01cSRoman Divacky   if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) {
605f22ef01cSRoman Divacky     // If we couldn't analyze the branch, examine the last instruction.
606f22ef01cSRoman Divacky     // If the block doesn't end in a known control barrier, assume fallthrough
607dff0c46cSDimitry Andric     // is possible. The isPredicated check is needed because this code can be
608f22ef01cSRoman Divacky     // called during IfConversion, where an instruction which is normally a
609dff0c46cSDimitry Andric     // Barrier is predicated and thus no longer an actual control barrier.
610dff0c46cSDimitry Andric     return empty() || !back().isBarrier() || TII->isPredicated(&back());
611f22ef01cSRoman Divacky   }
612f22ef01cSRoman Divacky 
613f22ef01cSRoman Divacky   // If there is no branch, control always falls through.
614f22ef01cSRoman Divacky   if (TBB == 0) return true;
615f22ef01cSRoman Divacky 
616f22ef01cSRoman Divacky   // If there is some explicit branch to the fallthrough block, it can obviously
617f22ef01cSRoman Divacky   // reach, even though the branch should get folded to fall through implicitly.
618f22ef01cSRoman Divacky   if (MachineFunction::iterator(TBB) == Fallthrough ||
619f22ef01cSRoman Divacky       MachineFunction::iterator(FBB) == Fallthrough)
620f22ef01cSRoman Divacky     return true;
621f22ef01cSRoman Divacky 
622f22ef01cSRoman Divacky   // If it's an unconditional branch to some block not the fall through, it
623f22ef01cSRoman Divacky   // doesn't fall through.
624f22ef01cSRoman Divacky   if (Cond.empty()) return false;
625f22ef01cSRoman Divacky 
626f22ef01cSRoman Divacky   // Otherwise, if it is conditional and has no explicit false block, it falls
627f22ef01cSRoman Divacky   // through.
628f22ef01cSRoman Divacky   return FBB == 0;
629f22ef01cSRoman Divacky }
630f22ef01cSRoman Divacky 
631ffd1746dSEd Schouten MachineBasicBlock *
632ffd1746dSEd Schouten MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P) {
6337ae0e2c9SDimitry Andric   // Splitting the critical edge to a landing pad block is non-trivial. Don't do
6347ae0e2c9SDimitry Andric   // it in this generic function.
6357ae0e2c9SDimitry Andric   if (Succ->isLandingPad())
6367ae0e2c9SDimitry Andric     return NULL;
6377ae0e2c9SDimitry Andric 
638ffd1746dSEd Schouten   MachineFunction *MF = getParent();
639ffd1746dSEd Schouten   DebugLoc dl;  // FIXME: this is nowhere
640ffd1746dSEd Schouten 
6412754fe60SDimitry Andric   // We may need to update this's terminator, but we can't do that if
6422754fe60SDimitry Andric   // AnalyzeBranch fails. If this uses a jump table, we won't touch it.
643ffd1746dSEd Schouten   const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
644ffd1746dSEd Schouten   MachineBasicBlock *TBB = 0, *FBB = 0;
645ffd1746dSEd Schouten   SmallVector<MachineOperand, 4> Cond;
646ffd1746dSEd Schouten   if (TII->AnalyzeBranch(*this, TBB, FBB, Cond))
647ffd1746dSEd Schouten     return NULL;
648ffd1746dSEd Schouten 
6492754fe60SDimitry Andric   // Avoid bugpoint weirdness: A block may end with a conditional branch but
6502754fe60SDimitry Andric   // jumps to the same MBB is either case. We have duplicate CFG edges in that
6512754fe60SDimitry Andric   // case that we can't handle. Since this never happens in properly optimized
6522754fe60SDimitry Andric   // code, just skip those edges.
6532754fe60SDimitry Andric   if (TBB && TBB == FBB) {
6542754fe60SDimitry Andric     DEBUG(dbgs() << "Won't split critical edge after degenerate BB#"
6552754fe60SDimitry Andric                  << getNumber() << '\n');
6562754fe60SDimitry Andric     return NULL;
6572754fe60SDimitry Andric   }
6582754fe60SDimitry Andric 
659ffd1746dSEd Schouten   MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock();
660ffd1746dSEd Schouten   MF->insert(llvm::next(MachineFunction::iterator(this)), NMBB);
661e580952dSDimitry Andric   DEBUG(dbgs() << "Splitting critical edge:"
662ffd1746dSEd Schouten         " BB#" << getNumber()
663ffd1746dSEd Schouten         << " -- BB#" << NMBB->getNumber()
664ffd1746dSEd Schouten         << " -- BB#" << Succ->getNumber() << '\n');
665ffd1746dSEd Schouten 
666bd5abe19SDimitry Andric   // On some targets like Mips, branches may kill virtual registers. Make sure
667bd5abe19SDimitry Andric   // that LiveVariables is properly updated after updateTerminator replaces the
668bd5abe19SDimitry Andric   // terminators.
669bd5abe19SDimitry Andric   LiveVariables *LV = P->getAnalysisIfAvailable<LiveVariables>();
670bd5abe19SDimitry Andric 
671bd5abe19SDimitry Andric   // Collect a list of virtual registers killed by the terminators.
672bd5abe19SDimitry Andric   SmallVector<unsigned, 4> KilledRegs;
673bd5abe19SDimitry Andric   if (LV)
674dff0c46cSDimitry Andric     for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
675dff0c46cSDimitry Andric          I != E; ++I) {
676bd5abe19SDimitry Andric       MachineInstr *MI = I;
677bd5abe19SDimitry Andric       for (MachineInstr::mop_iterator OI = MI->operands_begin(),
678bd5abe19SDimitry Andric            OE = MI->operands_end(); OI != OE; ++OI) {
679dff0c46cSDimitry Andric         if (!OI->isReg() || OI->getReg() == 0 ||
680dff0c46cSDimitry Andric             !OI->isUse() || !OI->isKill() || OI->isUndef())
681bd5abe19SDimitry Andric           continue;
682bd5abe19SDimitry Andric         unsigned Reg = OI->getReg();
683dff0c46cSDimitry Andric         if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
684bd5abe19SDimitry Andric             LV->getVarInfo(Reg).removeKill(MI)) {
685bd5abe19SDimitry Andric           KilledRegs.push_back(Reg);
686bd5abe19SDimitry Andric           DEBUG(dbgs() << "Removing terminator kill: " << *MI);
687bd5abe19SDimitry Andric           OI->setIsKill(false);
688bd5abe19SDimitry Andric         }
689bd5abe19SDimitry Andric       }
690bd5abe19SDimitry Andric     }
691bd5abe19SDimitry Andric 
692ffd1746dSEd Schouten   ReplaceUsesOfBlockWith(Succ, NMBB);
693ffd1746dSEd Schouten   updateTerminator();
694ffd1746dSEd Schouten 
695ffd1746dSEd Schouten   // Insert unconditional "jump Succ" instruction in NMBB if necessary.
696ffd1746dSEd Schouten   NMBB->addSuccessor(Succ);
697ffd1746dSEd Schouten   if (!NMBB->isLayoutSuccessor(Succ)) {
698ffd1746dSEd Schouten     Cond.clear();
699ffd1746dSEd Schouten     MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, Succ, NULL, Cond, dl);
700ffd1746dSEd Schouten   }
701ffd1746dSEd Schouten 
702ffd1746dSEd Schouten   // Fix PHI nodes in Succ so they refer to NMBB instead of this
703dff0c46cSDimitry Andric   for (MachineBasicBlock::instr_iterator
704dff0c46cSDimitry Andric          i = Succ->instr_begin(),e = Succ->instr_end();
705ffd1746dSEd Schouten        i != e && i->isPHI(); ++i)
706ffd1746dSEd Schouten     for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2)
707ffd1746dSEd Schouten       if (i->getOperand(ni+1).getMBB() == this)
708ffd1746dSEd Schouten         i->getOperand(ni+1).setMBB(NMBB);
709ffd1746dSEd Schouten 
7106122f3e6SDimitry Andric   // Inherit live-ins from the successor
7116122f3e6SDimitry Andric   for (MachineBasicBlock::livein_iterator I = Succ->livein_begin(),
7126122f3e6SDimitry Andric          E = Succ->livein_end(); I != E; ++I)
7136122f3e6SDimitry Andric     NMBB->addLiveIn(*I);
7146122f3e6SDimitry Andric 
715bd5abe19SDimitry Andric   // Update LiveVariables.
716dff0c46cSDimitry Andric   const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
717bd5abe19SDimitry Andric   if (LV) {
718bd5abe19SDimitry Andric     // Restore kills of virtual registers that were killed by the terminators.
719bd5abe19SDimitry Andric     while (!KilledRegs.empty()) {
720bd5abe19SDimitry Andric       unsigned Reg = KilledRegs.pop_back_val();
721dff0c46cSDimitry Andric       for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) {
722dff0c46cSDimitry Andric         if (!(--I)->addRegisterKilled(Reg, TRI, /* addIfNotFound= */ false))
723bd5abe19SDimitry Andric           continue;
724dff0c46cSDimitry Andric         if (TargetRegisterInfo::isVirtualRegister(Reg))
725bd5abe19SDimitry Andric           LV->getVarInfo(Reg).Kills.push_back(I);
726bd5abe19SDimitry Andric         DEBUG(dbgs() << "Restored terminator kill: " << *I);
727bd5abe19SDimitry Andric         break;
728bd5abe19SDimitry Andric       }
729bd5abe19SDimitry Andric     }
730bd5abe19SDimitry Andric     // Update relevant live-through information.
731ffd1746dSEd Schouten     LV->addNewBlock(NMBB, this, Succ);
732bd5abe19SDimitry Andric   }
733ffd1746dSEd Schouten 
734ffd1746dSEd Schouten   if (MachineDominatorTree *MDT =
735e580952dSDimitry Andric       P->getAnalysisIfAvailable<MachineDominatorTree>()) {
736e580952dSDimitry Andric     // Update dominator information.
737e580952dSDimitry Andric     MachineDomTreeNode *SucccDTNode = MDT->getNode(Succ);
738ffd1746dSEd Schouten 
739e580952dSDimitry Andric     bool IsNewIDom = true;
740e580952dSDimitry Andric     for (const_pred_iterator PI = Succ->pred_begin(), E = Succ->pred_end();
741e580952dSDimitry Andric          PI != E; ++PI) {
742e580952dSDimitry Andric       MachineBasicBlock *PredBB = *PI;
743e580952dSDimitry Andric       if (PredBB == NMBB)
744e580952dSDimitry Andric         continue;
745e580952dSDimitry Andric       if (!MDT->dominates(SucccDTNode, MDT->getNode(PredBB))) {
746e580952dSDimitry Andric         IsNewIDom = false;
747e580952dSDimitry Andric         break;
748e580952dSDimitry Andric       }
749e580952dSDimitry Andric     }
750e580952dSDimitry Andric 
751e580952dSDimitry Andric     // We know "this" dominates the newly created basic block.
752e580952dSDimitry Andric     MachineDomTreeNode *NewDTNode = MDT->addNewBlock(NMBB, this);
753e580952dSDimitry Andric 
754e580952dSDimitry Andric     // If all the other predecessors of "Succ" are dominated by "Succ" itself
755e580952dSDimitry Andric     // then the new block is the new immediate dominator of "Succ". Otherwise,
756e580952dSDimitry Andric     // the new block doesn't dominate anything.
757e580952dSDimitry Andric     if (IsNewIDom)
758e580952dSDimitry Andric       MDT->changeImmediateDominator(SucccDTNode, NewDTNode);
759e580952dSDimitry Andric   }
760e580952dSDimitry Andric 
761e580952dSDimitry Andric   if (MachineLoopInfo *MLI = P->getAnalysisIfAvailable<MachineLoopInfo>())
762ffd1746dSEd Schouten     if (MachineLoop *TIL = MLI->getLoopFor(this)) {
763ffd1746dSEd Schouten       // If one or the other blocks were not in a loop, the new block is not
764ffd1746dSEd Schouten       // either, and thus LI doesn't need to be updated.
765ffd1746dSEd Schouten       if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) {
766ffd1746dSEd Schouten         if (TIL == DestLoop) {
767ffd1746dSEd Schouten           // Both in the same loop, the NMBB joins loop.
768ffd1746dSEd Schouten           DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase());
769ffd1746dSEd Schouten         } else if (TIL->contains(DestLoop)) {
770ffd1746dSEd Schouten           // Edge from an outer loop to an inner loop.  Add to the outer loop.
771ffd1746dSEd Schouten           TIL->addBasicBlockToLoop(NMBB, MLI->getBase());
772ffd1746dSEd Schouten         } else if (DestLoop->contains(TIL)) {
773ffd1746dSEd Schouten           // Edge from an inner loop to an outer loop.  Add to the outer loop.
774ffd1746dSEd Schouten           DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase());
775ffd1746dSEd Schouten         } else {
776ffd1746dSEd Schouten           // Edge from two loops with no containment relation.  Because these
777ffd1746dSEd Schouten           // are natural loops, we know that the destination block must be the
778ffd1746dSEd Schouten           // header of its loop (adding a branch into a loop elsewhere would
779ffd1746dSEd Schouten           // create an irreducible loop).
780ffd1746dSEd Schouten           assert(DestLoop->getHeader() == Succ &&
781ffd1746dSEd Schouten                  "Should not create irreducible loops!");
782ffd1746dSEd Schouten           if (MachineLoop *P = DestLoop->getParentLoop())
783ffd1746dSEd Schouten             P->addBasicBlockToLoop(NMBB, MLI->getBase());
784ffd1746dSEd Schouten         }
785ffd1746dSEd Schouten       }
786ffd1746dSEd Schouten     }
787ffd1746dSEd Schouten 
788ffd1746dSEd Schouten   return NMBB;
789ffd1746dSEd Schouten }
790ffd1746dSEd Schouten 
791dff0c46cSDimitry Andric MachineBasicBlock::iterator
792dff0c46cSDimitry Andric MachineBasicBlock::erase(MachineBasicBlock::iterator I) {
793dff0c46cSDimitry Andric   if (I->isBundle()) {
794dff0c46cSDimitry Andric     MachineBasicBlock::iterator E = llvm::next(I);
795dff0c46cSDimitry Andric     return Insts.erase(I.getInstrIterator(), E.getInstrIterator());
796dff0c46cSDimitry Andric   }
797dff0c46cSDimitry Andric 
798dff0c46cSDimitry Andric   return Insts.erase(I.getInstrIterator());
799dff0c46cSDimitry Andric }
800dff0c46cSDimitry Andric 
801dff0c46cSDimitry Andric MachineInstr *MachineBasicBlock::remove(MachineInstr *I) {
802dff0c46cSDimitry Andric   if (I->isBundle()) {
803dff0c46cSDimitry Andric     instr_iterator MII = llvm::next(I);
804dff0c46cSDimitry Andric     iterator E = end();
805dff0c46cSDimitry Andric     while (MII != E && MII->isInsideBundle()) {
806dff0c46cSDimitry Andric       MachineInstr *MI = &*MII++;
807dff0c46cSDimitry Andric       Insts.remove(MI);
808dff0c46cSDimitry Andric     }
809dff0c46cSDimitry Andric   }
810dff0c46cSDimitry Andric 
811dff0c46cSDimitry Andric   return Insts.remove(I);
812dff0c46cSDimitry Andric }
813dff0c46cSDimitry Andric 
814dff0c46cSDimitry Andric void MachineBasicBlock::splice(MachineBasicBlock::iterator where,
815dff0c46cSDimitry Andric                                MachineBasicBlock *Other,
816dff0c46cSDimitry Andric                                MachineBasicBlock::iterator From) {
817dff0c46cSDimitry Andric   if (From->isBundle()) {
818dff0c46cSDimitry Andric     MachineBasicBlock::iterator To = llvm::next(From);
819dff0c46cSDimitry Andric     Insts.splice(where.getInstrIterator(), Other->Insts,
820dff0c46cSDimitry Andric                  From.getInstrIterator(), To.getInstrIterator());
821dff0c46cSDimitry Andric     return;
822dff0c46cSDimitry Andric   }
823dff0c46cSDimitry Andric 
824dff0c46cSDimitry Andric   Insts.splice(where.getInstrIterator(), Other->Insts, From.getInstrIterator());
825dff0c46cSDimitry Andric }
826dff0c46cSDimitry Andric 
827f22ef01cSRoman Divacky /// removeFromParent - This method unlinks 'this' from the containing function,
828f22ef01cSRoman Divacky /// and returns it, but does not delete it.
829f22ef01cSRoman Divacky MachineBasicBlock *MachineBasicBlock::removeFromParent() {
830f22ef01cSRoman Divacky   assert(getParent() && "Not embedded in a function!");
831f22ef01cSRoman Divacky   getParent()->remove(this);
832f22ef01cSRoman Divacky   return this;
833f22ef01cSRoman Divacky }
834f22ef01cSRoman Divacky 
835f22ef01cSRoman Divacky 
836f22ef01cSRoman Divacky /// eraseFromParent - This method unlinks 'this' from the containing function,
837f22ef01cSRoman Divacky /// and deletes it.
838f22ef01cSRoman Divacky void MachineBasicBlock::eraseFromParent() {
839f22ef01cSRoman Divacky   assert(getParent() && "Not embedded in a function!");
840f22ef01cSRoman Divacky   getParent()->erase(this);
841f22ef01cSRoman Divacky }
842f22ef01cSRoman Divacky 
843f22ef01cSRoman Divacky 
844f22ef01cSRoman Divacky /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
845f22ef01cSRoman Divacky /// 'Old', change the code and CFG so that it branches to 'New' instead.
846f22ef01cSRoman Divacky void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old,
847f22ef01cSRoman Divacky                                                MachineBasicBlock *New) {
848f22ef01cSRoman Divacky   assert(Old != New && "Cannot replace self with self!");
849f22ef01cSRoman Divacky 
850dff0c46cSDimitry Andric   MachineBasicBlock::instr_iterator I = instr_end();
851dff0c46cSDimitry Andric   while (I != instr_begin()) {
852f22ef01cSRoman Divacky     --I;
853dff0c46cSDimitry Andric     if (!I->isTerminator()) break;
854f22ef01cSRoman Divacky 
855f22ef01cSRoman Divacky     // Scan the operands of this machine instruction, replacing any uses of Old
856f22ef01cSRoman Divacky     // with New.
857f22ef01cSRoman Divacky     for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
858f22ef01cSRoman Divacky       if (I->getOperand(i).isMBB() &&
859f22ef01cSRoman Divacky           I->getOperand(i).getMBB() == Old)
860f22ef01cSRoman Divacky         I->getOperand(i).setMBB(New);
861f22ef01cSRoman Divacky   }
862f22ef01cSRoman Divacky 
863f22ef01cSRoman Divacky   // Update the successor information.
86417a519f9SDimitry Andric   replaceSuccessor(Old, New);
865f22ef01cSRoman Divacky }
866f22ef01cSRoman Divacky 
867f22ef01cSRoman Divacky /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the
868f22ef01cSRoman Divacky /// CFG to be inserted.  If we have proven that MBB can only branch to DestA and
869f22ef01cSRoman Divacky /// DestB, remove any other MBB successors from the CFG.  DestA and DestB can be
870f22ef01cSRoman Divacky /// null.
871f22ef01cSRoman Divacky ///
872f22ef01cSRoman Divacky /// Besides DestA and DestB, retain other edges leading to LandingPads
873f22ef01cSRoman Divacky /// (currently there can be only one; we don't check or require that here).
874f22ef01cSRoman Divacky /// Note it is possible that DestA and/or DestB are LandingPads.
875f22ef01cSRoman Divacky bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA,
876f22ef01cSRoman Divacky                                              MachineBasicBlock *DestB,
877f22ef01cSRoman Divacky                                              bool isCond) {
878f22ef01cSRoman Divacky   // The values of DestA and DestB frequently come from a call to the
879f22ef01cSRoman Divacky   // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial
880f22ef01cSRoman Divacky   // values from there.
881f22ef01cSRoman Divacky   //
882f22ef01cSRoman Divacky   // 1. If both DestA and DestB are null, then the block ends with no branches
883f22ef01cSRoman Divacky   //    (it falls through to its successor).
884f22ef01cSRoman Divacky   // 2. If DestA is set, DestB is null, and isCond is false, then the block ends
885f22ef01cSRoman Divacky   //    with only an unconditional branch.
886f22ef01cSRoman Divacky   // 3. If DestA is set, DestB is null, and isCond is true, then the block ends
887f22ef01cSRoman Divacky   //    with a conditional branch that falls through to a successor (DestB).
888f22ef01cSRoman Divacky   // 4. If DestA and DestB is set and isCond is true, then the block ends with a
889f22ef01cSRoman Divacky   //    conditional branch followed by an unconditional branch. DestA is the
890f22ef01cSRoman Divacky   //    'true' destination and DestB is the 'false' destination.
891f22ef01cSRoman Divacky 
892f22ef01cSRoman Divacky   bool Changed = false;
893f22ef01cSRoman Divacky 
894f22ef01cSRoman Divacky   MachineFunction::iterator FallThru =
895f22ef01cSRoman Divacky     llvm::next(MachineFunction::iterator(this));
896f22ef01cSRoman Divacky 
897f22ef01cSRoman Divacky   if (DestA == 0 && DestB == 0) {
898f22ef01cSRoman Divacky     // Block falls through to successor.
899f22ef01cSRoman Divacky     DestA = FallThru;
900f22ef01cSRoman Divacky     DestB = FallThru;
901f22ef01cSRoman Divacky   } else if (DestA != 0 && DestB == 0) {
902f22ef01cSRoman Divacky     if (isCond)
903f22ef01cSRoman Divacky       // Block ends in conditional jump that falls through to successor.
904f22ef01cSRoman Divacky       DestB = FallThru;
905f22ef01cSRoman Divacky   } else {
906f22ef01cSRoman Divacky     assert(DestA && DestB && isCond &&
907f22ef01cSRoman Divacky            "CFG in a bad state. Cannot correct CFG edges");
908f22ef01cSRoman Divacky   }
909f22ef01cSRoman Divacky 
910f22ef01cSRoman Divacky   // Remove superfluous edges. I.e., those which aren't destinations of this
911f22ef01cSRoman Divacky   // basic block, duplicate edges, or landing pads.
912f22ef01cSRoman Divacky   SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs;
913f22ef01cSRoman Divacky   MachineBasicBlock::succ_iterator SI = succ_begin();
914f22ef01cSRoman Divacky   while (SI != succ_end()) {
915f22ef01cSRoman Divacky     const MachineBasicBlock *MBB = *SI;
916f22ef01cSRoman Divacky     if (!SeenMBBs.insert(MBB) ||
917f22ef01cSRoman Divacky         (MBB != DestA && MBB != DestB && !MBB->isLandingPad())) {
918f22ef01cSRoman Divacky       // This is a superfluous edge, remove it.
919f22ef01cSRoman Divacky       SI = removeSuccessor(SI);
920f22ef01cSRoman Divacky       Changed = true;
921f22ef01cSRoman Divacky     } else {
922f22ef01cSRoman Divacky       ++SI;
923f22ef01cSRoman Divacky     }
924f22ef01cSRoman Divacky   }
925f22ef01cSRoman Divacky 
926f22ef01cSRoman Divacky   return Changed;
927f22ef01cSRoman Divacky }
928f22ef01cSRoman Divacky 
929f22ef01cSRoman Divacky /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
930f22ef01cSRoman Divacky /// any DBG_VALUE instructions.  Return UnknownLoc if there is none.
931f22ef01cSRoman Divacky DebugLoc
932dff0c46cSDimitry Andric MachineBasicBlock::findDebugLoc(instr_iterator MBBI) {
933f22ef01cSRoman Divacky   DebugLoc DL;
934dff0c46cSDimitry Andric   instr_iterator E = instr_end();
935dff0c46cSDimitry Andric   if (MBBI == E)
936dff0c46cSDimitry Andric     return DL;
937dff0c46cSDimitry Andric 
938f22ef01cSRoman Divacky   // Skip debug declarations, we don't want a DebugLoc from them.
939dff0c46cSDimitry Andric   while (MBBI != E && MBBI->isDebugValue())
940dff0c46cSDimitry Andric     MBBI++;
941dff0c46cSDimitry Andric   if (MBBI != E)
942dff0c46cSDimitry Andric     DL = MBBI->getDebugLoc();
943f22ef01cSRoman Divacky   return DL;
944f22ef01cSRoman Divacky }
945f22ef01cSRoman Divacky 
94617a519f9SDimitry Andric /// getSuccWeight - Return weight of the edge from this block to MBB.
94717a519f9SDimitry Andric ///
9483861d79fSDimitry Andric uint32_t MachineBasicBlock::getSuccWeight(const_succ_iterator Succ) const {
94917a519f9SDimitry Andric   if (Weights.empty())
95017a519f9SDimitry Andric     return 0;
95117a519f9SDimitry Andric 
9523861d79fSDimitry Andric   return *getWeightIterator(Succ);
95317a519f9SDimitry Andric }
95417a519f9SDimitry Andric 
95517a519f9SDimitry Andric /// getWeightIterator - Return wight iterator corresonding to the I successor
95617a519f9SDimitry Andric /// iterator
95717a519f9SDimitry Andric MachineBasicBlock::weight_iterator MachineBasicBlock::
95817a519f9SDimitry Andric getWeightIterator(MachineBasicBlock::succ_iterator I) {
95917a519f9SDimitry Andric   assert(Weights.size() == Successors.size() && "Async weight list!");
96017a519f9SDimitry Andric   size_t index = std::distance(Successors.begin(), I);
96117a519f9SDimitry Andric   assert(index < Weights.size() && "Not a current successor!");
96217a519f9SDimitry Andric   return Weights.begin() + index;
96317a519f9SDimitry Andric }
96417a519f9SDimitry Andric 
965dff0c46cSDimitry Andric /// getWeightIterator - Return wight iterator corresonding to the I successor
966dff0c46cSDimitry Andric /// iterator
967dff0c46cSDimitry Andric MachineBasicBlock::const_weight_iterator MachineBasicBlock::
968dff0c46cSDimitry Andric getWeightIterator(MachineBasicBlock::const_succ_iterator I) const {
969dff0c46cSDimitry Andric   assert(Weights.size() == Successors.size() && "Async weight list!");
970dff0c46cSDimitry Andric   const size_t index = std::distance(Successors.begin(), I);
971dff0c46cSDimitry Andric   assert(index < Weights.size() && "Not a current successor!");
972dff0c46cSDimitry Andric   return Weights.begin() + index;
973dff0c46cSDimitry Andric }
974dff0c46cSDimitry Andric 
9753861d79fSDimitry Andric /// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed
9763861d79fSDimitry Andric /// as of just before "MI".
9773861d79fSDimitry Andric ///
9783861d79fSDimitry Andric /// Search is localised to a neighborhood of
9793861d79fSDimitry Andric /// Neighborhood instructions before (searching for defs or kills) and N
9803861d79fSDimitry Andric /// instructions after (searching just for defs) MI.
9813861d79fSDimitry Andric MachineBasicBlock::LivenessQueryResult
9823861d79fSDimitry Andric MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI,
9833861d79fSDimitry Andric                                            unsigned Reg, MachineInstr *MI,
9843861d79fSDimitry Andric                                            unsigned Neighborhood) {
9853861d79fSDimitry Andric 
9863861d79fSDimitry Andric   unsigned N = Neighborhood;
9873861d79fSDimitry Andric   MachineBasicBlock *MBB = MI->getParent();
9883861d79fSDimitry Andric 
9893861d79fSDimitry Andric   // Start by searching backwards from MI, looking for kills, reads or defs.
9903861d79fSDimitry Andric 
9913861d79fSDimitry Andric   MachineBasicBlock::iterator I(MI);
9923861d79fSDimitry Andric   // If this is the first insn in the block, don't search backwards.
9933861d79fSDimitry Andric   if (I != MBB->begin()) {
9943861d79fSDimitry Andric     do {
9953861d79fSDimitry Andric       --I;
9963861d79fSDimitry Andric 
9973861d79fSDimitry Andric       MachineOperandIteratorBase::PhysRegInfo Analysis =
9983861d79fSDimitry Andric         MIOperands(I).analyzePhysReg(Reg, TRI);
9993861d79fSDimitry Andric 
10003861d79fSDimitry Andric       if (Analysis.Kills)
10013861d79fSDimitry Andric         // Register killed, so isn't live.
10023861d79fSDimitry Andric         return LQR_Dead;
10033861d79fSDimitry Andric 
10043861d79fSDimitry Andric       else if (Analysis.DefinesOverlap || Analysis.ReadsOverlap)
10053861d79fSDimitry Andric         // Defined or read without a previous kill - live.
10063861d79fSDimitry Andric         return (Analysis.Defines || Analysis.Reads) ?
10073861d79fSDimitry Andric           LQR_Live : LQR_OverlappingLive;
10083861d79fSDimitry Andric 
10093861d79fSDimitry Andric     } while (I != MBB->begin() && --N > 0);
10103861d79fSDimitry Andric   }
10113861d79fSDimitry Andric 
10123861d79fSDimitry Andric   // Did we get to the start of the block?
10133861d79fSDimitry Andric   if (I == MBB->begin()) {
10143861d79fSDimitry Andric     // If so, the register's state is definitely defined by the live-in state.
10153861d79fSDimitry Andric     for (MCRegAliasIterator RAI(Reg, TRI, /*IncludeSelf=*/true);
10163861d79fSDimitry Andric          RAI.isValid(); ++RAI) {
10173861d79fSDimitry Andric       if (MBB->isLiveIn(*RAI))
10183861d79fSDimitry Andric         return (*RAI == Reg) ? LQR_Live : LQR_OverlappingLive;
10193861d79fSDimitry Andric     }
10203861d79fSDimitry Andric 
10213861d79fSDimitry Andric     return LQR_Dead;
10223861d79fSDimitry Andric   }
10233861d79fSDimitry Andric 
10243861d79fSDimitry Andric   N = Neighborhood;
10253861d79fSDimitry Andric 
10263861d79fSDimitry Andric   // Try searching forwards from MI, looking for reads or defs.
10273861d79fSDimitry Andric   I = MachineBasicBlock::iterator(MI);
10283861d79fSDimitry Andric   // If this is the last insn in the block, don't search forwards.
10293861d79fSDimitry Andric   if (I != MBB->end()) {
10303861d79fSDimitry Andric     for (++I; I != MBB->end() && N > 0; ++I, --N) {
10313861d79fSDimitry Andric       MachineOperandIteratorBase::PhysRegInfo Analysis =
10323861d79fSDimitry Andric         MIOperands(I).analyzePhysReg(Reg, TRI);
10333861d79fSDimitry Andric 
10343861d79fSDimitry Andric       if (Analysis.ReadsOverlap)
10353861d79fSDimitry Andric         // Used, therefore must have been live.
10363861d79fSDimitry Andric         return (Analysis.Reads) ?
10373861d79fSDimitry Andric           LQR_Live : LQR_OverlappingLive;
10383861d79fSDimitry Andric 
10393861d79fSDimitry Andric       else if (Analysis.DefinesOverlap)
10403861d79fSDimitry Andric         // Defined (but not read) therefore cannot have been live.
10413861d79fSDimitry Andric         return LQR_Dead;
10423861d79fSDimitry Andric     }
10433861d79fSDimitry Andric   }
10443861d79fSDimitry Andric 
10453861d79fSDimitry Andric   // At this point we have no idea of the liveness of the register.
10463861d79fSDimitry Andric   return LQR_Unknown;
10473861d79fSDimitry Andric }
10483861d79fSDimitry Andric 
1049f22ef01cSRoman Divacky void llvm::WriteAsOperand(raw_ostream &OS, const MachineBasicBlock *MBB,
1050f22ef01cSRoman Divacky                           bool t) {
1051f22ef01cSRoman Divacky   OS << "BB#" << MBB->getNumber();
1052f22ef01cSRoman Divacky }
1053f22ef01cSRoman Divacky 
1054