blob: 888c20e3a7c77cb0286bb9838e067bda65199ca1 [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"
Cameron Zwarich8597c142013-02-11 09:24:47 +000017#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Dan Gohman853d3fb2010-06-22 17:25:57 +000018#include "llvm/CodeGen/LiveVariables.h"
19#include "llvm/CodeGen/MachineDominators.h"
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +000020#include "llvm/CodeGen/MachineFunction.h"
Jakob Stoklund Olesenf6476522013-07-03 23:56:20 +000021#include "llvm/CodeGen/MachineInstrBuilder.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"
Stephen Hines36b56882014-04-23 16:57:46 -070027#include "llvm/IR/LeakDetector.h"
Chris Lattnerf71cb012010-01-26 04:55:51 +000028#include "llvm/MC/MCAsmInfo.h"
29#include "llvm/MC/MCContext.h"
David Greenedbdbbd92010-01-04 23:22:07 +000030#include "llvm/Support/Debug.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),
Eli Bendersky2ad047e2013-04-22 21:21:08 +000040 AddressTaken(false), CachedMCSymbol(NULL) {
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 {
Eli Bendersky2ad047e2013-04-22 21:21:08 +000051 if (!CachedMCSymbol) {
52 const MachineFunction *MF = getParent();
53 MCContext &Ctx = MF->getContext();
Stephen Hines36b56882014-04-23 16:57:46 -070054 const TargetMachine &TM = MF->getTarget();
55 const char *Prefix = TM.getDataLayout()->getPrivateGlobalPrefix();
Eli Bendersky2ad047e2013-04-22 21:21:08 +000056 CachedMCSymbol = Ctx.GetOrCreateSymbol(Twine(Prefix) + "BB" +
57 Twine(MF->getFunctionNumber()) +
58 "_" + Twine(getNumber()));
59 }
60
61 return CachedMCSymbol;
Chris Lattnerf71cb012010-01-26 04:55:51 +000062}
63
64
Chris Lattner6371ed52009-08-23 00:35:30 +000065raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) {
Daniel Dunbar1cd1d982009-07-24 10:36:58 +000066 MBB.print(OS);
67 return OS;
68}
Tanya Lattner17fb34b2004-05-24 07:14:35 +000069
Jakub Staszak12af5ff2011-06-16 18:01:17 +000070/// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the
Chris Lattner62ed6b92008-01-01 01:12:31 +000071/// parent pointer of the MBB, the MBB numbering, and any instructions in the
72/// MBB to be on the right operand list for registers.
73///
74/// MBBs start out as #-1. When a MBB is added to a MachineFunction, it
75/// gets the next available unique MBB number. If it is removed from a
76/// MachineFunction, it goes back to being #-1.
Chris Lattner6371ed52009-08-23 00:35:30 +000077void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +000078 MachineFunction &MF = *N->getParent();
79 N->Number = MF.addToMBBNumbering(N);
Chris Lattner62ed6b92008-01-01 01:12:31 +000080
81 // Make sure the instructions have their operands in the reginfo lists.
Dan Gohman8e5f2c62008-07-07 23:14:23 +000082 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Evan Chengddfd1372011-12-14 02:11:42 +000083 for (MachineBasicBlock::instr_iterator
84 I = N->instr_begin(), E = N->instr_end(); I != E; ++I)
Chris Lattner62ed6b92008-01-01 01:12:31 +000085 I->AddRegOperandsToUseLists(RegInfo);
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000086
87 LeakDetector::removeGarbageObject(N);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000088}
89
Chris Lattner6371ed52009-08-23 00:35:30 +000090void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) {
Chris Lattnerf20c1a42007-12-31 04:56:33 +000091 N->getParent()->removeFromMBBNumbering(N->Number);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000092 N->Number = -1;
Dan Gohman2c3f7ae2008-07-17 23:49:46 +000093 LeakDetector::addGarbageObject(N);
Brian Gaeke0bcb1ad2004-05-12 21:35:22 +000094}
95
Chris Lattner5e61fa92004-02-19 16:13:54 +000096
Chris Lattner62ed6b92008-01-01 01:12:31 +000097/// addNodeToList (MI) - When we add an instruction to a basic block
98/// list, we update its parent pointer and add its operands from reg use/def
99/// lists if appropriate.
Chris Lattner6371ed52009-08-23 00:35:30 +0000100void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) {
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000101 assert(N->getParent() == 0 && "machine instruction already in a basic block");
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000102 N->setParent(Parent);
Jakub Staszak12af5ff2011-06-16 18:01:17 +0000103
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000104 // Add the instruction's register operands to their corresponding
105 // use/def lists.
106 MachineFunction *MF = Parent->getParent();
107 N->AddRegOperandsToUseLists(MF->getRegInfo());
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000108
109 LeakDetector::removeGarbageObject(N);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000110}
111
Chris Lattner62ed6b92008-01-01 01:12:31 +0000112/// removeNodeFromList (MI) - When we remove an instruction from a basic block
113/// list, we update its parent pointer and remove its operands from reg use/def
114/// lists if appropriate.
Chris Lattner6371ed52009-08-23 00:35:30 +0000115void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) {
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000116 assert(N->getParent() != 0 && "machine instruction not in a basic block");
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000117
118 // Remove from the use/def lists.
Jakob Stoklund Olesenff2b99a2012-08-09 22:49:37 +0000119 if (MachineFunction *MF = N->getParent()->getParent())
120 N->RemoveRegOperandsFromUseLists(MF->getRegInfo());
Jakub Staszak12af5ff2011-06-16 18:01:17 +0000121
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000122 N->setParent(0);
Dan Gohman2c3f7ae2008-07-17 23:49:46 +0000123
124 LeakDetector::addGarbageObject(N);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000125}
126
Chris Lattner62ed6b92008-01-01 01:12:31 +0000127/// transferNodesFromList (MI) - When moving a range of instructions from one
128/// MBB list to another, we need to update the parent pointers and the use/def
129/// lists.
Chris Lattner6371ed52009-08-23 00:35:30 +0000130void ilist_traits<MachineInstr>::
131transferNodesFromList(ilist_traits<MachineInstr> &fromList,
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000132 ilist_iterator<MachineInstr> first,
133 ilist_iterator<MachineInstr> last) {
Dan Gohmanfed90b62008-07-28 21:51:04 +0000134 assert(Parent->getParent() == fromList.Parent->getParent() &&
135 "MachineInstr parent mismatch!");
136
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000137 // Splice within the same MBB -> no change.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000138 if (Parent == fromList.Parent) return;
Chris Lattner62ed6b92008-01-01 01:12:31 +0000139
140 // If splicing between two blocks within the same function, just update the
141 // parent pointers.
Dan Gohmanfed90b62008-07-28 21:51:04 +0000142 for (; first != last; ++first)
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000143 first->setParent(Parent);
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000144}
145
Dan Gohmanfed90b62008-07-28 21:51:04 +0000146void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000147 assert(!MI->getParent() && "MI is still in a block!");
148 Parent->getParent()->DeleteMachineInstr(MI);
149}
150
Dan Gohmand463a742010-07-07 14:33:51 +0000151MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() {
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000152 instr_iterator I = instr_begin(), E = instr_end();
153 while (I != E && I->isPHI())
Dan Gohmand463a742010-07-07 14:33:51 +0000154 ++I;
Akira Hatanakaaf808222012-10-26 17:11:42 +0000155 assert((I == E || !I->isInsideBundle()) &&
156 "First non-phi MI cannot be inside a bundle!");
Dan Gohmand463a742010-07-07 14:33:51 +0000157 return I;
158}
159
Jakob Stoklund Olesen92095e72010-10-30 01:26:14 +0000160MachineBasicBlock::iterator
161MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) {
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000162 iterator E = end();
Stephen Hines36b56882014-04-23 16:57:46 -0700163 while (I != E && (I->isPHI() || I->isPosition() || I->isDebugValue()))
Jakob Stoklund Olesen92095e72010-10-30 01:26:14 +0000164 ++I;
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000165 // FIXME: This needs to change if we wish to bundle labels / dbg_values
166 // inside the bundle.
Akira Hatanakaaf808222012-10-26 17:11:42 +0000167 assert((I == E || !I->isInsideBundle()) &&
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000168 "First non-phi / non-label instruction is inside a bundle!");
Jakob Stoklund Olesen92095e72010-10-30 01:26:14 +0000169 return I;
170}
171
Chris Lattner52c09d72004-10-26 15:43:42 +0000172MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() {
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000173 iterator B = begin(), E = end(), I = E;
174 while (I != B && ((--I)->isTerminator() || I->isDebugValue()))
Jakob Stoklund Olesenb6436e52011-01-14 02:12:54 +0000175 ; /*noop */
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000176 while (I != E && !I->isTerminator())
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000177 ++I;
178 return I;
179}
180
181MachineBasicBlock::const_iterator
182MachineBasicBlock::getFirstTerminator() const {
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000183 const_iterator B = begin(), E = end(), I = E;
184 while (I != B && ((--I)->isTerminator() || I->isDebugValue()))
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000185 ; /*noop */
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000186 while (I != E && !I->isTerminator())
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000187 ++I;
188 return I;
189}
190
Evan Chengddfd1372011-12-14 02:11:42 +0000191MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() {
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000192 instr_iterator B = instr_begin(), E = instr_end(), I = E;
193 while (I != B && ((--I)->isTerminator() || I->isDebugValue()))
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000194 ; /*noop */
Benjamin Kramerf378f5f2012-02-10 00:28:31 +0000195 while (I != E && !I->isTerminator())
Jakob Stoklund Olesen04223902011-01-14 06:33:45 +0000196 ++I;
Jakob Stoklund Olesenb6436e52011-01-14 02:12:54 +0000197 return I;
Alkis Evlogimenos743d0a12004-02-23 18:14:48 +0000198}
199
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000200MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() {
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000201 // Skip over end-of-block dbg_value instructions.
Evan Chengddfd1372011-12-14 02:11:42 +0000202 instr_iterator B = instr_begin(), I = instr_end();
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000203 while (I != B) {
204 --I;
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000205 // Return instruction that starts a bundle.
206 if (I->isDebugValue() || I->isInsideBundle())
207 continue;
208 return I;
209 }
210 // The block is all debug values.
211 return end();
212}
213
214MachineBasicBlock::const_iterator
215MachineBasicBlock::getLastNonDebugInstr() const {
216 // Skip over end-of-block dbg_value instructions.
Evan Chengddfd1372011-12-14 02:11:42 +0000217 const_instr_iterator B = instr_begin(), I = instr_end();
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000218 while (I != B) {
219 --I;
220 // Return instruction that starts a bundle.
221 if (I->isDebugValue() || I->isInsideBundle())
Jakob Stoklund Olesen4f28c1c2011-01-13 21:28:52 +0000222 continue;
223 return I;
224 }
225 // The block is all debug values.
226 return end();
227}
228
Jakob Stoklund Olesencb640472011-02-04 19:33:11 +0000229const MachineBasicBlock *MachineBasicBlock::getLandingPadSuccessor() const {
230 // A block with a landing pad successor only has one other successor.
231 if (succ_size() > 2)
232 return 0;
233 for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I)
234 if ((*I)->isLandingPad())
235 return *I;
236 return 0;
237}
238
Manman Renb720be62012-09-11 22:23:19 +0000239#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattner52c09d72004-10-26 15:43:42 +0000240void MachineBasicBlock::dump() const {
David Greenedbdbbd92010-01-04 23:22:07 +0000241 print(dbgs());
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000242}
Manman Ren77e300e2012-09-06 19:06:06 +0000243#endif
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000244
Jakob Stoklund Olesen324da762009-11-20 01:17:03 +0000245StringRef MachineBasicBlock::getName() const {
246 if (const BasicBlock *LBB = getBasicBlock())
247 return LBB->getName();
248 else
249 return "(null)";
250}
251
Andrew Trick8ceaa662012-03-07 00:18:18 +0000252/// Return a hopefully unique identifier for this block.
253std::string MachineBasicBlock::getFullName() const {
254 std::string Name;
255 if (getParent())
Craig Topper96601ca2012-08-22 06:07:19 +0000256 Name = (getParent()->getName() + ":").str();
Andrew Trick8ceaa662012-03-07 00:18:18 +0000257 if (getBasicBlock())
258 Name += getBasicBlock()->getName();
259 else
260 Name += (Twine("BB") + Twine(getNumber())).str();
261 return Name;
262}
263
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000264void MachineBasicBlock::print(raw_ostream &OS, SlotIndexes *Indexes) const {
Evan Cheng13d82852007-02-10 02:38:19 +0000265 const MachineFunction *MF = getParent();
Chris Lattner6371ed52009-08-23 00:35:30 +0000266 if (!MF) {
Chris Lattner52c09d72004-10-26 15:43:42 +0000267 OS << "Can't print out MachineBasicBlock because parent MachineFunction"
268 << " is null\n";
Tanya Lattner792699c2004-05-24 06:11:51 +0000269 return;
270 }
Alkis Evlogimenosa6382862004-09-05 18:39:20 +0000271
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000272 if (Indexes)
273 OS << Indexes->getMBBStartIdx(this) << '\t';
274
Dan Gohman0ba90f32009-10-31 20:19:03 +0000275 OS << "BB#" << getNumber() << ": ";
276
277 const char *Comma = "";
278 if (const BasicBlock *LBB = getBasicBlock()) {
279 OS << Comma << "derived from LLVM BB ";
Stephen Hines36b56882014-04-23 16:57:46 -0700280 LBB->printAsOperand(OS, /*PrintType=*/false);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000281 Comma = ", ";
282 }
283 if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; }
284 if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; }
Bill Wendling4b8e1fd2012-06-15 19:30:42 +0000285 if (Alignment)
Jakob Stoklund Olesenf2e94452011-12-06 21:08:39 +0000286 OS << Comma << "Align " << Alignment << " (" << (1u << Alignment)
287 << " bytes)";
Jakob Stoklund Olesenf2e94452011-12-06 21:08:39 +0000288
Chris Lattner6371ed52009-08-23 00:35:30 +0000289 OS << '\n';
Evan Cheng13d82852007-02-10 02:38:19 +0000290
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000291 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
Dan Gohmancb406c22007-10-03 19:26:29 +0000292 if (!livein_empty()) {
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000293 if (Indexes) OS << '\t';
Dan Gohman0ba90f32009-10-31 20:19:03 +0000294 OS << " Live Ins:";
Dan Gohman81bf03e2010-04-13 16:57:55 +0000295 for (livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I)
Jakob Stoklund Olesen668c9e32011-01-13 00:57:35 +0000296 OS << ' ' << PrintReg(*I, TRI);
Chris Lattner6371ed52009-08-23 00:35:30 +0000297 OS << '\n';
Evan Cheng13d82852007-02-10 02:38:19 +0000298 }
Chris Lattner681764b2006-09-26 03:41:59 +0000299 // Print the preds of this block according to the CFG.
300 if (!pred_empty()) {
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000301 if (Indexes) OS << '\t';
Chris Lattner681764b2006-09-26 03:41:59 +0000302 OS << " Predecessors according to CFG:";
303 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000304 OS << " BB#" << (*PI)->getNumber();
Chris Lattner6371ed52009-08-23 00:35:30 +0000305 OS << '\n';
Chris Lattner681764b2006-09-26 03:41:59 +0000306 }
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000307
Evan Chengddfd1372011-12-14 02:11:42 +0000308 for (const_instr_iterator I = instr_begin(); I != instr_end(); ++I) {
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000309 if (Indexes) {
310 if (Indexes->hasIndex(I))
311 OS << Indexes->getInstructionIndex(I);
312 OS << '\t';
313 }
Chris Lattner2d8e3d22009-08-23 00:47:04 +0000314 OS << '\t';
Evan Chengddfd1372011-12-14 02:11:42 +0000315 if (I->isInsideBundle())
316 OS << " * ";
Alkis Evlogimenosa6382862004-09-05 18:39:20 +0000317 I->print(OS, &getParent()->getTarget());
318 }
Chris Lattner380ae492005-04-01 06:48:38 +0000319
320 // Print the successors of this block according to the CFG.
321 if (!succ_empty()) {
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000322 if (Indexes) OS << '\t';
Chris Lattner380ae492005-04-01 06:48:38 +0000323 OS << " Successors according to CFG:";
Jakob Stoklund Olesencb6889b2012-08-13 23:13:23 +0000324 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000325 OS << " BB#" << (*SI)->getNumber();
Jakob Stoklund Olesencb6889b2012-08-13 23:13:23 +0000326 if (!Weights.empty())
327 OS << '(' << *getWeightIterator(SI) << ')';
328 }
Chris Lattner6371ed52009-08-23 00:35:30 +0000329 OS << '\n';
Chris Lattner380ae492005-04-01 06:48:38 +0000330 }
Alkis Evlogimenosaad5c052004-02-16 07:17:43 +0000331}
Chris Lattner52c09d72004-10-26 15:43:42 +0000332
Stephen Hines36b56882014-04-23 16:57:46 -0700333void MachineBasicBlock::printAsOperand(raw_ostream &OS, bool /*PrintType*/) {
334 OS << "BB#" << getNumber();
335}
336
Evan Chengb371f452007-02-19 21:49:54 +0000337void MachineBasicBlock::removeLiveIn(unsigned Reg) {
Dan Gohman81bf03e2010-04-13 16:57:55 +0000338 std::vector<unsigned>::iterator I =
339 std::find(LiveIns.begin(), LiveIns.end(), Reg);
Jakob Stoklund Olesen78836f02012-03-28 20:11:42 +0000340 if (I != LiveIns.end())
341 LiveIns.erase(I);
Evan Chengb371f452007-02-19 21:49:54 +0000342}
343
Evan Chenga971dbd2008-04-24 09:06:33 +0000344bool MachineBasicBlock::isLiveIn(unsigned Reg) const {
Dan Gohman81bf03e2010-04-13 16:57:55 +0000345 livein_iterator I = std::find(livein_begin(), livein_end(), Reg);
Evan Chenga971dbd2008-04-24 09:06:33 +0000346 return I != livein_end();
347}
348
Jakob Stoklund Olesenf6476522013-07-03 23:56:20 +0000349unsigned
350MachineBasicBlock::addLiveIn(unsigned PhysReg, const TargetRegisterClass *RC) {
351 assert(getParent() && "MBB must be inserted in function");
352 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) && "Expected physreg");
353 assert(RC && "Register class is required");
354 assert((isLandingPad() || this == &getParent()->front()) &&
355 "Only the entry block and landing pads can have physreg live ins");
356
357 bool LiveIn = isLiveIn(PhysReg);
Jakob Stoklund Olesenc982e142013-07-04 04:32:35 +0000358 iterator I = SkipPHIsAndLabels(begin()), E = end();
Jakob Stoklund Olesenf6476522013-07-03 23:56:20 +0000359 MachineRegisterInfo &MRI = getParent()->getRegInfo();
360 const TargetInstrInfo &TII = *getParent()->getTarget().getInstrInfo();
361
362 // Look for an existing copy.
363 if (LiveIn)
364 for (;I != E && I->isCopy(); ++I)
365 if (I->getOperand(1).getReg() == PhysReg) {
366 unsigned VirtReg = I->getOperand(0).getReg();
367 if (!MRI.constrainRegClass(VirtReg, RC))
368 llvm_unreachable("Incompatible live-in register class.");
369 return VirtReg;
370 }
371
372 // No luck, create a virtual register.
373 unsigned VirtReg = MRI.createVirtualRegister(RC);
374 BuildMI(*this, I, DebugLoc(), TII.get(TargetOpcode::COPY), VirtReg)
375 .addReg(PhysReg, RegState::Kill);
376 if (!LiveIn)
377 addLiveIn(PhysReg);
378 return VirtReg;
379}
380
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000381void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000382 getParent()->splice(NewAfter, this);
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000383}
384
385void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) {
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000386 MachineFunction::iterator BBI = NewBefore;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000387 getParent()->splice(++BBI, this);
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000388}
389
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000390void MachineBasicBlock::updateTerminator() {
391 const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo();
392 // A block with no successors has no concerns with fall-through edges.
393 if (this->succ_empty()) return;
394
395 MachineBasicBlock *TBB = 0, *FBB = 0;
396 SmallVector<MachineOperand, 4> Cond;
Stuart Hastings3bf91252010-06-17 22:43:56 +0000397 DebugLoc dl; // FIXME: this is nowhere
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000398 bool B = TII->AnalyzeBranch(*this, TBB, FBB, Cond);
399 (void) B;
400 assert(!B && "UpdateTerminators requires analyzable predecessors!");
401 if (Cond.empty()) {
402 if (TBB) {
403 // The block has an unconditional branch. If its successor is now
404 // its layout successor, delete the branch.
405 if (isLayoutSuccessor(TBB))
406 TII->RemoveBranch(*this);
407 } else {
408 // The block has an unconditional fallthrough. If its successor is not
Chandler Carruth3b7b2092011-11-22 13:13:16 +0000409 // its layout successor, insert a branch. First we have to locate the
410 // only non-landing-pad successor, as that is the fallthrough block.
411 for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) {
412 if ((*SI)->isLandingPad())
413 continue;
414 assert(!TBB && "Found more than one non-landing-pad successor!");
415 TBB = *SI;
416 }
Chandler Carruth521fc5b2011-11-23 08:23:54 +0000417
418 // If there is no non-landing-pad successor, the block has no
419 // fall-through edges to be concerned with.
420 if (!TBB)
421 return;
422
423 // Finally update the unconditional successor to be reached via a branch
424 // if it would not be reached by fallthrough.
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000425 if (!isLayoutSuccessor(TBB))
Stuart Hastings3bf91252010-06-17 22:43:56 +0000426 TII->InsertBranch(*this, TBB, 0, Cond, dl);
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000427 }
428 } else {
429 if (FBB) {
430 // The block has a non-fallthrough conditional branch. If one of its
431 // successors is its layout successor, rewrite it to a fallthrough
432 // conditional branch.
433 if (isLayoutSuccessor(TBB)) {
Jakob Stoklund Olesene0239932009-11-22 18:28:04 +0000434 if (TII->ReverseBranchCondition(Cond))
435 return;
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000436 TII->RemoveBranch(*this);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000437 TII->InsertBranch(*this, FBB, 0, Cond, dl);
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000438 } else if (isLayoutSuccessor(FBB)) {
439 TII->RemoveBranch(*this);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000440 TII->InsertBranch(*this, TBB, 0, Cond, dl);
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000441 }
442 } else {
Chandler Carruthf1a60c72012-04-16 22:03:00 +0000443 // Walk through the successors and find the successor which is not
444 // a landing pad and is not the conditional branch destination (in TBB)
445 // as the fallthrough successor.
446 MachineBasicBlock *FallthroughBB = 0;
447 for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) {
448 if ((*SI)->isLandingPad() || *SI == TBB)
449 continue;
450 assert(!FallthroughBB && "Found more than one fallthrough successor.");
451 FallthroughBB = *SI;
452 }
453 if (!FallthroughBB && canFallThrough()) {
454 // We fallthrough to the same basic block as the conditional jump
455 // targets. Remove the conditional jump, leaving unconditional
456 // fallthrough.
457 // FIXME: This does not seem like a reasonable pattern to support, but it
458 // has been seen in the wild coming out of degenerate ARM test cases.
459 TII->RemoveBranch(*this);
460
461 // Finally update the unconditional successor to be reached via a branch
462 // if it would not be reached by fallthrough.
463 if (!isLayoutSuccessor(TBB))
464 TII->InsertBranch(*this, TBB, 0, Cond, dl);
465 return;
466 }
467
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000468 // The block has a fallthrough conditional branch.
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000469 if (isLayoutSuccessor(TBB)) {
Jakob Stoklund Olesene0239932009-11-22 18:28:04 +0000470 if (TII->ReverseBranchCondition(Cond)) {
471 // We can't reverse the condition, add an unconditional branch.
472 Cond.clear();
Chandler Carruthf1a60c72012-04-16 22:03:00 +0000473 TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl);
Jakob Stoklund Olesene0239932009-11-22 18:28:04 +0000474 return;
475 }
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000476 TII->RemoveBranch(*this);
Chandler Carruthf1a60c72012-04-16 22:03:00 +0000477 TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl);
478 } else if (!isLayoutSuccessor(FallthroughBB)) {
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000479 TII->RemoveBranch(*this);
Chandler Carruthf1a60c72012-04-16 22:03:00 +0000480 TII->InsertBranch(*this, TBB, FallthroughBB, Cond, dl);
Jim Grosbach7707a0d2009-11-12 03:55:33 +0000481 }
482 }
483 }
484}
Chris Lattnerc585a3f2006-10-24 00:02:26 +0000485
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000486void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ, uint32_t weight) {
487
488 // If we see non-zero value for the first time it means we actually use Weight
489 // list, so we fill all Weights with 0's.
490 if (weight != 0 && Weights.empty())
491 Weights.resize(Successors.size());
492
493 if (weight != 0 || !Weights.empty())
494 Weights.push_back(weight);
495
496 Successors.push_back(succ);
497 succ->addPredecessor(this);
498 }
Chris Lattner52c09d72004-10-26 15:43:42 +0000499
500void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) {
501 succ->removePredecessor(this);
502 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ);
503 assert(I != Successors.end() && "Not a current successor!");
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000504
505 // If Weight list is empty it means we don't use it (disabled optimization).
506 if (!Weights.empty()) {
507 weight_iterator WI = getWeightIterator(I);
508 Weights.erase(WI);
509 }
510
Chris Lattner52c09d72004-10-26 15:43:42 +0000511 Successors.erase(I);
512}
513
Jakub Staszak12af5ff2011-06-16 18:01:17 +0000514MachineBasicBlock::succ_iterator
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000515MachineBasicBlock::removeSuccessor(succ_iterator I) {
Chris Lattner52c09d72004-10-26 15:43:42 +0000516 assert(I != Successors.end() && "Not a current successor!");
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000517
518 // If Weight list is empty it means we don't use it (disabled optimization).
519 if (!Weights.empty()) {
520 weight_iterator WI = getWeightIterator(I);
521 Weights.erase(WI);
522 }
523
Chris Lattner52c09d72004-10-26 15:43:42 +0000524 (*I)->removePredecessor(this);
Dan Gohman5d5ee802009-01-08 22:19:34 +0000525 return Successors.erase(I);
Chris Lattner52c09d72004-10-26 15:43:42 +0000526}
527
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000528void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old,
529 MachineBasicBlock *New) {
Jakob Stoklund Olesen15121ca2012-08-10 03:23:27 +0000530 if (Old == New)
531 return;
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000532
Jakob Stoklund Olesen15121ca2012-08-10 03:23:27 +0000533 succ_iterator E = succ_end();
534 succ_iterator NewI = E;
535 succ_iterator OldI = E;
536 for (succ_iterator I = succ_begin(); I != E; ++I) {
537 if (*I == Old) {
538 OldI = I;
539 if (NewI != E)
540 break;
541 }
542 if (*I == New) {
543 NewI = I;
544 if (OldI != E)
545 break;
546 }
547 }
548 assert(OldI != E && "Old is not a successor of this block");
549 Old->removePredecessor(this);
550
551 // If New isn't already a successor, let it take Old's place.
552 if (NewI == E) {
553 New->addPredecessor(this);
554 *OldI = New;
555 return;
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000556 }
557
Jakob Stoklund Olesen15121ca2012-08-10 03:23:27 +0000558 // New is already a successor.
559 // Update its weight instead of adding a duplicate edge.
560 if (!Weights.empty()) {
561 weight_iterator OldWI = getWeightIterator(OldI);
562 *getWeightIterator(NewI) += *OldWI;
563 Weights.erase(OldWI);
564 }
565 Successors.erase(OldI);
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000566}
567
Chris Lattner52c09d72004-10-26 15:43:42 +0000568void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) {
569 Predecessors.push_back(pred);
570}
571
572void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) {
Eli Friedman6dda9162011-04-18 21:21:37 +0000573 pred_iterator I = std::find(Predecessors.begin(), Predecessors.end(), pred);
Chris Lattner52c09d72004-10-26 15:43:42 +0000574 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!");
575 Predecessors.erase(I);
576}
Evan Cheng4f098782007-05-17 23:58:53 +0000577
Chris Lattner6371ed52009-08-23 00:35:30 +0000578void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) {
Mon P Wang63307c32008-05-05 19:05:59 +0000579 if (this == fromMBB)
580 return;
Jakub Staszak12af5ff2011-06-16 18:01:17 +0000581
Dan Gohman14152b42010-07-06 20:24:04 +0000582 while (!fromMBB->succ_empty()) {
583 MachineBasicBlock *Succ = *fromMBB->succ_begin();
Jakob Stoklund Olesen03e593e2012-08-13 23:13:25 +0000584 uint32_t Weight = 0;
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000585
586 // If Weight list is empty it means we don't use it (disabled optimization).
587 if (!fromMBB->Weights.empty())
Jakob Stoklund Olesen03e593e2012-08-13 23:13:25 +0000588 Weight = *fromMBB->Weights.begin();
Jakub Staszak7cc2b072011-06-16 20:22:37 +0000589
Jakob Stoklund Olesen03e593e2012-08-13 23:13:25 +0000590 addSuccessor(Succ, Weight);
Dan Gohman14152b42010-07-06 20:24:04 +0000591 fromMBB->removeSuccessor(Succ);
592 }
593}
594
595void
596MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB) {
597 if (this == fromMBB)
598 return;
Jakub Staszak12af5ff2011-06-16 18:01:17 +0000599
Dan Gohman14152b42010-07-06 20:24:04 +0000600 while (!fromMBB->succ_empty()) {
601 MachineBasicBlock *Succ = *fromMBB->succ_begin();
Jakob Stoklund Olesen03e593e2012-08-13 23:13:25 +0000602 uint32_t Weight = 0;
603 if (!fromMBB->Weights.empty())
604 Weight = *fromMBB->Weights.begin();
605 addSuccessor(Succ, Weight);
Dan Gohman14152b42010-07-06 20:24:04 +0000606 fromMBB->removeSuccessor(Succ);
607
608 // Fix up any PHI nodes in the successor.
Evan Chengddfd1372011-12-14 02:11:42 +0000609 for (MachineBasicBlock::instr_iterator MI = Succ->instr_begin(),
610 ME = Succ->instr_end(); MI != ME && MI->isPHI(); ++MI)
Dan Gohman14152b42010-07-06 20:24:04 +0000611 for (unsigned i = 2, e = MI->getNumOperands()+1; i != e; i += 2) {
612 MachineOperand &MO = MI->getOperand(i);
613 if (MO.getMBB() == fromMBB)
614 MO.setMBB(this);
615 }
616 }
Mon P Wang63307c32008-05-05 19:05:59 +0000617}
618
Jakob Stoklund Olesenf192b502012-07-30 17:36:47 +0000619bool MachineBasicBlock::isPredecessor(const MachineBasicBlock *MBB) const {
620 return std::find(pred_begin(), pred_end(), MBB) != pred_end();
621}
622
Dan Gohman6d1b89e2009-03-30 20:06:29 +0000623bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const {
Jakob Stoklund Olesenf192b502012-07-30 17:36:47 +0000624 return std::find(succ_begin(), succ_end(), MBB) != succ_end();
Evan Cheng4f098782007-05-17 23:58:53 +0000625}
Evan Cheng0370fad2007-06-04 06:44:01 +0000626
Dan Gohman6d1b89e2009-03-30 20:06:29 +0000627bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const {
Dan Gohman6ade6f52008-10-02 22:09:09 +0000628 MachineFunction::const_iterator I(this);
Stephen Hines36b56882014-04-23 16:57:46 -0700629 return std::next(I) == MachineFunction::const_iterator(MBB);
Dan Gohman6ade6f52008-10-02 22:09:09 +0000630}
631
Bob Wilson15acadd2009-11-26 00:32:21 +0000632bool MachineBasicBlock::canFallThrough() {
Bob Wilson15acadd2009-11-26 00:32:21 +0000633 MachineFunction::iterator Fallthrough = this;
634 ++Fallthrough;
635 // If FallthroughBlock is off the end of the function, it can't fall through.
636 if (Fallthrough == getParent()->end())
637 return false;
638
639 // If FallthroughBlock isn't a successor, no fallthrough is possible.
640 if (!isSuccessor(Fallthrough))
641 return false;
642
Dan Gohman735985f2009-12-05 00:32:59 +0000643 // Analyze the branches, if any, at the end of the block.
644 MachineBasicBlock *TBB = 0, *FBB = 0;
645 SmallVector<MachineOperand, 4> Cond;
646 const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo();
Jakob Stoklund Olesen33cc8d62010-01-15 20:00:12 +0000647 if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) {
Dan Gohman735985f2009-12-05 00:32:59 +0000648 // If we couldn't analyze the branch, examine the last instruction.
649 // If the block doesn't end in a known control barrier, assume fallthrough
Chad Rosier0162ff42012-01-26 18:24:25 +0000650 // is possible. The isPredicated check is needed because this code can be
Dan Gohman735985f2009-12-05 00:32:59 +0000651 // called during IfConversion, where an instruction which is normally a
Chad Rosier6a5d0e22012-01-26 20:19:05 +0000652 // Barrier is predicated and thus no longer an actual control barrier.
Chad Rosier0162ff42012-01-26 18:24:25 +0000653 return empty() || !back().isBarrier() || TII->isPredicated(&back());
Dan Gohman735985f2009-12-05 00:32:59 +0000654 }
Bob Wilson15acadd2009-11-26 00:32:21 +0000655
656 // If there is no branch, control always falls through.
657 if (TBB == 0) return true;
658
659 // If there is some explicit branch to the fallthrough block, it can obviously
660 // reach, even though the branch should get folded to fall through implicitly.
661 if (MachineFunction::iterator(TBB) == Fallthrough ||
662 MachineFunction::iterator(FBB) == Fallthrough)
663 return true;
664
665 // If it's an unconditional branch to some block not the fall through, it
666 // doesn't fall through.
667 if (Cond.empty()) return false;
668
669 // Otherwise, if it is conditional and has no explicit false block, it falls
670 // through.
671 return FBB == 0;
672}
673
Dan Gohman853d3fb2010-06-22 17:25:57 +0000674MachineBasicBlock *
675MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P) {
Evan Chengddb14202012-04-24 19:06:55 +0000676 // Splitting the critical edge to a landing pad block is non-trivial. Don't do
677 // it in this generic function.
678 if (Succ->isLandingPad())
679 return NULL;
680
Dan Gohman853d3fb2010-06-22 17:25:57 +0000681 MachineFunction *MF = getParent();
682 DebugLoc dl; // FIXME: this is nowhere
683
Stephen Hines36b56882014-04-23 16:57:46 -0700684 // Performance might be harmed on HW that implements branching using exec mask
685 // where both sides of the branches are always executed.
686 if (MF->getTarget().requiresStructuredCFG())
687 return NULL;
688
Jakob Stoklund Olesen371e82b2010-11-02 00:58:37 +0000689 // We may need to update this's terminator, but we can't do that if
690 // AnalyzeBranch fails. If this uses a jump table, we won't touch it.
Dan Gohman853d3fb2010-06-22 17:25:57 +0000691 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
692 MachineBasicBlock *TBB = 0, *FBB = 0;
693 SmallVector<MachineOperand, 4> Cond;
694 if (TII->AnalyzeBranch(*this, TBB, FBB, Cond))
695 return NULL;
696
Jakob Stoklund Olesen371e82b2010-11-02 00:58:37 +0000697 // Avoid bugpoint weirdness: A block may end with a conditional branch but
698 // jumps to the same MBB is either case. We have duplicate CFG edges in that
699 // case that we can't handle. Since this never happens in properly optimized
700 // code, just skip those edges.
701 if (TBB && TBB == FBB) {
702 DEBUG(dbgs() << "Won't split critical edge after degenerate BB#"
703 << getNumber() << '\n');
704 return NULL;
705 }
706
Dan Gohman853d3fb2010-06-22 17:25:57 +0000707 MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock();
Stephen Hines36b56882014-04-23 16:57:46 -0700708 MF->insert(std::next(MachineFunction::iterator(this)), NMBB);
Evan Cheng087fbeb2010-08-17 17:15:14 +0000709 DEBUG(dbgs() << "Splitting critical edge:"
Dan Gohman853d3fb2010-06-22 17:25:57 +0000710 " BB#" << getNumber()
711 << " -- BB#" << NMBB->getNumber()
712 << " -- BB#" << Succ->getNumber() << '\n');
Cameron Zwarichdd58fa42013-02-12 03:49:20 +0000713
714 LiveIntervals *LIS = P->getAnalysisIfAvailable<LiveIntervals>();
Cameron Zwarichf5844a72013-02-10 23:29:54 +0000715 SlotIndexes *Indexes = P->getAnalysisIfAvailable<SlotIndexes>();
Cameron Zwarichdd58fa42013-02-12 03:49:20 +0000716 if (LIS)
717 LIS->insertMBBInMaps(NMBB);
718 else if (Indexes)
Cameron Zwarichf5844a72013-02-10 23:29:54 +0000719 Indexes->insertMBBInMaps(NMBB);
Dan Gohman853d3fb2010-06-22 17:25:57 +0000720
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000721 // On some targets like Mips, branches may kill virtual registers. Make sure
722 // that LiveVariables is properly updated after updateTerminator replaces the
723 // terminators.
724 LiveVariables *LV = P->getAnalysisIfAvailable<LiveVariables>();
725
726 // Collect a list of virtual registers killed by the terminators.
727 SmallVector<unsigned, 4> KilledRegs;
728 if (LV)
Evan Chengddfd1372011-12-14 02:11:42 +0000729 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
Evan Cheng7c2a4a32011-12-06 22:12:01 +0000730 I != E; ++I) {
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000731 MachineInstr *MI = I;
732 for (MachineInstr::mop_iterator OI = MI->operands_begin(),
733 OE = MI->operands_end(); OI != OE; ++OI) {
Lang Hames72a043f2012-02-09 05:59:36 +0000734 if (!OI->isReg() || OI->getReg() == 0 ||
735 !OI->isUse() || !OI->isKill() || OI->isUndef())
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000736 continue;
737 unsigned Reg = OI->getReg();
Lang Hames72a043f2012-02-09 05:59:36 +0000738 if (TargetRegisterInfo::isPhysicalRegister(Reg) ||
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000739 LV->getVarInfo(Reg).removeKill(MI)) {
740 KilledRegs.push_back(Reg);
741 DEBUG(dbgs() << "Removing terminator kill: " << *MI);
742 OI->setIsKill(false);
743 }
744 }
745 }
746
Cameron Zwarichf0b25352013-02-17 00:10:44 +0000747 SmallVector<unsigned, 4> UsedRegs;
748 if (LIS) {
749 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
750 I != E; ++I) {
751 MachineInstr *MI = I;
752
753 for (MachineInstr::mop_iterator OI = MI->operands_begin(),
754 OE = MI->operands_end(); OI != OE; ++OI) {
755 if (!OI->isReg() || OI->getReg() == 0)
756 continue;
757
758 unsigned Reg = OI->getReg();
759 if (std::find(UsedRegs.begin(), UsedRegs.end(), Reg) == UsedRegs.end())
760 UsedRegs.push_back(Reg);
761 }
762 }
763 }
764
Dan Gohman853d3fb2010-06-22 17:25:57 +0000765 ReplaceUsesOfBlockWith(Succ, NMBB);
Cameron Zwarichcbe3f5e2013-02-11 09:24:45 +0000766
767 // If updateTerminator() removes instructions, we need to remove them from
768 // SlotIndexes.
769 SmallVector<MachineInstr*, 4> Terminators;
770 if (Indexes) {
771 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
772 I != E; ++I)
773 Terminators.push_back(I);
774 }
775
Dan Gohman853d3fb2010-06-22 17:25:57 +0000776 updateTerminator();
777
Cameron Zwarichcbe3f5e2013-02-11 09:24:45 +0000778 if (Indexes) {
779 SmallVector<MachineInstr*, 4> NewTerminators;
780 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end();
781 I != E; ++I)
782 NewTerminators.push_back(I);
783
784 for (SmallVectorImpl<MachineInstr*>::iterator I = Terminators.begin(),
785 E = Terminators.end(); I != E; ++I) {
786 if (std::find(NewTerminators.begin(), NewTerminators.end(), *I) ==
787 NewTerminators.end())
788 Indexes->removeMachineInstrFromMaps(*I);
789 }
790 }
791
Dan Gohman853d3fb2010-06-22 17:25:57 +0000792 // Insert unconditional "jump Succ" instruction in NMBB if necessary.
793 NMBB->addSuccessor(Succ);
794 if (!NMBB->isLayoutSuccessor(Succ)) {
795 Cond.clear();
796 MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, Succ, NULL, Cond, dl);
Cameron Zwarichf5844a72013-02-10 23:29:54 +0000797
798 if (Indexes) {
799 for (instr_iterator I = NMBB->instr_begin(), E = NMBB->instr_end();
800 I != E; ++I) {
801 // Some instructions may have been moved to NMBB by updateTerminator(),
802 // so we first remove any instruction that already has an index.
803 if (Indexes->hasIndex(I))
804 Indexes->removeMachineInstrFromMaps(I);
805 Indexes->insertMachineInstrInMaps(I);
806 }
807 }
Dan Gohman853d3fb2010-06-22 17:25:57 +0000808 }
809
810 // Fix PHI nodes in Succ so they refer to NMBB instead of this
Evan Chengddfd1372011-12-14 02:11:42 +0000811 for (MachineBasicBlock::instr_iterator
812 i = Succ->instr_begin(),e = Succ->instr_end();
813 i != e && i->isPHI(); ++i)
Dan Gohman853d3fb2010-06-22 17:25:57 +0000814 for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2)
815 if (i->getOperand(ni+1).getMBB() == this)
816 i->getOperand(ni+1).setMBB(NMBB);
817
Jakob Stoklund Olesenac7caa02011-10-14 17:25:46 +0000818 // Inherit live-ins from the successor
819 for (MachineBasicBlock::livein_iterator I = Succ->livein_begin(),
Bill Wendling96cb1122012-07-19 00:04:14 +0000820 E = Succ->livein_end(); I != E; ++I)
Jakob Stoklund Olesenac7caa02011-10-14 17:25:46 +0000821 NMBB->addLiveIn(*I);
822
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000823 // Update LiveVariables.
Lang Hames72a043f2012-02-09 05:59:36 +0000824 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000825 if (LV) {
826 // Restore kills of virtual registers that were killed by the terminators.
827 while (!KilledRegs.empty()) {
828 unsigned Reg = KilledRegs.pop_back_val();
Evan Chengddfd1372011-12-14 02:11:42 +0000829 for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) {
Lang Hames72a043f2012-02-09 05:59:36 +0000830 if (!(--I)->addRegisterKilled(Reg, TRI, /* addIfNotFound= */ false))
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000831 continue;
Lang Hames72a043f2012-02-09 05:59:36 +0000832 if (TargetRegisterInfo::isVirtualRegister(Reg))
833 LV->getVarInfo(Reg).Kills.push_back(I);
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000834 DEBUG(dbgs() << "Restored terminator kill: " << *I);
835 break;
836 }
837 }
838 // Update relevant live-through information.
Dan Gohman853d3fb2010-06-22 17:25:57 +0000839 LV->addNewBlock(NMBB, this, Succ);
Jakob Stoklund Olesen57903352011-05-29 20:10:28 +0000840 }
Dan Gohman853d3fb2010-06-22 17:25:57 +0000841
Cameron Zwarichdd58fa42013-02-12 03:49:20 +0000842 if (LIS) {
Cameron Zwarich8597c142013-02-11 09:24:47 +0000843 // After splitting the edge and updating SlotIndexes, live intervals may be
844 // in one of two situations, depending on whether this block was the last in
845 // the function. If the original block was the last in the function, all live
846 // intervals will end prior to the beginning of the new split block. If the
847 // original block was not at the end of the function, all live intervals will
848 // extend to the end of the new split block.
849
850 bool isLastMBB =
Stephen Hines36b56882014-04-23 16:57:46 -0700851 std::next(MachineFunction::iterator(NMBB)) == getParent()->end();
Cameron Zwarich8597c142013-02-11 09:24:47 +0000852
853 SlotIndex StartIndex = Indexes->getMBBEndIdx(this);
854 SlotIndex PrevIndex = StartIndex.getPrevSlot();
855 SlotIndex EndIndex = Indexes->getMBBEndIdx(NMBB);
856
857 // Find the registers used from NMBB in PHIs in Succ.
858 SmallSet<unsigned, 8> PHISrcRegs;
859 for (MachineBasicBlock::instr_iterator
860 I = Succ->instr_begin(), E = Succ->instr_end();
861 I != E && I->isPHI(); ++I) {
862 for (unsigned ni = 1, ne = I->getNumOperands(); ni != ne; ni += 2) {
863 if (I->getOperand(ni+1).getMBB() == NMBB) {
864 MachineOperand &MO = I->getOperand(ni);
865 unsigned Reg = MO.getReg();
866 PHISrcRegs.insert(Reg);
Cameron Zwarichdbf10c42013-02-12 03:49:17 +0000867 if (MO.isUndef())
868 continue;
Cameron Zwarich8597c142013-02-11 09:24:47 +0000869
870 LiveInterval &LI = LIS->getInterval(Reg);
871 VNInfo *VNI = LI.getVNInfoAt(PrevIndex);
872 assert(VNI && "PHI sources should be live out of their predecessors.");
Matthias Braun331de112013-10-10 21:28:43 +0000873 LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI));
Cameron Zwarich8597c142013-02-11 09:24:47 +0000874 }
875 }
876 }
877
878 MachineRegisterInfo *MRI = &getParent()->getRegInfo();
879 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
880 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
881 if (PHISrcRegs.count(Reg) || !LIS->hasInterval(Reg))
882 continue;
883
884 LiveInterval &LI = LIS->getInterval(Reg);
885 if (!LI.liveAt(PrevIndex))
886 continue;
887
Cameron Zwarichdbf10c42013-02-12 03:49:17 +0000888 bool isLiveOut = LI.liveAt(LIS->getMBBStartIdx(Succ));
Cameron Zwarich8597c142013-02-11 09:24:47 +0000889 if (isLiveOut && isLastMBB) {
890 VNInfo *VNI = LI.getVNInfoAt(PrevIndex);
891 assert(VNI && "LiveInterval should have VNInfo where it is live.");
Matthias Braun331de112013-10-10 21:28:43 +0000892 LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI));
Cameron Zwarich8597c142013-02-11 09:24:47 +0000893 } else if (!isLiveOut && !isLastMBB) {
Matthias Braun331de112013-10-10 21:28:43 +0000894 LI.removeSegment(StartIndex, EndIndex);
Cameron Zwarich8597c142013-02-11 09:24:47 +0000895 }
896 }
Cameron Zwarichf0b25352013-02-17 00:10:44 +0000897
898 // Update all intervals for registers whose uses may have been modified by
899 // updateTerminator().
Cameron Zwarich680c98f2013-02-17 11:09:00 +0000900 LIS->repairIntervalsInRange(this, getFirstTerminator(), end(), UsedRegs);
Cameron Zwarich8597c142013-02-11 09:24:47 +0000901 }
902
Dan Gohman853d3fb2010-06-22 17:25:57 +0000903 if (MachineDominatorTree *MDT =
Evan Cheng19708922010-08-19 23:32:47 +0000904 P->getAnalysisIfAvailable<MachineDominatorTree>()) {
905 // Update dominator information.
906 MachineDomTreeNode *SucccDTNode = MDT->getNode(Succ);
907
908 bool IsNewIDom = true;
909 for (const_pred_iterator PI = Succ->pred_begin(), E = Succ->pred_end();
910 PI != E; ++PI) {
911 MachineBasicBlock *PredBB = *PI;
912 if (PredBB == NMBB)
913 continue;
914 if (!MDT->dominates(SucccDTNode, MDT->getNode(PredBB))) {
915 IsNewIDom = false;
916 break;
917 }
918 }
919
920 // We know "this" dominates the newly created basic block.
921 MachineDomTreeNode *NewDTNode = MDT->addNewBlock(NMBB, this);
922
923 // If all the other predecessors of "Succ" are dominated by "Succ" itself
924 // then the new block is the new immediate dominator of "Succ". Otherwise,
925 // the new block doesn't dominate anything.
926 if (IsNewIDom)
927 MDT->changeImmediateDominator(SucccDTNode, NewDTNode);
928 }
Dan Gohman853d3fb2010-06-22 17:25:57 +0000929
Evan Chenge0083842010-08-17 17:43:50 +0000930 if (MachineLoopInfo *MLI = P->getAnalysisIfAvailable<MachineLoopInfo>())
Dan Gohman853d3fb2010-06-22 17:25:57 +0000931 if (MachineLoop *TIL = MLI->getLoopFor(this)) {
932 // If one or the other blocks were not in a loop, the new block is not
933 // either, and thus LI doesn't need to be updated.
934 if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) {
935 if (TIL == DestLoop) {
936 // Both in the same loop, the NMBB joins loop.
937 DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase());
938 } else if (TIL->contains(DestLoop)) {
939 // Edge from an outer loop to an inner loop. Add to the outer loop.
940 TIL->addBasicBlockToLoop(NMBB, MLI->getBase());
941 } else if (DestLoop->contains(TIL)) {
942 // Edge from an inner loop to an outer loop. Add to the outer loop.
943 DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase());
944 } else {
945 // Edge from two loops with no containment relation. Because these
946 // are natural loops, we know that the destination block must be the
947 // header of its loop (adding a branch into a loop elsewhere would
948 // create an irreducible loop).
949 assert(DestLoop->getHeader() == Succ &&
950 "Should not create irreducible loops!");
951 if (MachineLoop *P = DestLoop->getParentLoop())
952 P->addBasicBlockToLoop(NMBB, MLI->getBase());
953 }
954 }
955 }
956
957 return NMBB;
958}
959
Jakob Stoklund Olesen9f4692d2012-12-17 23:55:38 +0000960/// Prepare MI to be removed from its bundle. This fixes bundle flags on MI's
961/// neighboring instructions so the bundle won't be broken by removing MI.
962static void unbundleSingleMI(MachineInstr *MI) {
963 // Removing the first instruction in a bundle.
964 if (MI->isBundledWithSucc() && !MI->isBundledWithPred())
965 MI->unbundleFromSucc();
966 // Removing the last instruction in a bundle.
967 if (MI->isBundledWithPred() && !MI->isBundledWithSucc())
968 MI->unbundleFromPred();
969 // If MI is not bundled, or if it is internal to a bundle, the neighbor flags
970 // are already fine.
Evan Chengddfd1372011-12-14 02:11:42 +0000971}
972
Jakob Stoklund Olesen9f4692d2012-12-17 23:55:38 +0000973MachineBasicBlock::instr_iterator
974MachineBasicBlock::erase(MachineBasicBlock::instr_iterator I) {
975 unbundleSingleMI(I);
976 return Insts.erase(I);
977}
Evan Chengddfd1372011-12-14 02:11:42 +0000978
Jakob Stoklund Olesen9f4692d2012-12-17 23:55:38 +0000979MachineInstr *MachineBasicBlock::remove_instr(MachineInstr *MI) {
980 unbundleSingleMI(MI);
981 MI->clearFlag(MachineInstr::BundledPred);
982 MI->clearFlag(MachineInstr::BundledSucc);
983 return Insts.remove(MI);
Evan Chengddfd1372011-12-14 02:11:42 +0000984}
985
Jakob Stoklund Olesenedc35032012-12-18 17:54:53 +0000986MachineBasicBlock::instr_iterator
987MachineBasicBlock::insert(instr_iterator I, MachineInstr *MI) {
988 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() &&
989 "Cannot insert instruction with bundle flags");
990 // Set the bundle flags when inserting inside a bundle.
991 if (I != instr_end() && I->isBundledWithPred()) {
992 MI->setFlag(MachineInstr::BundledPred);
993 MI->setFlag(MachineInstr::BundledSucc);
994 }
995 return Insts.insert(I, MI);
996}
997
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000998/// removeFromParent - This method unlinks 'this' from the containing function,
999/// and returns it, but does not delete it.
1000MachineBasicBlock *MachineBasicBlock::removeFromParent() {
1001 assert(getParent() && "Not embedded in a function!");
1002 getParent()->remove(this);
1003 return this;
1004}
1005
1006
1007/// eraseFromParent - This method unlinks 'this' from the containing function,
1008/// and deletes it.
1009void MachineBasicBlock::eraseFromParent() {
1010 assert(getParent() && "Not embedded in a function!");
1011 getParent()->erase(this);
1012}
1013
1014
Evan Cheng0370fad2007-06-04 06:44:01 +00001015/// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
1016/// 'Old', change the code and CFG so that it branches to 'New' instead.
1017void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old,
1018 MachineBasicBlock *New) {
1019 assert(Old != New && "Cannot replace self with self!");
1020
Evan Chengddfd1372011-12-14 02:11:42 +00001021 MachineBasicBlock::instr_iterator I = instr_end();
1022 while (I != instr_begin()) {
Evan Cheng0370fad2007-06-04 06:44:01 +00001023 --I;
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001024 if (!I->isTerminator()) break;
Evan Cheng0370fad2007-06-04 06:44:01 +00001025
1026 // Scan the operands of this machine instruction, replacing any uses of Old
1027 // with New.
1028 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
Dan Gohmand735b802008-10-03 15:45:36 +00001029 if (I->getOperand(i).isMBB() &&
Dan Gohman014278e2008-09-13 17:58:21 +00001030 I->getOperand(i).getMBB() == Old)
Chris Lattner8aa797a2007-12-30 23:10:15 +00001031 I->getOperand(i).setMBB(New);
Evan Cheng0370fad2007-06-04 06:44:01 +00001032 }
1033
Dan Gohman5412d062009-05-05 21:10:19 +00001034 // Update the successor information.
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001035 replaceSuccessor(Old, New);
Evan Cheng0370fad2007-06-04 06:44:01 +00001036}
1037
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001038/// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the
1039/// CFG to be inserted. If we have proven that MBB can only branch to DestA and
Bill Wendlingc70d33112009-12-16 00:08:36 +00001040/// DestB, remove any other MBB successors from the CFG. DestA and DestB can be
1041/// null.
Jakub Staszak12af5ff2011-06-16 18:01:17 +00001042///
Chris Lattnerf20c1a42007-12-31 04:56:33 +00001043/// Besides DestA and DestB, retain other edges leading to LandingPads
1044/// (currently there can be only one; we don't check or require that here).
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001045/// Note it is possible that DestA and/or DestB are LandingPads.
1046bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA,
1047 MachineBasicBlock *DestB,
1048 bool isCond) {
Bill Wendlingc70d33112009-12-16 00:08:36 +00001049 // The values of DestA and DestB frequently come from a call to the
1050 // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial
1051 // values from there.
1052 //
1053 // 1. If both DestA and DestB are null, then the block ends with no branches
1054 // (it falls through to its successor).
1055 // 2. If DestA is set, DestB is null, and isCond is false, then the block ends
1056 // with only an unconditional branch.
1057 // 3. If DestA is set, DestB is null, and isCond is true, then the block ends
1058 // with a conditional branch that falls through to a successor (DestB).
1059 // 4. If DestA and DestB is set and isCond is true, then the block ends with a
1060 // conditional branch followed by an unconditional branch. DestA is the
1061 // 'true' destination and DestB is the 'false' destination.
1062
Bill Wendlinge543d162010-04-01 00:00:43 +00001063 bool Changed = false;
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001064
Chris Lattner7896c9f2009-12-03 00:50:42 +00001065 MachineFunction::iterator FallThru =
Stephen Hines36b56882014-04-23 16:57:46 -07001066 std::next(MachineFunction::iterator(this));
Bill Wendlinge543d162010-04-01 00:00:43 +00001067
1068 if (DestA == 0 && DestB == 0) {
1069 // Block falls through to successor.
1070 DestA = FallThru;
1071 DestB = FallThru;
1072 } else if (DestA != 0 && DestB == 0) {
1073 if (isCond)
1074 // Block ends in conditional jump that falls through to successor.
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001075 DestB = FallThru;
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001076 } else {
Bill Wendlinge543d162010-04-01 00:00:43 +00001077 assert(DestA && DestB && isCond &&
1078 "CFG in a bad state. Cannot correct CFG edges");
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001079 }
Bill Wendlinge543d162010-04-01 00:00:43 +00001080
1081 // Remove superfluous edges. I.e., those which aren't destinations of this
1082 // basic block, duplicate edges, or landing pads.
1083 SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs;
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001084 MachineBasicBlock::succ_iterator SI = succ_begin();
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001085 while (SI != succ_end()) {
Bill Wendlingc70d33112009-12-16 00:08:36 +00001086 const MachineBasicBlock *MBB = *SI;
Bill Wendlinge543d162010-04-01 00:00:43 +00001087 if (!SeenMBBs.insert(MBB) ||
1088 (MBB != DestA && MBB != DestB && !MBB->isLandingPad())) {
1089 // This is a superfluous edge, remove it.
Bill Wendling9e9cca42010-03-31 23:26:26 +00001090 SI = removeSuccessor(SI);
Bill Wendlinge543d162010-04-01 00:00:43 +00001091 Changed = true;
1092 } else {
1093 ++SI;
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001094 }
1095 }
Bill Wendlingc70d33112009-12-16 00:08:36 +00001096
Bill Wendlinge543d162010-04-01 00:00:43 +00001097 return Changed;
Evan Cheng2bdb7d02007-06-18 22:43:58 +00001098}
Evan Cheng2a085c32009-11-17 19:19:59 +00001099
Dale Johannesen73e884b2010-01-20 21:36:02 +00001100/// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
Dale Johannesen10fedd22010-02-10 00:11:11 +00001101/// any DBG_VALUE instructions. Return UnknownLoc if there is none.
Dale Johannesen73e884b2010-01-20 21:36:02 +00001102DebugLoc
Evan Chengddfd1372011-12-14 02:11:42 +00001103MachineBasicBlock::findDebugLoc(instr_iterator MBBI) {
Dale Johannesen73e884b2010-01-20 21:36:02 +00001104 DebugLoc DL;
Evan Chengddfd1372011-12-14 02:11:42 +00001105 instr_iterator E = instr_end();
Evan Cheng7c2a4a32011-12-06 22:12:01 +00001106 if (MBBI == E)
1107 return DL;
1108
1109 // Skip debug declarations, we don't want a DebugLoc from them.
1110 while (MBBI != E && MBBI->isDebugValue())
1111 MBBI++;
1112 if (MBBI != E)
1113 DL = MBBI->getDebugLoc();
Dale Johannesen73e884b2010-01-20 21:36:02 +00001114 return DL;
1115}
1116
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001117/// getSuccWeight - Return weight of the edge from this block to MBB.
1118///
Jakob Stoklund Olesen990ca552012-08-20 22:01:38 +00001119uint32_t MachineBasicBlock::getSuccWeight(const_succ_iterator Succ) const {
Jakub Staszak981d8262011-06-17 18:00:21 +00001120 if (Weights.empty())
1121 return 0;
1122
Jakob Stoklund Olesen990ca552012-08-20 22:01:38 +00001123 return *getWeightIterator(Succ);
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001124}
1125
Stephen Hines36b56882014-04-23 16:57:46 -07001126/// Set successor weight of a given iterator.
1127void MachineBasicBlock::setSuccWeight(succ_iterator I, uint32_t weight) {
1128 if (Weights.empty())
1129 return;
1130 *getWeightIterator(I) = weight;
1131}
1132
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001133/// getWeightIterator - Return wight iterator corresonding to the I successor
1134/// iterator
1135MachineBasicBlock::weight_iterator MachineBasicBlock::
1136getWeightIterator(MachineBasicBlock::succ_iterator I) {
Jakub Staszak981d8262011-06-17 18:00:21 +00001137 assert(Weights.size() == Successors.size() && "Async weight list!");
Jakub Staszak7cc2b072011-06-16 20:22:37 +00001138 size_t index = std::distance(Successors.begin(), I);
1139 assert(index < Weights.size() && "Not a current successor!");
1140 return Weights.begin() + index;
1141}
1142
Jakub Staszak25101bb2011-12-20 20:03:10 +00001143/// getWeightIterator - Return wight iterator corresonding to the I successor
1144/// iterator
1145MachineBasicBlock::const_weight_iterator MachineBasicBlock::
1146getWeightIterator(MachineBasicBlock::const_succ_iterator I) const {
1147 assert(Weights.size() == Successors.size() && "Async weight list!");
1148 const size_t index = std::distance(Successors.begin(), I);
1149 assert(index < Weights.size() && "Not a current successor!");
1150 return Weights.begin() + index;
1151}
1152
James Molloyc4f70d42012-09-12 10:18:23 +00001153/// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed
1154/// as of just before "MI".
1155///
1156/// Search is localised to a neighborhood of
1157/// Neighborhood instructions before (searching for defs or kills) and N
1158/// instructions after (searching just for defs) MI.
1159MachineBasicBlock::LivenessQueryResult
1160MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI,
1161 unsigned Reg, MachineInstr *MI,
1162 unsigned Neighborhood) {
James Molloyc4f70d42012-09-12 10:18:23 +00001163 unsigned N = Neighborhood;
1164 MachineBasicBlock *MBB = MI->getParent();
1165
1166 // Start by searching backwards from MI, looking for kills, reads or defs.
1167
1168 MachineBasicBlock::iterator I(MI);
1169 // If this is the first insn in the block, don't search backwards.
1170 if (I != MBB->begin()) {
1171 do {
1172 --I;
1173
1174 MachineOperandIteratorBase::PhysRegInfo Analysis =
1175 MIOperands(I).analyzePhysReg(Reg, TRI);
1176
Tim Northover310f2482012-11-20 09:56:11 +00001177 if (Analysis.Defines)
1178 // Outputs happen after inputs so they take precedence if both are
1179 // present.
1180 return Analysis.DefinesDead ? LQR_Dead : LQR_Live;
1181
1182 if (Analysis.Kills || Analysis.Clobbers)
James Molloyc4f70d42012-09-12 10:18:23 +00001183 // Register killed, so isn't live.
1184 return LQR_Dead;
1185
Tim Northover310f2482012-11-20 09:56:11 +00001186 else if (Analysis.ReadsOverlap)
James Molloyc4f70d42012-09-12 10:18:23 +00001187 // Defined or read without a previous kill - live.
Tim Northover310f2482012-11-20 09:56:11 +00001188 return Analysis.Reads ? LQR_Live : LQR_OverlappingLive;
James Molloyc4f70d42012-09-12 10:18:23 +00001189
1190 } while (I != MBB->begin() && --N > 0);
1191 }
1192
1193 // Did we get to the start of the block?
1194 if (I == MBB->begin()) {
1195 // If so, the register's state is definitely defined by the live-in state.
1196 for (MCRegAliasIterator RAI(Reg, TRI, /*IncludeSelf=*/true);
1197 RAI.isValid(); ++RAI) {
1198 if (MBB->isLiveIn(*RAI))
1199 return (*RAI == Reg) ? LQR_Live : LQR_OverlappingLive;
1200 }
1201
1202 return LQR_Dead;
1203 }
1204
1205 N = Neighborhood;
1206
1207 // Try searching forwards from MI, looking for reads or defs.
1208 I = MachineBasicBlock::iterator(MI);
1209 // If this is the last insn in the block, don't search forwards.
1210 if (I != MBB->end()) {
1211 for (++I; I != MBB->end() && N > 0; ++I, --N) {
1212 MachineOperandIteratorBase::PhysRegInfo Analysis =
1213 MIOperands(I).analyzePhysReg(Reg, TRI);
1214
1215 if (Analysis.ReadsOverlap)
1216 // Used, therefore must have been live.
1217 return (Analysis.Reads) ?
1218 LQR_Live : LQR_OverlappingLive;
1219
Tim Northover310f2482012-11-20 09:56:11 +00001220 else if (Analysis.Clobbers || Analysis.Defines)
James Molloyc4f70d42012-09-12 10:18:23 +00001221 // Defined (but not read) therefore cannot have been live.
1222 return LQR_Dead;
1223 }
1224 }
1225
1226 // At this point we have no idea of the liveness of the register.
1227 return LQR_Unknown;
1228}