blob: 3d754366ee454f4e80373f6e3c996ba4d5ffe1cd [file] [log] [blame]
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +00001//===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Collect the sequence of machine instructions for a basic block.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MachineBasicBlock.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/ADT/SmallPtrSet.h"
16#include "llvm/ADT/SmallString.h"
17#include "llvm/Assembly/Writer.h"
Cameron Zwarich8597c142013-02-11 09:24:47 +000018#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Dan Gohman853d3fb2010-06-22 17:25:57 +000019#include "llvm/CodeGen/LiveVariables.h"
20#include "llvm/CodeGen/MachineDominators.h"
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000021#include "llvm/CodeGen/MachineFunction.h"
Dan Gohman853d3fb2010-06-22 17:25:57 +000022#include "llvm/CodeGen/MachineLoopInfo.h"
Cameron Zwarich8597c142013-02-11 09:24:47 +000023#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +000024#include "llvm/CodeGen/SlotIndexes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000025#include "llvm/IR/BasicBlock.h"
26#include "llvm/IR/DataLayout.h"
Chris Lattnerf71cb012010-01-26 04:55:51 +000027#include "llvm/MC/MCAsmInfo.h"
28#include "llvm/MC/MCContext.h"
David Greenedbdbbd92010-01-04 23:22:07 +000029#include "llvm/Support/Debug.h"
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000030#include "llvm/Support/LeakDetector.h"
Daniel Dunbar1cd1d982009-07-24 10:36:58 +000031#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000032#include "llvm/Target/TargetInstrInfo.h"
33#include "llvm/Target/TargetMachine.h"
34#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner52c09d72004-10-26 15:43:42 +000035#include <algorithm>
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000036using namespace llvm;
37
Dan Gohman8e5f2c62008-07-07 23:14:23 +000038MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb)
Dan Gohman8c2b5252009-10-30 01:27:03 +000039 : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false),
40 AddressTaken(false) {
Dan Gohmanfed90b62008-07-28 21:51:04 +000041 Insts.Parent = this;
Tanya Lattner17fb34b2004-05-24 07:14:35 +000042}
Tanya Lattner17fb34b2004-05-24 07:14:35 +000043
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000044MachineBasicBlock::~MachineBasicBlock() {
45 LeakDetector::removeGarbageObject(this);
46}
47
Chris Lattnerf71cb012010-01-26 04:55:51 +000048/// getSymbol - Return the MCSymbol for this basic block.
49///
Chris Lattner1b2eb0e2010-03-13 21:04:28 +000050MCSymbol *MachineBasicBlock::getSymbol() const {
Chris Lattnerf71cb012010-01-26 04:55:51 +000051 const MachineFunction *MF = getParent();
Chris Lattner1b2eb0e2010-03-13 21:04:28 +000052 MCContext &Ctx = MF->getContext();
53 const char *Prefix = Ctx.getAsmInfo().getPrivateGlobalPrefix();
Chris Lattner9b97a732010-03-30 18:10:53 +000054 return Ctx.GetOrCreateSymbol(Twine(Prefix) + "BB" +
55 Twine(MF->getFunctionNumber()) + "_" +
56 Twine(getNumber()));
Chris Lattnerf71cb012010-01-26 04:55:51 +000057}
58
59
Chris Lattner6371ed52009-08-23 00:35:30 +000060raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) {
Daniel Dunbar1cd1d982009-07-24 10:36:58 +000061 MBB.print(OS);
62 return OS;
63}
Tanya Lattner17fb34b2004-05-24 07:14:35 +000064
Jakub Staszak12af5ff2011-06-16 18:01:17 +000065/// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the
Chris Lattner62ed6b92008-01-01 01:12:31 +000066/// parent pointer of the MBB, the MBB numbering, and any instructions in the
67/// MBB to be on the right operand list for registers.
68///
69/// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
70/// gets the next available unique MBB number. If it is removed from a
71/// MachineFunction, it goes back to being #-1.
Chris Lattner6371ed52009-08-23 00:35:30 +000072void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +000073 MachineFunction &MF = *N->getParent();
74 N->Number = MF.addToMBBNumbering(N);
Chris Lattner62ed6b92008-01-01 01:12:31 +000075
76 // Make sure the instructions have their operands in the reginfo lists.
Dan Gohman8e5f2c62008-07-07 23:14:23 +000077 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Evan Chengddfd1372011-12-14 02:11:42 +000078 for (MachineBasicBlock::instr_iterator
79 I = N->instr_begin(), E = N->instr_end(); I != E; ++I)
Chris Lattner62ed6b92008-01-01 01:12:31 +000080 I->AddRegOperandsToUseLists(RegInfo);
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000081
82 LeakDetector::removeGarbageObject(N);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000083}
84
Chris Lattner6371ed52009-08-23 00:35:30 +000085void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) {
Chris Lattnerf20c1a42007-12-31 04:56:33 +000086 N->getParent()->removeFromMBBNumbering(N->Number);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000087 N->Number = -1;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000088 LeakDetector::addGarbageObject(N);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000089}
90
Chris Lattner5e61fa92004-02-19 16:13:54 +000091
Chris Lattner62ed6b92008-01-01 01:12:31 +000092/// addNodeToList (MI) - When we add an instruction to a basic block
93/// list, we update its parent pointer and add its operands from reg use/def
94/// lists if appropriate.
Chris Lattner6371ed52009-08-23 00:35:30 +000095void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) {
Chris Lattnerf20c1a42007-12-31 04:56:33 +000096 assert(N->getParent() == 0 && "machine instruction already in a basic block");
Dan Gohman8e5f2c62008-07-07 23:14:23 +000097 N->setParent(Parent);
Jakub Staszak12af5ff2011-06-16 18:01:17 +000098
Dan Gohman8e5f2c62008-07-07 23:14:23 +000099 // Add the instruction's register operands to their corresponding
100 // use/def lists.
101 MachineFunction *MF = Parent->getParent();
102 N->AddRegOperandsToUseLists(MF->getRegInfo());
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000103
104 LeakDetector::removeGarbageObject(N);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000105}
106
Chris Lattner62ed6b92008-01-01 01:12:31 +0000107/// removeNodeFromList (MI) - When we remove an instruction from a basic block
108/// list, we update its parent pointer and remove its operands from reg use/def
109/// lists if appropriate.
Chris Lattner6371ed52009-08-23 00:35:30 +0000110void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) {
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000111 assert(N->getParent() != 0 && "machine instruction not in a basic block");
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000112
113 // Remove from the use/def lists.
Jakob Stoklund Olesenff2b99a2012-08-09 22:49:37 +0000114 if (MachineFunction *MF = N->getParent()->getParent())
115 N->RemoveRegOperandsFromUseLists(MF->getRegInfo());
Jakub Staszak12af5ff2011-06-16 18:01:17 +0000116
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000117 N->setParent(0);
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000118
119 LeakDetector::addGarbageObject(N);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000120}
121
Chris Lattner62ed6b92008-01-01 01:12:31 +0000122/// transferNodesFromList (MI) - When moving a range of instructions from one
123/// MBB list to another, we need to update the parent pointers and the use/def
124/// lists.
Chris Lattner6371ed52009-08-23 00:35:30 +0000125void ilist_traits<MachineInstr>::
126transferNodesFromList(ilist_traits<MachineInstr> &fromList,
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000127 ilist_iterator<MachineInstr> first,
128 ilist_iterator<MachineInstr> last) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000129 assert(Parent->getParent() == fromList.Parent->getParent() &&
130 "MachineInstr parent mismatch!");
131
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000132 // Splice within the same MBB -> no change.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000133 if (Parent == fromList.Parent) return;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000134
135 // If splicing between two blocks within the same function, just update the
136 // parent pointers.
Dan Gohmanfed90b62008-07-28 21:51:04 +0000137 for (; first != last; ++first)
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000138 first->setParent(Parent);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000139}
140
Dan Gohmanfed90b62008-07-28 21:51:04 +0000141void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000142 assert(!MI->getParent() && "MI is still in a block!");
143 Parent->getParent()->DeleteMachineInstr(MI);
144}
145
Dan Gohmand463a742010-07-07 14:33:51 +0000146MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() {
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000147 instr_iterator I = instr_begin(), E = instr_end();
148 while (I != E && I->isPHI())
Dan Gohmand463a742010-07-07 14:33:51 +0000149 ++I;
Akira Hatanakaaf808222012-10-26 17:11:42 +0000150 assert((I == E || !I->isInsideBundle()) &&
151 "First non-phi MI cannot be inside a bundle!");
Dan Gohmand463a742010-07-07 14:33:51 +0000152 return I;
153}
154
Jakob Stoklund Olesen92095e72010-10-30 01:26:14 +0000155MachineBasicBlock::iterator
156MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) {
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000157 iterator E = end();
158 while (I != E && (I->isPHI() || I->isLabel() || I->isDebugValue()))
Jakob Stoklund Olesen92095e72010-10-30 01:26:14 +0000159 ++I;
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000160 // FIXME: This needs to change if we wish to bundle labels / dbg_values
161 // inside the bundle.
Akira Hatanakaaf808222012-10-26 17:11:42 +0000162 assert((I == E || !I->isInsideBundle()) &&
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000163 "First non-phi / non-label instruction is inside a bundle!");
Jakob Stoklund Olesen92095e72010-10-30 01:26:14 +0000164 return I;
165}
166
Chris Lattner52c09d72004-10-26 15:43:42 +0000167MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000168 iterator B = begin(), E = end(), I = E;
169 while (I != B && ((--I)->isTerminator() || I->isDebugValue()))
Jakob Stoklund Olesenb6436e52011-01-14 02:12:54 +0000170 ; /*noop */
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000171 while (I != E && !I->isTerminator())
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000172 ++I;
173 return I;
174}
175
176MachineBasicBlock::const_iterator
177MachineBasicBlock::getFirstTerminator() const {
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000178 const_iterator B = begin(), E = end(), I = E;
179 while (I != B && ((--I)->isTerminator() || I->isDebugValue()))
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000180 ; /*noop */
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000181 while (I != E && !I->isTerminator())
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000182 ++I;
183 return I;
184}
185
Evan Chengddfd1372011-12-14 02:11:42 +0000186MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() {
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000187 instr_iterator B = instr_begin(), E = instr_end(), I = E;
188 while (I != B && ((--I)->isTerminator() || I->isDebugValue()))
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000189 ; /*noop */
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000190 while (I != E && !I->isTerminator())
Jakob Stoklund Olesen04223902011-01-14 06:33:45 +0000191 ++I;
Jakob Stoklund Olesenb6436e52011-01-14 02:12:54 +0000192 return I;
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +0000193}
194
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000195MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() {
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000196 // Skip over end-of-block dbg_value instructions.
Evan Chengddfd1372011-12-14 02:11:42 +0000197 instr_iterator B = instr_begin(), I = instr_end();
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000198 while (I != B) {
199 --I;
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000200 // Return instruction that starts a bundle.
201 if (I->isDebugValue() || I->isInsideBundle())
202 continue;
203 return I;
204 }
205 // The block is all debug values.
206 return end();
207}
208
209MachineBasicBlock::const_iterator
210MachineBasicBlock::getLastNonDebugInstr() const {
211 // Skip over end-of-block dbg_value instructions.
Evan Chengddfd1372011-12-14 02:11:42 +0000212 const_instr_iterator B = instr_begin(), I = instr_end();
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000213 while (I != B) {
214 --I;
215 // Return instruction that starts a bundle.
216 if (I->isDebugValue() || I->isInsideBundle())
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000217 continue;
218 return I;
219 }
220 // The block is all debug values.
221 return end();
222}
223
Jakob Stoklund Olesencb640472011-02-04 19:33:11 +0000224const MachineBasicBlock *MachineBasicBlock::getLandingPadSuccessor() const {
225 // A block with a landing pad successor only has one other successor.
226 if (succ_size() > 2)
227 return 0;
228 for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I)
229 if ((*I)->isLandingPad())
230 return *I;
231 return 0;
232}
233
Manman Renb720be62012-09-11 22:23:19 +0000234#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattner52c09d72004-10-26 15:43:42 +0000235void MachineBasicBlock::dump() const {
David Greenedbdbbd92010-01-04 23:22:07 +0000236 print(dbgs());
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000237}
Manman Ren77e300e2012-09-06 19:06:06 +0000238#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000239
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000240StringRef MachineBasicBlock::getName() const {
241 if (const BasicBlock *LBB = getBasicBlock())
242 return LBB->getName();
243 else
244 return "(null)";
245}
246
Andrew Trick8ceaa662012-03-07 00:18:18 +0000247/// Return a hopefully unique identifier for this block.
248std::string MachineBasicBlock::getFullName() const {
249 std::string Name;
250 if (getParent())
Craig Topper96601ca2012-08-22 06:07:19 +0000251 Name = (getParent()->getName() + ":").str();
Andrew Trick8ceaa662012-03-07 00:18:18 +0000252 if (getBasicBlock())
253 Name += getBasicBlock()->getName();
254 else
255 Name += (Twine("BB") + Twine(getNumber())).str();
256 return Name;
257}
258
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000259void MachineBasicBlock::print(raw_ostream &OS, SlotIndexes *Indexes) const {
Evan Cheng13d82852007-02-10 02:38:19 +0000260 const MachineFunction *MF = getParent();
Chris Lattner6371ed52009-08-23 00:35:30 +0000261 if (!MF) {
Chris Lattner52c09d72004-10-26 15:43:42 +0000262 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
263 << " is null\n";
Tanya Lattner792699c2004-05-24 06:11:51 +0000264 return;
265 }
Alkis Evlogimenosa6382862004-09-05 18:39:20 +0000266
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000267 if (Indexes)
268 OS << Indexes->getMBBStartIdx(this) << '\t';
269
Dan Gohman0ba90f32009-10-31 20:19:03 +0000270 OS << "BB#" << getNumber() << ": ";
271
272 const char *Comma = "";
273 if (const BasicBlock *LBB = getBasicBlock()) {
274 OS << Comma << "derived from LLVM BB ";
275 WriteAsOperand(OS, LBB, /*PrintType=*/false);
276 Comma = ", ";
277 }
278 if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; }
279 if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; }
Bill Wendling4b8e1fd2012-06-15 19:30:42 +0000280 if (Alignment)
Jakob Stoklund Olesenf2e94452011-12-06 21:08:39 +0000281 OS << Comma << "Align " << Alignment << " (" << (1u << Alignment)
282 << " bytes)";
Jakob Stoklund Olesenf2e94452011-12-06 21:08:39 +0000283
Chris Lattner6371ed52009-08-23 00:35:30 +0000284 OS << '\n';
Evan Cheng13d82852007-02-10 02:38:19 +0000285
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000286 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
Dan Gohmancb406c22007-10-03 19:26:29 +0000287 if (!livein_empty()) {
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000288 if (Indexes) OS << '\t';
Dan Gohman0ba90f32009-10-31 20:19:03 +0000289 OS << " Live Ins:";
Dan Gohman81bf03e2010-04-13 16:57:55 +0000290 for (livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I)
Jakob Stoklund Olesen668c9e32011-01-13 00:57:35 +0000291 OS << ' ' << PrintReg(*I, TRI);
Chris Lattner6371ed52009-08-23 00:35:30 +0000292 OS << '\n';
Evan Cheng13d82852007-02-10 02:38:19 +0000293 }
Chris Lattner681764b2006-09-26 03:41:59 +0000294 // Print the preds of this block according to the CFG.
295 if (!pred_empty()) {
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000296 if (Indexes) OS << '\t';
Chris Lattner681764b2006-09-26 03:41:59 +0000297 OS << " Predecessors according to CFG:";
298 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000299 OS << " BB#" << (*PI)->getNumber();
Chris Lattner6371ed52009-08-23 00:35:30 +0000300 OS << '\n';
Chris Lattner681764b2006-09-26 03:41:59 +0000301 }
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000302
Evan Chengddfd1372011-12-14 02:11:42 +0000303 for (const_instr_iterator I = instr_begin(); I != instr_end(); ++I) {
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000304 if (Indexes) {
305 if (Indexes->hasIndex(I))
306 OS << Indexes->getInstructionIndex(I);
307 OS << '\t';
308 }
Chris Lattner2d8e3d22009-08-23 00:47:04 +0000309 OS << '\t';
Evan Chengddfd1372011-12-14 02:11:42 +0000310 if (I->isInsideBundle())
311 OS << " * ";
Alkis Evlogimenosa6382862004-09-05 18:39:20 +0000312 I->print(OS, &getParent()->getTarget());
313 }
Chris Lattner380ae492005-04-01 06:48:38 +0000314
315 // Print the successors of this block according to the CFG.
316 if (!succ_empty()) {
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000317 if (Indexes) OS << '\t';
Chris Lattner380ae492005-04-01 06:48:38 +0000318 OS << " Successors according to CFG:";
Jakob Stoklund Olesencb6889b2012-08-13 23:13:23 +0000319 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000320 OS << " BB#" << (*SI)->getNumber();
Jakob Stoklund Olesencb6889b2012-08-13 23:13:23 +0000321 if (!Weights.empty())
322 OS << '(' << *getWeightIterator(SI) << ')';
323 }
Chris Lattner6371ed52009-08-23 00:35:30 +0000324 OS << '\n';
Chris Lattner380ae492005-04-01 06:48:38 +0000325 }
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000326}
Chris Lattner52c09d72004-10-26 15:43:42 +0000327
Evan Chengb371f452007-02-19 21:49:54 +0000328void MachineBasicBlock::removeLiveIn(unsigned Reg) {
Dan Gohman81bf03e2010-04-13 16:57:55 +0000329 std::vector<unsigned>::iterator I =
330 std::find(LiveIns.begin(), LiveIns.end(), Reg);
Jakob Stoklund Olesen78836f02012-03-28 20:11:42 +0000331 if (I != LiveIns.end())
332 LiveIns.erase(I);
Evan Chengb371f452007-02-19 21:49:54 +0000333}
334
Evan Chenga971dbd2008-04-24 09:06:33 +0000335bool MachineBasicBlock::isLiveIn(unsigned Reg) const {
Dan Gohman81bf03e2010-04-13 16:57:55 +0000336 livein_iterator I = std::find(livein_begin(), livein_end(), Reg);
Evan Chenga971dbd2008-04-24 09:06:33 +0000337 return I != livein_end();
338}
339
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000340void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000341 getParent()->splice(NewAfter, this);
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000342}
343
344void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000345 MachineFunction::iterator BBI = NewBefore;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000346 getParent()->splice(++BBI, this);
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000347}
348
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000349void MachineBasicBlock::updateTerminator() {
350 const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo();
351 // A block with no successors has no concerns with fall-through edges.
352 if (this->succ_empty()) return;
353
354 MachineBasicBlock *TBB = 0, *FBB = 0;
355 SmallVector<MachineOperand, 4> Cond;
Stuart Hastings3bf91252010-06-17 22:43:56 +0000356 DebugLoc dl; // FIXME: this is nowhere
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000357 bool B = TII->AnalyzeBranch(*this, TBB, FBB, Cond);
358 (void) B;
359 assert(!B && "UpdateTerminators requires analyzable predecessors!");
360 if (Cond.empty()) {
361 if (TBB) {
362 // The block has an unconditional branch. If its successor is now
363 // its layout successor, delete the branch.
364 if (isLayoutSuccessor(TBB))
365 TII->RemoveBranch(*this);
366 } else {
367 // The block has an unconditional fallthrough. If its successor is not
Chandler Carruth3b7b2092011-11-22 13:13:16 +0000368 // its layout successor, insert a branch. First we have to locate the
369 // only non-landing-pad successor, as that is the fallthrough block.
370 for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) {
371 if ((*SI)->isLandingPad())
372 continue;
373 assert(!TBB && "Found more than one non-landing-pad successor!");
374 TBB = *SI;
375 }
Chandler Carruth521fc5b2011-11-23 08:23:54 +0000376
377 // If there is no non-landing-pad successor, the block has no
378 // fall-through edges to be concerned with.
379 if (!TBB)
380 return;
381
382 // Finally update the unconditional successor to be reached via a branch
383 // if it would not be reached by fallthrough.
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000384 if (!isLayoutSuccessor(TBB))
Stuart Hastings3bf91252010-06-17 22:43:56 +0000385 TII->InsertBranch(*this, TBB, 0, Cond, dl);
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000386 }
387 } else {
388 if (FBB) {
389 // The block has a non-fallthrough conditional branch. If one of its
390 // successors is its layout successor, rewrite it to a fallthrough
391 // conditional branch.
392 if (isLayoutSuccessor(TBB)) {
Jakob Stoklund Olesene0239932009-11-22 18:28:04 +0000393 if (TII->ReverseBranchCondition(Cond))
394 return;
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000395 TII->RemoveBranch(*this);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000396 TII->InsertBranch(*this, FBB, 0, Cond, dl);
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000397 } else if (isLayoutSuccessor(FBB)) {
398 TII->RemoveBranch(*this);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000399 TII->InsertBranch(*this, TBB, 0, Cond, dl);
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000400 }
401 } else {
Chandler Carruthf1a60c72012-04-16 22:03:00 +0000402 // Walk through the successors and find the successor which is not
403 // a landing pad and is not the conditional branch destination (in TBB)
404 // as the fallthrough successor.
405 MachineBasicBlock *FallthroughBB = 0;
406 for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) {
407 if ((*SI)->isLandingPad() || *SI == TBB)
408 continue;
409 assert(!FallthroughBB && "Found more than one fallthrough successor.");
410 FallthroughBB = *SI;
411 }
412 if (!FallthroughBB && canFallThrough()) {
413 // We fallthrough to the same basic block as the conditional jump
414 // targets. Remove the conditional jump, leaving unconditional
415 // fallthrough.
416 // FIXME: This does not seem like a reasonable pattern to support, but it
417 // has been seen in the wild coming out of degenerate ARM test cases.
418 TII->RemoveBranch(*this);
419
420 // Finally update the unconditional successor to be reached via a branch
421 // if it would not be reached by fallthrough.
422 if (!isLayoutSuccessor(TBB))
423 TII->InsertBranch(*this, TBB, 0, Cond, dl);
424 return;
425 }
426
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000427 // The block has a fallthrough conditional branch.
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000428 if (isLayoutSuccessor(TBB)) {
Jakob Stoklund Olesene0239932009-11-22 18:28:04 +0000429 if (TII->ReverseBranchCondition(Cond)) {
430 // We can't reverse the condition, add an unconditional branch.
431 Cond.clear();
Chandler Carruthf1a60c72012-04-16 22:03:00 +0000432 TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl);
Jakob Stoklund Olesene0239932009-11-22 18:28:04 +0000433 return;
434 }
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000435 TII->RemoveBranch(*this);
Chandler Carruthf1a60c72012-04-16 22:03:00 +0000436 TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl);
437 } else if (!isLayoutSuccessor(FallthroughBB)) {
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000438 TII->RemoveBranch(*this);
Chandler Carruthf1a60c72012-04-16 22:03:00 +0000439 TII->InsertBranch(*this, TBB, FallthroughBB, Cond, dl);
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000440 }
441 }
442 }
443}
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000444
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000445void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ, uint32_t weight) {
446
447 // If we see non-zero value for the first time it means we actually use Weight
448 // list, so we fill all Weights with 0's.
449 if (weight != 0 && Weights.empty())
450 Weights.resize(Successors.size());
451
452 if (weight != 0 || !Weights.empty())
453 Weights.push_back(weight);
454
455 Successors.push_back(succ);
456 succ->addPredecessor(this);
457 }
Chris Lattner52c09d72004-10-26 15:43:42 +0000458
459void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
460 succ->removePredecessor(this);
461 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
462 assert(I != Successors.end() && "Not a current successor!");
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000463
464 // If Weight list is empty it means we don't use it (disabled optimization).
465 if (!Weights.empty()) {
466 weight_iterator WI = getWeightIterator(I);
467 Weights.erase(WI);
468 }
469
Chris Lattner52c09d72004-10-26 15:43:42 +0000470 Successors.erase(I);
471}
472
Jakub Staszak12af5ff2011-06-16 18:01:17 +0000473MachineBasicBlock::succ_iterator
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000474MachineBasicBlock::removeSuccessor(succ_iterator I) {
Chris Lattner52c09d72004-10-26 15:43:42 +0000475 assert(I != Successors.end() && "Not a current successor!");
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000476
477 // If Weight list is empty it means we don't use it (disabled optimization).
478 if (!Weights.empty()) {
479 weight_iterator WI = getWeightIterator(I);
480 Weights.erase(WI);
481 }
482
Chris Lattner52c09d72004-10-26 15:43:42 +0000483 (*I)->removePredecessor(this);
Dan Gohman5d5ee802009-01-08 22:19:34 +0000484 return Successors.erase(I);
Chris Lattner52c09d72004-10-26 15:43:42 +0000485}
486
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000487void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old,
488 MachineBasicBlock *New) {
Jakob Stoklund Olesen15121ca2012-08-10 03:23:27 +0000489 if (Old == New)
490 return;
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000491
Jakob Stoklund Olesen15121ca2012-08-10 03:23:27 +0000492 succ_iterator E = succ_end();
493 succ_iterator NewI = E;
494 succ_iterator OldI = E;
495 for (succ_iterator I = succ_begin(); I != E; ++I) {
496 if (*I == Old) {
497 OldI = I;
498 if (NewI != E)
499 break;
500 }
501 if (*I == New) {
502 NewI = I;
503 if (OldI != E)
504 break;
505 }
506 }
507 assert(OldI != E && "Old is not a successor of this block");
508 Old->removePredecessor(this);
509
510 // If New isn't already a successor, let it take Old's place.
511 if (NewI == E) {
512 New->addPredecessor(this);
513 *OldI = New;
514 return;
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000515 }
516
Jakob Stoklund Olesen15121ca2012-08-10 03:23:27 +0000517 // New is already a successor.
518 // Update its weight instead of adding a duplicate edge.
519 if (!Weights.empty()) {
520 weight_iterator OldWI = getWeightIterator(OldI);
521 *getWeightIterator(NewI) += *OldWI;
522 Weights.erase(OldWI);
523 }
524 Successors.erase(OldI);
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000525}
526
Chris Lattner52c09d72004-10-26 15:43:42 +0000527void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
528 Predecessors.push_back(pred);
529}
530
531void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
Eli Friedman6dda9162011-04-18 21:21:37 +0000532 pred_iterator I = std::find(Predecessors.begin(), Predecessors.end(), pred);
Chris Lattner52c09d72004-10-26 15:43:42 +0000533 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
534 Predecessors.erase(I);
535}
Evan Cheng4f098782007-05-17 23:58:53 +0000536
Chris Lattner6371ed52009-08-23 00:35:30 +0000537void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) {
Mon P Wang63307c32008-05-05 19:05:59 +0000538 if (this == fromMBB)
539 return;
Jakub Staszak12af5ff2011-06-16 18:01:17 +0000540
Dan Gohman14152b42010-07-06 20:24:04 +0000541 while (!fromMBB->succ_empty()) {
542 MachineBasicBlock *Succ = *fromMBB->succ_begin();
Jakob Stoklund Olesen03e593e2012-08-13 23:13:25 +0000543 uint32_t Weight = 0;
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000544
545 // If Weight list is empty it means we don't use it (disabled optimization).
546 if (!fromMBB->Weights.empty())
Jakob Stoklund Olesen03e593e2012-08-13 23:13:25 +0000547 Weight = *fromMBB->Weights.begin();
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000548
Jakob Stoklund Olesen03e593e2012-08-13 23:13:25 +0000549 addSuccessor(Succ, Weight);
Dan Gohman14152b42010-07-06 20:24:04 +0000550 fromMBB->removeSuccessor(Succ);
551 }
552}
553
554void
555MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB) {
556 if (this == fromMBB)
557 return;
Jakub Staszak12af5ff2011-06-16 18:01:17 +0000558
Dan Gohman14152b42010-07-06 20:24:04 +0000559 while (!fromMBB->succ_empty()) {
560 MachineBasicBlock *Succ = *fromMBB->succ_begin();
Jakob Stoklund Olesen03e593e2012-08-13 23:13:25 +0000561 uint32_t Weight = 0;
562 if (!fromMBB->Weights.empty())
563 Weight = *fromMBB->Weights.begin();
564 addSuccessor(Succ, Weight);
Dan Gohman14152b42010-07-06 20:24:04 +0000565 fromMBB->removeSuccessor(Succ);
566
567 // Fix up any PHI nodes in the successor.
Evan Chengddfd1372011-12-14 02:11:42 +0000568 for (MachineBasicBlock::instr_iterator MI = Succ->instr_begin(),
569 ME = Succ->instr_end(); MI != ME && MI->isPHI(); ++MI)
Dan Gohman14152b42010-07-06 20:24:04 +0000570 for (unsigned i = 2, e = MI->getNumOperands()+1; i != e; i += 2) {
571 MachineOperand &MO = MI->getOperand(i);
572 if (MO.getMBB() == fromMBB)
573 MO.setMBB(this);
574 }
575 }
Mon P Wang63307c32008-05-05 19:05:59 +0000576}
577
Jakob Stoklund Olesenf192b502012-07-30 17:36:47 +0000578bool MachineBasicBlock::isPredecessor(const MachineBasicBlock *MBB) const {
579 return std::find(pred_begin(), pred_end(), MBB) != pred_end();
580}
581
Dan Gohman6d1b89e2009-03-30 20:06:29 +0000582bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const {
Jakob Stoklund Olesenf192b502012-07-30 17:36:47 +0000583 return std::find(succ_begin(), succ_end(), MBB) != succ_end();
Evan Cheng4f098782007-05-17 23:58:53 +0000584}
Evan Cheng0370fad2007-06-04 06:44:01 +0000585
Dan Gohman6d1b89e2009-03-30 20:06:29 +0000586bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const {
Dan Gohman6ade6f52008-10-02 22:09:09 +0000587 MachineFunction::const_iterator I(this);
Chris Lattner7896c9f2009-12-03 00:50:42 +0000588 return llvm::next(I) == MachineFunction::const_iterator(MBB);
Dan Gohman6ade6f52008-10-02 22:09:09 +0000589}
590
Bob Wilson15acadd2009-11-26 00:32:21 +0000591bool MachineBasicBlock::canFallThrough() {
Bob Wilson15acadd2009-11-26 00:32:21 +0000592 MachineFunction::iterator Fallthrough = this;
593 ++Fallthrough;
594 // If FallthroughBlock is off the end of the function, it can't fall through.
595 if (Fallthrough == getParent()->end())
596 return false;
597
598 // If FallthroughBlock isn't a successor, no fallthrough is possible.
599 if (!isSuccessor(Fallthrough))
600 return false;
601
Dan Gohman735985f2009-12-05 00:32:59 +0000602 // Analyze the branches, if any, at the end of the block.
603 MachineBasicBlock *TBB = 0, *FBB = 0;
604 SmallVector<MachineOperand, 4> Cond;
605 const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo();
Jakob Stoklund Olesen33cc8d62010-01-15 20:00:12 +0000606 if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) {
Dan Gohman735985f2009-12-05 00:32:59 +0000607 // If we couldn't analyze the branch, examine the last instruction.
608 // If the block doesn't end in a known control barrier, assume fallthrough
Chad Rosier0162ff42012-01-26 18:24:25 +0000609 // is possible. The isPredicated check is needed because this code can be
Dan Gohman735985f2009-12-05 00:32:59 +0000610 // called during IfConversion, where an instruction which is normally a
Chad Rosier6a5d0e22012-01-26 20:19:05 +0000611 // Barrier is predicated and thus no longer an actual control barrier.
Chad Rosier0162ff42012-01-26 18:24:25 +0000612 return empty() || !back().isBarrier() || TII->isPredicated(&back());
Dan Gohman735985f2009-12-05 00:32:59 +0000613 }
Bob Wilson15acadd2009-11-26 00:32:21 +0000614
615 // If there is no branch, control always falls through.
616 if (TBB == 0) return true;
617
618 // If there is some explicit branch to the fallthrough block, it can obviously
619 // reach, even though the branch should get folded to fall through implicitly.
620 if (MachineFunction::iterator(TBB) == Fallthrough ||
621 MachineFunction::iterator(FBB) == Fallthrough)
622 return true;
623
624 // If it's an unconditional branch to some block not the fall through, it
625 // doesn't fall through.
626 if (Cond.empty()) return false;
627
628 // Otherwise, if it is conditional and has no explicit false block, it falls
629 // through.
630 return FBB == 0;
631}
632
Dan Gohman853d3fb2010-06-22 17:25:57 +0000633MachineBasicBlock *
634MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P) {
Evan Chengddb14202012-04-24 19:06:55 +0000635 // Splitting the critical edge to a landing pad block is non-trivial. Don't do
636 // it in this generic function.
637 if (Succ->isLandingPad())
638 return NULL;
639
Dan Gohman853d3fb2010-06-22 17:25:57 +0000640 MachineFunction *MF = getParent();
641 DebugLoc dl; // FIXME: this is nowhere
642
Jakob Stoklund Olesen371e82b2010-11-02 00:58:37 +0000643 // We may need to update this's terminator, but we can't do that if
644 // AnalyzeBranch fails. If this uses a jump table, we won't touch it.
Dan Gohman853d3fb2010-06-22 17:25:57 +0000645 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
646 MachineBasicBlock *TBB = 0, *FBB = 0;
647 SmallVector<MachineOperand, 4> Cond;
648 if (TII->AnalyzeBranch(*this, TBB, FBB, Cond))
649 return NULL;
650
Jakob Stoklund Olesen371e82b2010-11-02 00:58:37 +0000651 // Avoid bugpoint weirdness: A block may end with a conditional branch but
652 // jumps to the same MBB is either case. We have duplicate CFG edges in that
653 // case that we can't handle. Since this never happens in properly optimized
654 // code, just skip those edges.
655 if (TBB && TBB == FBB) {
656 DEBUG(dbgs() << "Won't split critical edge after degenerate BB#"
657 << getNumber() << '\n');
658 return NULL;
659 }
660
Dan Gohman853d3fb2010-06-22 17:25:57 +0000661 MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock();
662 MF->insert(llvm::next(MachineFunction::iterator(this)), NMBB);
Evan Cheng087fbeb2010-08-17 17:15:14 +0000663 DEBUG(dbgs() << "Splitting critical edge:"
Dan Gohman853d3fb2010-06-22 17:25:57 +0000664 " BB#" << getNumber()
665 << " -- BB#" << NMBB->getNumber()
666 << " -- BB#" << Succ->getNumber() << '\n');
Cameron Zwarichdd58fa42013-02-12 03:49:20 +0000667
668 LiveIntervals *LIS = P->getAnalysisIfAvailable<LiveIntervals>();
Cameron Zwarichf5844a72013-02-10 23:29:54 +0000669 SlotIndexes *Indexes = P->getAnalysisIfAvailable<SlotIndexes>();
Cameron Zwarichdd58fa42013-02-12 03:49:20 +0000670 if (LIS)
671 LIS->insertMBBInMaps(NMBB);
672 else if (Indexes)
Cameron Zwarichf5844a72013-02-10 23:29:54 +0000673 Indexes->insertMBBInMaps(NMBB);
Dan Gohman853d3fb2010-06-22 17:25:57 +0000674
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000675 // On some targets like Mips, branches may kill virtual registers. Make sure
676 // that LiveVariables is properly updated after updateTerminator replaces the
677 // terminators.
678 LiveVariables *LV = P->getAnalysisIfAvailable<LiveVariables>();
679
680 // Collect a list of virtual registers killed by the terminators.
681 SmallVector<unsigned, 4> KilledRegs;
682 if (LV)
Evan Chengddfd1372011-12-14 02:11:42 +0000683 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000684 I != E; ++I) {
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000685 MachineInstr *MI = I;
686 for (MachineInstr::mop_iterator OI = MI->operands_begin(),
687 OE = MI->operands_end(); OI != OE; ++OI) {
Lang Hames72a043f2012-02-09 05:59:36 +0000688 if (!OI->isReg() || OI->getReg() == 0 ||
689 !OI->isUse() || !OI->isKill() || OI->isUndef())
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000690 continue;
691 unsigned Reg = OI->getReg();
Lang Hames72a043f2012-02-09 05:59:36 +0000692 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000693 LV->getVarInfo(Reg).removeKill(MI)) {
694 KilledRegs.push_back(Reg);
695 DEBUG(dbgs() << "Removing terminator kill: " << *MI);
696 OI->setIsKill(false);
697 }
698 }
699 }
700
Cameron Zwarichf0b25352013-02-17 00:10:44 +0000701 SmallVector<unsigned, 4> UsedRegs;
702 if (LIS) {
703 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
704 I != E; ++I) {
705 MachineInstr *MI = I;
706
707 for (MachineInstr::mop_iterator OI = MI->operands_begin(),
708 OE = MI->operands_end(); OI != OE; ++OI) {
709 if (!OI->isReg() || OI->getReg() == 0)
710 continue;
711
712 unsigned Reg = OI->getReg();
713 if (std::find(UsedRegs.begin(), UsedRegs.end(), Reg) == UsedRegs.end())
714 UsedRegs.push_back(Reg);
715 }
716 }
717 }
718
Dan Gohman853d3fb2010-06-22 17:25:57 +0000719 ReplaceUsesOfBlockWith(Succ, NMBB);
Cameron Zwarichcbe3f5e2013-02-11 09:24:45 +0000720
721 // If updateTerminator() removes instructions, we need to remove them from
722 // SlotIndexes.
723 SmallVector<MachineInstr*, 4> Terminators;
724 if (Indexes) {
725 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
726 I != E; ++I)
727 Terminators.push_back(I);
728 }
729
Dan Gohman853d3fb2010-06-22 17:25:57 +0000730 updateTerminator();
731
Cameron Zwarichcbe3f5e2013-02-11 09:24:45 +0000732 if (Indexes) {
733 SmallVector<MachineInstr*, 4> NewTerminators;
734 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
735 I != E; ++I)
736 NewTerminators.push_back(I);
737
738 for (SmallVectorImpl<MachineInstr*>::iterator I = Terminators.begin(),
739 E = Terminators.end(); I != E; ++I) {
740 if (std::find(NewTerminators.begin(), NewTerminators.end(), *I) ==
741 NewTerminators.end())
742 Indexes->removeMachineInstrFromMaps(*I);
743 }
744 }
745
Dan Gohman853d3fb2010-06-22 17:25:57 +0000746 // Insert unconditional "jump Succ" instruction in NMBB if necessary.
747 NMBB->addSuccessor(Succ);
748 if (!NMBB->isLayoutSuccessor(Succ)) {
749 Cond.clear();
750 MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, Succ, NULL, Cond, dl);
Cameron Zwarichf5844a72013-02-10 23:29:54 +0000751
752 if (Indexes) {
753 for (instr_iterator I = NMBB->instr_begin(), E = NMBB->instr_end();
754 I != E; ++I) {
755 // Some instructions may have been moved to NMBB by updateTerminator(),
756 // so we first remove any instruction that already has an index.
757 if (Indexes->hasIndex(I))
758 Indexes->removeMachineInstrFromMaps(I);
759 Indexes->insertMachineInstrInMaps(I);
760 }
761 }
Dan Gohman853d3fb2010-06-22 17:25:57 +0000762 }
763
764 // Fix PHI nodes in Succ so they refer to NMBB instead of this
Evan Chengddfd1372011-12-14 02:11:42 +0000765 for (MachineBasicBlock::instr_iterator
766 i = Succ->instr_begin(),e = Succ->instr_end();
767 i != e && i->isPHI(); ++i)
Dan Gohman853d3fb2010-06-22 17:25:57 +0000768 for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2)
769 if (i->getOperand(ni+1).getMBB() == this)
770 i->getOperand(ni+1).setMBB(NMBB);
771
Jakob Stoklund Olesenac7caa02011-10-14 17:25:46 +0000772 // Inherit live-ins from the successor
773 for (MachineBasicBlock::livein_iterator I = Succ->livein_begin(),
Bill Wendling96cb1122012-07-19 00:04:14 +0000774 E = Succ->livein_end(); I != E; ++I)
Jakob Stoklund Olesenac7caa02011-10-14 17:25:46 +0000775 NMBB->addLiveIn(*I);
776
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000777 // Update LiveVariables.
Lang Hames72a043f2012-02-09 05:59:36 +0000778 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000779 if (LV) {
780 // Restore kills of virtual registers that were killed by the terminators.
781 while (!KilledRegs.empty()) {
782 unsigned Reg = KilledRegs.pop_back_val();
Evan Chengddfd1372011-12-14 02:11:42 +0000783 for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) {
Lang Hames72a043f2012-02-09 05:59:36 +0000784 if (!(--I)->addRegisterKilled(Reg, TRI, /* addIfNotFound= */ false))
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000785 continue;
Lang Hames72a043f2012-02-09 05:59:36 +0000786 if (TargetRegisterInfo::isVirtualRegister(Reg))
787 LV->getVarInfo(Reg).Kills.push_back(I);
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000788 DEBUG(dbgs() << "Restored terminator kill: " << *I);
789 break;
790 }
791 }
792 // Update relevant live-through information.
Dan Gohman853d3fb2010-06-22 17:25:57 +0000793 LV->addNewBlock(NMBB, this, Succ);
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000794 }
Dan Gohman853d3fb2010-06-22 17:25:57 +0000795
Cameron Zwarichdd58fa42013-02-12 03:49:20 +0000796 if (LIS) {
Cameron Zwarich8597c142013-02-11 09:24:47 +0000797 // After splitting the edge and updating SlotIndexes, live intervals may be
798 // in one of two situations, depending on whether this block was the last in
799 // the function. If the original block was the last in the function, all live
800 // intervals will end prior to the beginning of the new split block. If the
801 // original block was not at the end of the function, all live intervals will
802 // extend to the end of the new split block.
803
804 bool isLastMBB =
805 llvm::next(MachineFunction::iterator(NMBB)) == getParent()->end();
806
807 SlotIndex StartIndex = Indexes->getMBBEndIdx(this);
808 SlotIndex PrevIndex = StartIndex.getPrevSlot();
809 SlotIndex EndIndex = Indexes->getMBBEndIdx(NMBB);
810
811 // Find the registers used from NMBB in PHIs in Succ.
812 SmallSet<unsigned, 8> PHISrcRegs;
813 for (MachineBasicBlock::instr_iterator
814 I = Succ->instr_begin(), E = Succ->instr_end();
815 I != E && I->isPHI(); ++I) {
816 for (unsigned ni = 1, ne = I->getNumOperands(); ni != ne; ni += 2) {
817 if (I->getOperand(ni+1).getMBB() == NMBB) {
818 MachineOperand &MO = I->getOperand(ni);
819 unsigned Reg = MO.getReg();
820 PHISrcRegs.insert(Reg);
Cameron Zwarichdbf10c42013-02-12 03:49:17 +0000821 if (MO.isUndef())
822 continue;
Cameron Zwarich8597c142013-02-11 09:24:47 +0000823
824 LiveInterval &LI = LIS->getInterval(Reg);
825 VNInfo *VNI = LI.getVNInfoAt(PrevIndex);
826 assert(VNI && "PHI sources should be live out of their predecessors.");
827 LI.addRange(LiveRange(StartIndex, EndIndex, VNI));
828 }
829 }
830 }
831
832 MachineRegisterInfo *MRI = &getParent()->getRegInfo();
833 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
834 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
835 if (PHISrcRegs.count(Reg) || !LIS->hasInterval(Reg))
836 continue;
837
838 LiveInterval &LI = LIS->getInterval(Reg);
839 if (!LI.liveAt(PrevIndex))
840 continue;
841
Cameron Zwarichdbf10c42013-02-12 03:49:17 +0000842 bool isLiveOut = LI.liveAt(LIS->getMBBStartIdx(Succ));
Cameron Zwarich8597c142013-02-11 09:24:47 +0000843 if (isLiveOut && isLastMBB) {
844 VNInfo *VNI = LI.getVNInfoAt(PrevIndex);
845 assert(VNI && "LiveInterval should have VNInfo where it is live.");
846 LI.addRange(LiveRange(StartIndex, EndIndex, VNI));
847 } else if (!isLiveOut && !isLastMBB) {
848 LI.removeRange(StartIndex, EndIndex);
849 }
850 }
Cameron Zwarichf0b25352013-02-17 00:10:44 +0000851
852 // Update all intervals for registers whose uses may have been modified by
853 // updateTerminator().
854 iterator FirstTerminator = getFirstTerminator();
855 MachineInstr *FirstTerminatorMI = FirstTerminator;
856 if (FirstTerminatorMI->isBundled())
857 FirstTerminatorMI = getBundleStart(FirstTerminatorMI);
858 reverse_iterator PreTerminators =
859 (FirstTerminator == begin()) ? rend()
860 : reverse_iterator(FirstTerminatorMI);
861 LIS->repairIntervalsInRange(this, rbegin(), PreTerminators, UsedRegs);
Cameron Zwarich8597c142013-02-11 09:24:47 +0000862 }
863
Dan Gohman853d3fb2010-06-22 17:25:57 +0000864 if (MachineDominatorTree *MDT =
Evan Cheng19708922010-08-19 23:32:47 +0000865 P->getAnalysisIfAvailable<MachineDominatorTree>()) {
866 // Update dominator information.
867 MachineDomTreeNode *SucccDTNode = MDT->getNode(Succ);
868
869 bool IsNewIDom = true;
870 for (const_pred_iterator PI = Succ->pred_begin(), E = Succ->pred_end();
871 PI != E; ++PI) {
872 MachineBasicBlock *PredBB = *PI;
873 if (PredBB == NMBB)
874 continue;
875 if (!MDT->dominates(SucccDTNode, MDT->getNode(PredBB))) {
876 IsNewIDom = false;
877 break;
878 }
879 }
880
881 // We know "this" dominates the newly created basic block.
882 MachineDomTreeNode *NewDTNode = MDT->addNewBlock(NMBB, this);
883
884 // If all the other predecessors of "Succ" are dominated by "Succ" itself
885 // then the new block is the new immediate dominator of "Succ". Otherwise,
886 // the new block doesn't dominate anything.
887 if (IsNewIDom)
888 MDT->changeImmediateDominator(SucccDTNode, NewDTNode);
889 }
Dan Gohman853d3fb2010-06-22 17:25:57 +0000890
Evan Chenge0083842010-08-17 17:43:50 +0000891 if (MachineLoopInfo *MLI = P->getAnalysisIfAvailable<MachineLoopInfo>())
Dan Gohman853d3fb2010-06-22 17:25:57 +0000892 if (MachineLoop *TIL = MLI->getLoopFor(this)) {
893 // If one or the other blocks were not in a loop, the new block is not
894 // either, and thus LI doesn't need to be updated.
895 if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) {
896 if (TIL == DestLoop) {
897 // Both in the same loop, the NMBB joins loop.
898 DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase());
899 } else if (TIL->contains(DestLoop)) {
900 // Edge from an outer loop to an inner loop. Add to the outer loop.
901 TIL->addBasicBlockToLoop(NMBB, MLI->getBase());
902 } else if (DestLoop->contains(TIL)) {
903 // Edge from an inner loop to an outer loop. Add to the outer loop.
904 DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase());
905 } else {
906 // Edge from two loops with no containment relation. Because these
907 // are natural loops, we know that the destination block must be the
908 // header of its loop (adding a branch into a loop elsewhere would
909 // create an irreducible loop).
910 assert(DestLoop->getHeader() == Succ &&
911 "Should not create irreducible loops!");
912 if (MachineLoop *P = DestLoop->getParentLoop())
913 P->addBasicBlockToLoop(NMBB, MLI->getBase());
914 }
915 }
916 }
917
918 return NMBB;
919}
920
Jakob Stoklund Olesen9f4692d2012-12-17 23:55:38 +0000921/// Prepare MI to be removed from its bundle. This fixes bundle flags on MI's
922/// neighboring instructions so the bundle won't be broken by removing MI.
923static void unbundleSingleMI(MachineInstr *MI) {
924 // Removing the first instruction in a bundle.
925 if (MI->isBundledWithSucc() && !MI->isBundledWithPred())
926 MI->unbundleFromSucc();
927 // Removing the last instruction in a bundle.
928 if (MI->isBundledWithPred() && !MI->isBundledWithSucc())
929 MI->unbundleFromPred();
930 // If MI is not bundled, or if it is internal to a bundle, the neighbor flags
931 // are already fine.
Evan Chengddfd1372011-12-14 02:11:42 +0000932}
933
Jakob Stoklund Olesen9f4692d2012-12-17 23:55:38 +0000934MachineBasicBlock::instr_iterator
935MachineBasicBlock::erase(MachineBasicBlock::instr_iterator I) {
936 unbundleSingleMI(I);
937 return Insts.erase(I);
938}
Evan Chengddfd1372011-12-14 02:11:42 +0000939
Jakob Stoklund Olesen9f4692d2012-12-17 23:55:38 +0000940MachineInstr *MachineBasicBlock::remove_instr(MachineInstr *MI) {
941 unbundleSingleMI(MI);
942 MI->clearFlag(MachineInstr::BundledPred);
943 MI->clearFlag(MachineInstr::BundledSucc);
944 return Insts.remove(MI);
Evan Chengddfd1372011-12-14 02:11:42 +0000945}
946
Jakob Stoklund Olesenedc35032012-12-18 17:54:53 +0000947MachineBasicBlock::instr_iterator
948MachineBasicBlock::insert(instr_iterator I, MachineInstr *MI) {
949 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
950 "Cannot insert instruction with bundle flags");
951 // Set the bundle flags when inserting inside a bundle.
952 if (I != instr_end() && I->isBundledWithPred()) {
953 MI->setFlag(MachineInstr::BundledPred);
954 MI->setFlag(MachineInstr::BundledSucc);
955 }
956 return Insts.insert(I, MI);
957}
958
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000959/// removeFromParent - This method unlinks 'this' from the containing function,
960/// and returns it, but does not delete it.
961MachineBasicBlock *MachineBasicBlock::removeFromParent() {
962 assert(getParent() && "Not embedded in a function!");
963 getParent()->remove(this);
964 return this;
965}
966
967
968/// eraseFromParent - This method unlinks 'this' from the containing function,
969/// and deletes it.
970void MachineBasicBlock::eraseFromParent() {
971 assert(getParent() && "Not embedded in a function!");
972 getParent()->erase(this);
973}
974
975
Evan Cheng0370fad2007-06-04 06:44:01 +0000976/// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
977/// 'Old', change the code and CFG so that it branches to 'New' instead.
978void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old,
979 MachineBasicBlock *New) {
980 assert(Old != New && "Cannot replace self with self!");
981
Evan Chengddfd1372011-12-14 02:11:42 +0000982 MachineBasicBlock::instr_iterator I = instr_end();
983 while (I != instr_begin()) {
Evan Cheng0370fad2007-06-04 06:44:01 +0000984 --I;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000985 if (!I->isTerminator()) break;
Evan Cheng0370fad2007-06-04 06:44:01 +0000986
987 // Scan the operands of this machine instruction, replacing any uses of Old
988 // with New.
989 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
Dan Gohmand735b802008-10-03 15:45:36 +0000990 if (I->getOperand(i).isMBB() &&
Dan Gohman014278e2008-09-13 17:58:21 +0000991 I->getOperand(i).getMBB() == Old)
Chris Lattner8aa797a2007-12-30 23:10:15 +0000992 I->getOperand(i).setMBB(New);
Evan Cheng0370fad2007-06-04 06:44:01 +0000993 }
994
Dan Gohman5412d062009-05-05 21:10:19 +0000995 // Update the successor information.
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000996 replaceSuccessor(Old, New);
Evan Cheng0370fad2007-06-04 06:44:01 +0000997}
998
Evan Cheng2bdb7d02007-06-18 22:43:58 +0000999/// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the
1000/// CFG to be inserted. If we have proven that MBB can only branch to DestA and
Bill Wendlingc70d33112009-12-16 00:08:36 +00001001/// DestB, remove any other MBB successors from the CFG. DestA and DestB can be
1002/// null.
Jakub Staszak12af5ff2011-06-16 18:01:17 +00001003///
Chris Lattnerf20c1a42007-12-31 04:56:33 +00001004/// Besides DestA and DestB, retain other edges leading to LandingPads
1005/// (currently there can be only one; we don't check or require that here).
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001006/// Note it is possible that DestA and/or DestB are LandingPads.
1007bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA,
1008 MachineBasicBlock *DestB,
1009 bool isCond) {
Bill Wendlingc70d33112009-12-16 00:08:36 +00001010 // The values of DestA and DestB frequently come from a call to the
1011 // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial
1012 // values from there.
1013 //
1014 // 1. If both DestA and DestB are null, then the block ends with no branches
1015 // (it falls through to its successor).
1016 // 2. If DestA is set, DestB is null, and isCond is false, then the block ends
1017 // with only an unconditional branch.
1018 // 3. If DestA is set, DestB is null, and isCond is true, then the block ends
1019 // with a conditional branch that falls through to a successor (DestB).
1020 // 4. If DestA and DestB is set and isCond is true, then the block ends with a
1021 // conditional branch followed by an unconditional branch. DestA is the
1022 // 'true' destination and DestB is the 'false' destination.
1023
Bill Wendlinge543d162010-04-01 00:00:43 +00001024 bool Changed = false;
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001025
Chris Lattner7896c9f2009-12-03 00:50:42 +00001026 MachineFunction::iterator FallThru =
1027 llvm::next(MachineFunction::iterator(this));
Bill Wendlinge543d162010-04-01 00:00:43 +00001028
1029 if (DestA == 0 && DestB == 0) {
1030 // Block falls through to successor.
1031 DestA = FallThru;
1032 DestB = FallThru;
1033 } else if (DestA != 0 && DestB == 0) {
1034 if (isCond)
1035 // Block ends in conditional jump that falls through to successor.
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001036 DestB = FallThru;
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001037 } else {
Bill Wendlinge543d162010-04-01 00:00:43 +00001038 assert(DestA && DestB && isCond &&
1039 "CFG in a bad state. Cannot correct CFG edges");
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001040 }
Bill Wendlinge543d162010-04-01 00:00:43 +00001041
1042 // Remove superfluous edges. I.e., those which aren't destinations of this
1043 // basic block, duplicate edges, or landing pads.
1044 SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs;
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001045 MachineBasicBlock::succ_iterator SI = succ_begin();
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001046 while (SI != succ_end()) {
Bill Wendlingc70d33112009-12-16 00:08:36 +00001047 const MachineBasicBlock *MBB = *SI;
Bill Wendlinge543d162010-04-01 00:00:43 +00001048 if (!SeenMBBs.insert(MBB) ||
1049 (MBB != DestA && MBB != DestB && !MBB->isLandingPad())) {
1050 // This is a superfluous edge, remove it.
Bill Wendling9e9cca42010-03-31 23:26:26 +00001051 SI = removeSuccessor(SI);
Bill Wendlinge543d162010-04-01 00:00:43 +00001052 Changed = true;
1053 } else {
1054 ++SI;
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001055 }
1056 }
Bill Wendlingc70d33112009-12-16 00:08:36 +00001057
Bill Wendlinge543d162010-04-01 00:00:43 +00001058 return Changed;
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001059}
Evan Cheng2a085c32009-11-17 19:19:59 +00001060
Dale Johannesen73e884b2010-01-20 21:36:02 +00001061/// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
Dale Johannesen10fedd22010-02-10 00:11:11 +00001062/// any DBG_VALUE instructions. Return UnknownLoc if there is none.
Dale Johannesen73e884b2010-01-20 21:36:02 +00001063DebugLoc
Evan Chengddfd1372011-12-14 02:11:42 +00001064MachineBasicBlock::findDebugLoc(instr_iterator MBBI) {
Dale Johannesen73e884b2010-01-20 21:36:02 +00001065 DebugLoc DL;
Evan Chengddfd1372011-12-14 02:11:42 +00001066 instr_iterator E = instr_end();
Evan Cheng7c2a4a32011-12-06 22:12:01 +00001067 if (MBBI == E)
1068 return DL;
1069
1070 // Skip debug declarations, we don't want a DebugLoc from them.
1071 while (MBBI != E && MBBI->isDebugValue())
1072 MBBI++;
1073 if (MBBI != E)
1074 DL = MBBI->getDebugLoc();
Dale Johannesen73e884b2010-01-20 21:36:02 +00001075 return DL;
1076}
1077
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001078/// getSuccWeight - Return weight of the edge from this block to MBB.
1079///
Jakob Stoklund Olesen990ca552012-08-20 22:01:38 +00001080uint32_t MachineBasicBlock::getSuccWeight(const_succ_iterator Succ) const {
Jakub Staszak981d8262011-06-17 18:00:21 +00001081 if (Weights.empty())
1082 return 0;
1083
Jakob Stoklund Olesen990ca552012-08-20 22:01:38 +00001084 return *getWeightIterator(Succ);
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001085}
1086
1087/// getWeightIterator - Return wight iterator corresonding to the I successor
1088/// iterator
1089MachineBasicBlock::weight_iterator MachineBasicBlock::
1090getWeightIterator(MachineBasicBlock::succ_iterator I) {
Jakub Staszak981d8262011-06-17 18:00:21 +00001091 assert(Weights.size() == Successors.size() && "Async weight list!");
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001092 size_t index = std::distance(Successors.begin(), I);
1093 assert(index < Weights.size() && "Not a current successor!");
1094 return Weights.begin() + index;
1095}
1096
Jakub Staszak25101bb2011-12-20 20:03:10 +00001097/// getWeightIterator - Return wight iterator corresonding to the I successor
1098/// iterator
1099MachineBasicBlock::const_weight_iterator MachineBasicBlock::
1100getWeightIterator(MachineBasicBlock::const_succ_iterator I) const {
1101 assert(Weights.size() == Successors.size() && "Async weight list!");
1102 const size_t index = std::distance(Successors.begin(), I);
1103 assert(index < Weights.size() && "Not a current successor!");
1104 return Weights.begin() + index;
1105}
1106
James Molloyc4f70d42012-09-12 10:18:23 +00001107/// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed
1108/// as of just before "MI".
1109///
1110/// Search is localised to a neighborhood of
1111/// Neighborhood instructions before (searching for defs or kills) and N
1112/// instructions after (searching just for defs) MI.
1113MachineBasicBlock::LivenessQueryResult
1114MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI,
1115 unsigned Reg, MachineInstr *MI,
1116 unsigned Neighborhood) {
James Molloyc4f70d42012-09-12 10:18:23 +00001117 unsigned N = Neighborhood;
1118 MachineBasicBlock *MBB = MI->getParent();
1119
1120 // Start by searching backwards from MI, looking for kills, reads or defs.
1121
1122 MachineBasicBlock::iterator I(MI);
1123 // If this is the first insn in the block, don't search backwards.
1124 if (I != MBB->begin()) {
1125 do {
1126 --I;
1127
1128 MachineOperandIteratorBase::PhysRegInfo Analysis =
1129 MIOperands(I).analyzePhysReg(Reg, TRI);
1130
Tim Northover310f2482012-11-20 09:56:11 +00001131 if (Analysis.Defines)
1132 // Outputs happen after inputs so they take precedence if both are
1133 // present.
1134 return Analysis.DefinesDead ? LQR_Dead : LQR_Live;
1135
1136 if (Analysis.Kills || Analysis.Clobbers)
James Molloyc4f70d42012-09-12 10:18:23 +00001137 // Register killed, so isn't live.
1138 return LQR_Dead;
1139
Tim Northover310f2482012-11-20 09:56:11 +00001140 else if (Analysis.ReadsOverlap)
James Molloyc4f70d42012-09-12 10:18:23 +00001141 // Defined or read without a previous kill - live.
Tim Northover310f2482012-11-20 09:56:11 +00001142 return Analysis.Reads ? LQR_Live : LQR_OverlappingLive;
James Molloyc4f70d42012-09-12 10:18:23 +00001143
1144 } while (I != MBB->begin() && --N > 0);
1145 }
1146
1147 // Did we get to the start of the block?
1148 if (I == MBB->begin()) {
1149 // If so, the register's state is definitely defined by the live-in state.
1150 for (MCRegAliasIterator RAI(Reg, TRI, /*IncludeSelf=*/true);
1151 RAI.isValid(); ++RAI) {
1152 if (MBB->isLiveIn(*RAI))
1153 return (*RAI == Reg) ? LQR_Live : LQR_OverlappingLive;
1154 }
1155
1156 return LQR_Dead;
1157 }
1158
1159 N = Neighborhood;
1160
1161 // Try searching forwards from MI, looking for reads or defs.
1162 I = MachineBasicBlock::iterator(MI);
1163 // If this is the last insn in the block, don't search forwards.
1164 if (I != MBB->end()) {
1165 for (++I; I != MBB->end() && N > 0; ++I, --N) {
1166 MachineOperandIteratorBase::PhysRegInfo Analysis =
1167 MIOperands(I).analyzePhysReg(Reg, TRI);
1168
1169 if (Analysis.ReadsOverlap)
1170 // Used, therefore must have been live.
1171 return (Analysis.Reads) ?
1172 LQR_Live : LQR_OverlappingLive;
1173
Tim Northover310f2482012-11-20 09:56:11 +00001174 else if (Analysis.Clobbers || Analysis.Defines)
James Molloyc4f70d42012-09-12 10:18:23 +00001175 // Defined (but not read) therefore cannot have been live.
1176 return LQR_Dead;
1177 }
1178 }
1179
1180 // At this point we have no idea of the liveness of the register.
1181 return LQR_Unknown;
1182}
1183
Evan Cheng2a085c32009-11-17 19:19:59 +00001184void llvm::WriteAsOperand(raw_ostream &OS, const MachineBasicBlock *MBB,
1185 bool t) {
1186 OS << "BB#" << MBB->getNumber();
1187}
Dale Johannesen918f0f02010-01-20 00:19:24 +00001188