Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Collect the sequence of machine instructions for a basic block. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallPtrSet.h" |
| 16 | #include "llvm/ADT/SmallString.h" |
| 17 | #include "llvm/Assembly/Writer.h" |
Cameron Zwarich | 8597c14 | 2013-02-11 09:24:47 +0000 | [diff] [blame^] | 18 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/LiveVariables.h" |
| 20 | #include "llvm/CodeGen/MachineDominators.h" |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFunction.h" |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Cameron Zwarich | 8597c14 | 2013-02-11 09:24:47 +0000 | [diff] [blame^] | 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/SlotIndexes.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/BasicBlock.h" |
| 26 | #include "llvm/IR/DataLayout.h" |
Chris Lattner | f71cb01 | 2010-01-26 04:55:51 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCAsmInfo.h" |
| 28 | #include "llvm/MC/MCContext.h" |
David Greene | dbdbbd9 | 2010-01-04 23:22:07 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 30 | #include "llvm/Support/LeakDetector.h" |
Daniel Dunbar | 1cd1d98 | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetInstrInfo.h" |
| 33 | #include "llvm/Target/TargetMachine.h" |
| 34 | #include "llvm/Target/TargetRegisterInfo.h" |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 38 | MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb) |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 39 | : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false), |
| 40 | AddressTaken(false) { |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 41 | Insts.Parent = this; |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 42 | } |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 43 | |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 44 | MachineBasicBlock::~MachineBasicBlock() { |
| 45 | LeakDetector::removeGarbageObject(this); |
| 46 | } |
| 47 | |
Chris Lattner | f71cb01 | 2010-01-26 04:55:51 +0000 | [diff] [blame] | 48 | /// getSymbol - Return the MCSymbol for this basic block. |
| 49 | /// |
Chris Lattner | 1b2eb0e | 2010-03-13 21:04:28 +0000 | [diff] [blame] | 50 | MCSymbol *MachineBasicBlock::getSymbol() const { |
Chris Lattner | f71cb01 | 2010-01-26 04:55:51 +0000 | [diff] [blame] | 51 | const MachineFunction *MF = getParent(); |
Chris Lattner | 1b2eb0e | 2010-03-13 21:04:28 +0000 | [diff] [blame] | 52 | MCContext &Ctx = MF->getContext(); |
| 53 | const char *Prefix = Ctx.getAsmInfo().getPrivateGlobalPrefix(); |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 54 | return Ctx.GetOrCreateSymbol(Twine(Prefix) + "BB" + |
| 55 | Twine(MF->getFunctionNumber()) + "_" + |
| 56 | Twine(getNumber())); |
Chris Lattner | f71cb01 | 2010-01-26 04:55:51 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 60 | raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) { |
Daniel Dunbar | 1cd1d98 | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 61 | MBB.print(OS); |
| 62 | return OS; |
| 63 | } |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 64 | |
Jakub Staszak | 12af5ff | 2011-06-16 18:01:17 +0000 | [diff] [blame] | 65 | /// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 66 | /// 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 Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 72 | void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 73 | MachineFunction &MF = *N->getParent(); |
| 74 | N->Number = MF.addToMBBNumbering(N); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 75 | |
| 76 | // Make sure the instructions have their operands in the reginfo lists. |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 77 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 78 | for (MachineBasicBlock::instr_iterator |
| 79 | I = N->instr_begin(), E = N->instr_end(); I != E; ++I) |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 80 | I->AddRegOperandsToUseLists(RegInfo); |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 81 | |
| 82 | LeakDetector::removeGarbageObject(N); |
Brian Gaeke | 0bcb1ad | 2004-05-12 21:35:22 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 85 | void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) { |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 86 | N->getParent()->removeFromMBBNumbering(N->Number); |
Brian Gaeke | 0bcb1ad | 2004-05-12 21:35:22 +0000 | [diff] [blame] | 87 | N->Number = -1; |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 88 | LeakDetector::addGarbageObject(N); |
Brian Gaeke | 0bcb1ad | 2004-05-12 21:35:22 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Chris Lattner | 5e61fa9 | 2004-02-19 16:13:54 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 92 | /// 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 Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 95 | void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) { |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 96 | assert(N->getParent() == 0 && "machine instruction already in a basic block"); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 97 | N->setParent(Parent); |
Jakub Staszak | 12af5ff | 2011-06-16 18:01:17 +0000 | [diff] [blame] | 98 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 99 | // Add the instruction's register operands to their corresponding |
| 100 | // use/def lists. |
| 101 | MachineFunction *MF = Parent->getParent(); |
| 102 | N->AddRegOperandsToUseLists(MF->getRegInfo()); |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 103 | |
| 104 | LeakDetector::removeGarbageObject(N); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 107 | /// 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 Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 110 | void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) { |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 111 | assert(N->getParent() != 0 && "machine instruction not in a basic block"); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 112 | |
| 113 | // Remove from the use/def lists. |
Jakob Stoklund Olesen | ff2b99a | 2012-08-09 22:49:37 +0000 | [diff] [blame] | 114 | if (MachineFunction *MF = N->getParent()->getParent()) |
| 115 | N->RemoveRegOperandsFromUseLists(MF->getRegInfo()); |
Jakub Staszak | 12af5ff | 2011-06-16 18:01:17 +0000 | [diff] [blame] | 116 | |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 117 | N->setParent(0); |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 118 | |
| 119 | LeakDetector::addGarbageObject(N); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 122 | /// 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 Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 125 | void ilist_traits<MachineInstr>:: |
| 126 | transferNodesFromList(ilist_traits<MachineInstr> &fromList, |
Evan Cheng | 7c2a4a3 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 127 | ilist_iterator<MachineInstr> first, |
| 128 | ilist_iterator<MachineInstr> last) { |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 129 | assert(Parent->getParent() == fromList.Parent->getParent() && |
| 130 | "MachineInstr parent mismatch!"); |
| 131 | |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 132 | // Splice within the same MBB -> no change. |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 133 | if (Parent == fromList.Parent) return; |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 134 | |
| 135 | // If splicing between two blocks within the same function, just update the |
| 136 | // parent pointers. |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 137 | for (; first != last; ++first) |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 138 | first->setParent(Parent); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 141 | void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 142 | assert(!MI->getParent() && "MI is still in a block!"); |
| 143 | Parent->getParent()->DeleteMachineInstr(MI); |
| 144 | } |
| 145 | |
Dan Gohman | d463a74 | 2010-07-07 14:33:51 +0000 | [diff] [blame] | 146 | MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() { |
Benjamin Kramer | f378f5f | 2012-02-10 00:28:31 +0000 | [diff] [blame] | 147 | instr_iterator I = instr_begin(), E = instr_end(); |
| 148 | while (I != E && I->isPHI()) |
Dan Gohman | d463a74 | 2010-07-07 14:33:51 +0000 | [diff] [blame] | 149 | ++I; |
Akira Hatanaka | af80822 | 2012-10-26 17:11:42 +0000 | [diff] [blame] | 150 | assert((I == E || !I->isInsideBundle()) && |
| 151 | "First non-phi MI cannot be inside a bundle!"); |
Dan Gohman | d463a74 | 2010-07-07 14:33:51 +0000 | [diff] [blame] | 152 | return I; |
| 153 | } |
| 154 | |
Jakob Stoklund Olesen | 92095e7 | 2010-10-30 01:26:14 +0000 | [diff] [blame] | 155 | MachineBasicBlock::iterator |
| 156 | MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) { |
Benjamin Kramer | f378f5f | 2012-02-10 00:28:31 +0000 | [diff] [blame] | 157 | iterator E = end(); |
| 158 | while (I != E && (I->isPHI() || I->isLabel() || I->isDebugValue())) |
Jakob Stoklund Olesen | 92095e7 | 2010-10-30 01:26:14 +0000 | [diff] [blame] | 159 | ++I; |
Evan Cheng | 7c2a4a3 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 160 | // FIXME: This needs to change if we wish to bundle labels / dbg_values |
| 161 | // inside the bundle. |
Akira Hatanaka | af80822 | 2012-10-26 17:11:42 +0000 | [diff] [blame] | 162 | assert((I == E || !I->isInsideBundle()) && |
Evan Cheng | 7c2a4a3 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 163 | "First non-phi / non-label instruction is inside a bundle!"); |
Jakob Stoklund Olesen | 92095e7 | 2010-10-30 01:26:14 +0000 | [diff] [blame] | 164 | return I; |
| 165 | } |
| 166 | |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 167 | MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { |
Benjamin Kramer | f378f5f | 2012-02-10 00:28:31 +0000 | [diff] [blame] | 168 | iterator B = begin(), E = end(), I = E; |
| 169 | while (I != B && ((--I)->isTerminator() || I->isDebugValue())) |
Jakob Stoklund Olesen | b6436e5 | 2011-01-14 02:12:54 +0000 | [diff] [blame] | 170 | ; /*noop */ |
Benjamin Kramer | f378f5f | 2012-02-10 00:28:31 +0000 | [diff] [blame] | 171 | while (I != E && !I->isTerminator()) |
Evan Cheng | 7c2a4a3 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 172 | ++I; |
| 173 | return I; |
| 174 | } |
| 175 | |
| 176 | MachineBasicBlock::const_iterator |
| 177 | MachineBasicBlock::getFirstTerminator() const { |
Benjamin Kramer | f378f5f | 2012-02-10 00:28:31 +0000 | [diff] [blame] | 178 | const_iterator B = begin(), E = end(), I = E; |
| 179 | while (I != B && ((--I)->isTerminator() || I->isDebugValue())) |
Evan Cheng | 7c2a4a3 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 180 | ; /*noop */ |
Benjamin Kramer | f378f5f | 2012-02-10 00:28:31 +0000 | [diff] [blame] | 181 | while (I != E && !I->isTerminator()) |
Evan Cheng | 7c2a4a3 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 182 | ++I; |
| 183 | return I; |
| 184 | } |
| 185 | |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 186 | MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() { |
Benjamin Kramer | f378f5f | 2012-02-10 00:28:31 +0000 | [diff] [blame] | 187 | instr_iterator B = instr_begin(), E = instr_end(), I = E; |
| 188 | while (I != B && ((--I)->isTerminator() || I->isDebugValue())) |
Evan Cheng | 7c2a4a3 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 189 | ; /*noop */ |
Benjamin Kramer | f378f5f | 2012-02-10 00:28:31 +0000 | [diff] [blame] | 190 | while (I != E && !I->isTerminator()) |
Jakob Stoklund Olesen | 0422390 | 2011-01-14 06:33:45 +0000 | [diff] [blame] | 191 | ++I; |
Jakob Stoklund Olesen | b6436e5 | 2011-01-14 02:12:54 +0000 | [diff] [blame] | 192 | return I; |
Alkis Evlogimenos | 743d0a1 | 2004-02-23 18:14:48 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Jakob Stoklund Olesen | 4f28c1c | 2011-01-13 21:28:52 +0000 | [diff] [blame] | 195 | MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() { |
Evan Cheng | 7c2a4a3 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 196 | // Skip over end-of-block dbg_value instructions. |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 197 | instr_iterator B = instr_begin(), I = instr_end(); |
Jakob Stoklund Olesen | 4f28c1c | 2011-01-13 21:28:52 +0000 | [diff] [blame] | 198 | while (I != B) { |
| 199 | --I; |
Evan Cheng | 7c2a4a3 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 200 | // 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 | |
| 209 | MachineBasicBlock::const_iterator |
| 210 | MachineBasicBlock::getLastNonDebugInstr() const { |
| 211 | // Skip over end-of-block dbg_value instructions. |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 212 | const_instr_iterator B = instr_begin(), I = instr_end(); |
Evan Cheng | 7c2a4a3 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 213 | while (I != B) { |
| 214 | --I; |
| 215 | // Return instruction that starts a bundle. |
| 216 | if (I->isDebugValue() || I->isInsideBundle()) |
Jakob Stoklund Olesen | 4f28c1c | 2011-01-13 21:28:52 +0000 | [diff] [blame] | 217 | continue; |
| 218 | return I; |
| 219 | } |
| 220 | // The block is all debug values. |
| 221 | return end(); |
| 222 | } |
| 223 | |
Jakob Stoklund Olesen | cb64047 | 2011-02-04 19:33:11 +0000 | [diff] [blame] | 224 | const 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 Ren | b720be6 | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 234 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 235 | void MachineBasicBlock::dump() const { |
David Greene | dbdbbd9 | 2010-01-04 23:22:07 +0000 | [diff] [blame] | 236 | print(dbgs()); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 237 | } |
Manman Ren | 77e300e | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 238 | #endif |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 239 | |
Jakob Stoklund Olesen | 324da76 | 2009-11-20 01:17:03 +0000 | [diff] [blame] | 240 | StringRef MachineBasicBlock::getName() const { |
| 241 | if (const BasicBlock *LBB = getBasicBlock()) |
| 242 | return LBB->getName(); |
| 243 | else |
| 244 | return "(null)"; |
| 245 | } |
| 246 | |
Andrew Trick | 8ceaa66 | 2012-03-07 00:18:18 +0000 | [diff] [blame] | 247 | /// Return a hopefully unique identifier for this block. |
| 248 | std::string MachineBasicBlock::getFullName() const { |
| 249 | std::string Name; |
| 250 | if (getParent()) |
Craig Topper | 96601ca | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 251 | Name = (getParent()->getName() + ":").str(); |
Andrew Trick | 8ceaa66 | 2012-03-07 00:18:18 +0000 | [diff] [blame] | 252 | if (getBasicBlock()) |
| 253 | Name += getBasicBlock()->getName(); |
| 254 | else |
| 255 | Name += (Twine("BB") + Twine(getNumber())).str(); |
| 256 | return Name; |
| 257 | } |
| 258 | |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 259 | void MachineBasicBlock::print(raw_ostream &OS, SlotIndexes *Indexes) const { |
Evan Cheng | 13d8285 | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 260 | const MachineFunction *MF = getParent(); |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 261 | if (!MF) { |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 262 | OS << "Can't print out MachineBasicBlock because parent MachineFunction" |
| 263 | << " is null\n"; |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 264 | return; |
| 265 | } |
Alkis Evlogimenos | a638286 | 2004-09-05 18:39:20 +0000 | [diff] [blame] | 266 | |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 267 | if (Indexes) |
| 268 | OS << Indexes->getMBBStartIdx(this) << '\t'; |
| 269 | |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 270 | 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 Wendling | 4b8e1fd | 2012-06-15 19:30:42 +0000 | [diff] [blame] | 280 | if (Alignment) |
Jakob Stoklund Olesen | f2e9445 | 2011-12-06 21:08:39 +0000 | [diff] [blame] | 281 | OS << Comma << "Align " << Alignment << " (" << (1u << Alignment) |
| 282 | << " bytes)"; |
Jakob Stoklund Olesen | f2e9445 | 2011-12-06 21:08:39 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 284 | OS << '\n'; |
Evan Cheng | 13d8285 | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 285 | |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 286 | const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); |
Dan Gohman | cb406c2 | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 287 | if (!livein_empty()) { |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 288 | if (Indexes) OS << '\t'; |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 289 | OS << " Live Ins:"; |
Dan Gohman | 81bf03e | 2010-04-13 16:57:55 +0000 | [diff] [blame] | 290 | for (livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I) |
Jakob Stoklund Olesen | 668c9e3 | 2011-01-13 00:57:35 +0000 | [diff] [blame] | 291 | OS << ' ' << PrintReg(*I, TRI); |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 292 | OS << '\n'; |
Evan Cheng | 13d8285 | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 293 | } |
Chris Lattner | 681764b | 2006-09-26 03:41:59 +0000 | [diff] [blame] | 294 | // Print the preds of this block according to the CFG. |
| 295 | if (!pred_empty()) { |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 296 | if (Indexes) OS << '\t'; |
Chris Lattner | 681764b | 2006-09-26 03:41:59 +0000 | [diff] [blame] | 297 | OS << " Predecessors according to CFG:"; |
| 298 | for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 299 | OS << " BB#" << (*PI)->getNumber(); |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 300 | OS << '\n'; |
Chris Lattner | 681764b | 2006-09-26 03:41:59 +0000 | [diff] [blame] | 301 | } |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 302 | |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 303 | for (const_instr_iterator I = instr_begin(); I != instr_end(); ++I) { |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 304 | if (Indexes) { |
| 305 | if (Indexes->hasIndex(I)) |
| 306 | OS << Indexes->getInstructionIndex(I); |
| 307 | OS << '\t'; |
| 308 | } |
Chris Lattner | 2d8e3d2 | 2009-08-23 00:47:04 +0000 | [diff] [blame] | 309 | OS << '\t'; |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 310 | if (I->isInsideBundle()) |
| 311 | OS << " * "; |
Alkis Evlogimenos | a638286 | 2004-09-05 18:39:20 +0000 | [diff] [blame] | 312 | I->print(OS, &getParent()->getTarget()); |
| 313 | } |
Chris Lattner | 380ae49 | 2005-04-01 06:48:38 +0000 | [diff] [blame] | 314 | |
| 315 | // Print the successors of this block according to the CFG. |
| 316 | if (!succ_empty()) { |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 317 | if (Indexes) OS << '\t'; |
Chris Lattner | 380ae49 | 2005-04-01 06:48:38 +0000 | [diff] [blame] | 318 | OS << " Successors according to CFG:"; |
Jakob Stoklund Olesen | cb6889b | 2012-08-13 23:13:23 +0000 | [diff] [blame] | 319 | for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) { |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 320 | OS << " BB#" << (*SI)->getNumber(); |
Jakob Stoklund Olesen | cb6889b | 2012-08-13 23:13:23 +0000 | [diff] [blame] | 321 | if (!Weights.empty()) |
| 322 | OS << '(' << *getWeightIterator(SI) << ')'; |
| 323 | } |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 324 | OS << '\n'; |
Chris Lattner | 380ae49 | 2005-04-01 06:48:38 +0000 | [diff] [blame] | 325 | } |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 326 | } |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 327 | |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 328 | void MachineBasicBlock::removeLiveIn(unsigned Reg) { |
Dan Gohman | 81bf03e | 2010-04-13 16:57:55 +0000 | [diff] [blame] | 329 | std::vector<unsigned>::iterator I = |
| 330 | std::find(LiveIns.begin(), LiveIns.end(), Reg); |
Jakob Stoklund Olesen | 78836f0 | 2012-03-28 20:11:42 +0000 | [diff] [blame] | 331 | if (I != LiveIns.end()) |
| 332 | LiveIns.erase(I); |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Evan Cheng | a971dbd | 2008-04-24 09:06:33 +0000 | [diff] [blame] | 335 | bool MachineBasicBlock::isLiveIn(unsigned Reg) const { |
Dan Gohman | 81bf03e | 2010-04-13 16:57:55 +0000 | [diff] [blame] | 336 | livein_iterator I = std::find(livein_begin(), livein_end(), Reg); |
Evan Cheng | a971dbd | 2008-04-24 09:06:33 +0000 | [diff] [blame] | 337 | return I != livein_end(); |
| 338 | } |
| 339 | |
Chris Lattner | c585a3f | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 340 | void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 341 | getParent()->splice(NewAfter, this); |
Chris Lattner | c585a3f | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { |
Chris Lattner | c585a3f | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 345 | MachineFunction::iterator BBI = NewBefore; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 346 | getParent()->splice(++BBI, this); |
Chris Lattner | c585a3f | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 349 | void 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 Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 356 | DebugLoc dl; // FIXME: this is nowhere |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 357 | 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 Carruth | 3b7b209 | 2011-11-22 13:13:16 +0000 | [diff] [blame] | 368 | // 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 Carruth | 521fc5b | 2011-11-23 08:23:54 +0000 | [diff] [blame] | 376 | |
| 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 Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 384 | if (!isLayoutSuccessor(TBB)) |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 385 | TII->InsertBranch(*this, TBB, 0, Cond, dl); |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 386 | } |
| 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 Olesen | e023993 | 2009-11-22 18:28:04 +0000 | [diff] [blame] | 393 | if (TII->ReverseBranchCondition(Cond)) |
| 394 | return; |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 395 | TII->RemoveBranch(*this); |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 396 | TII->InsertBranch(*this, FBB, 0, Cond, dl); |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 397 | } else if (isLayoutSuccessor(FBB)) { |
| 398 | TII->RemoveBranch(*this); |
Stuart Hastings | 3bf9125 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 399 | TII->InsertBranch(*this, TBB, 0, Cond, dl); |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 400 | } |
| 401 | } else { |
Chandler Carruth | f1a60c7 | 2012-04-16 22:03:00 +0000 | [diff] [blame] | 402 | // 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 Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 427 | // The block has a fallthrough conditional branch. |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 428 | if (isLayoutSuccessor(TBB)) { |
Jakob Stoklund Olesen | e023993 | 2009-11-22 18:28:04 +0000 | [diff] [blame] | 429 | if (TII->ReverseBranchCondition(Cond)) { |
| 430 | // We can't reverse the condition, add an unconditional branch. |
| 431 | Cond.clear(); |
Chandler Carruth | f1a60c7 | 2012-04-16 22:03:00 +0000 | [diff] [blame] | 432 | TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl); |
Jakob Stoklund Olesen | e023993 | 2009-11-22 18:28:04 +0000 | [diff] [blame] | 433 | return; |
| 434 | } |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 435 | TII->RemoveBranch(*this); |
Chandler Carruth | f1a60c7 | 2012-04-16 22:03:00 +0000 | [diff] [blame] | 436 | TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl); |
| 437 | } else if (!isLayoutSuccessor(FallthroughBB)) { |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 438 | TII->RemoveBranch(*this); |
Chandler Carruth | f1a60c7 | 2012-04-16 22:03:00 +0000 | [diff] [blame] | 439 | TII->InsertBranch(*this, TBB, FallthroughBB, Cond, dl); |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | } |
| 443 | } |
Chris Lattner | c585a3f | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 444 | |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 445 | void 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 Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 458 | |
| 459 | void 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 Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 463 | |
| 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 Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 470 | Successors.erase(I); |
| 471 | } |
| 472 | |
Jakub Staszak | 12af5ff | 2011-06-16 18:01:17 +0000 | [diff] [blame] | 473 | MachineBasicBlock::succ_iterator |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 474 | MachineBasicBlock::removeSuccessor(succ_iterator I) { |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 475 | assert(I != Successors.end() && "Not a current successor!"); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 476 | |
| 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 Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 483 | (*I)->removePredecessor(this); |
Dan Gohman | 5d5ee80 | 2009-01-08 22:19:34 +0000 | [diff] [blame] | 484 | return Successors.erase(I); |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 487 | void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old, |
| 488 | MachineBasicBlock *New) { |
Jakob Stoklund Olesen | 15121ca | 2012-08-10 03:23:27 +0000 | [diff] [blame] | 489 | if (Old == New) |
| 490 | return; |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 491 | |
Jakob Stoklund Olesen | 15121ca | 2012-08-10 03:23:27 +0000 | [diff] [blame] | 492 | 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 Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Jakob Stoklund Olesen | 15121ca | 2012-08-10 03:23:27 +0000 | [diff] [blame] | 517 | // 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 Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 527 | void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { |
| 528 | Predecessors.push_back(pred); |
| 529 | } |
| 530 | |
| 531 | void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { |
Eli Friedman | 6dda916 | 2011-04-18 21:21:37 +0000 | [diff] [blame] | 532 | pred_iterator I = std::find(Predecessors.begin(), Predecessors.end(), pred); |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 533 | assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); |
| 534 | Predecessors.erase(I); |
| 535 | } |
Evan Cheng | 4f09878 | 2007-05-17 23:58:53 +0000 | [diff] [blame] | 536 | |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 537 | void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) { |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 538 | if (this == fromMBB) |
| 539 | return; |
Jakub Staszak | 12af5ff | 2011-06-16 18:01:17 +0000 | [diff] [blame] | 540 | |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 541 | while (!fromMBB->succ_empty()) { |
| 542 | MachineBasicBlock *Succ = *fromMBB->succ_begin(); |
Jakob Stoklund Olesen | 03e593e | 2012-08-13 23:13:25 +0000 | [diff] [blame] | 543 | uint32_t Weight = 0; |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 544 | |
| 545 | // If Weight list is empty it means we don't use it (disabled optimization). |
| 546 | if (!fromMBB->Weights.empty()) |
Jakob Stoklund Olesen | 03e593e | 2012-08-13 23:13:25 +0000 | [diff] [blame] | 547 | Weight = *fromMBB->Weights.begin(); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 548 | |
Jakob Stoklund Olesen | 03e593e | 2012-08-13 23:13:25 +0000 | [diff] [blame] | 549 | addSuccessor(Succ, Weight); |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 550 | fromMBB->removeSuccessor(Succ); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | void |
| 555 | MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB) { |
| 556 | if (this == fromMBB) |
| 557 | return; |
Jakub Staszak | 12af5ff | 2011-06-16 18:01:17 +0000 | [diff] [blame] | 558 | |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 559 | while (!fromMBB->succ_empty()) { |
| 560 | MachineBasicBlock *Succ = *fromMBB->succ_begin(); |
Jakob Stoklund Olesen | 03e593e | 2012-08-13 23:13:25 +0000 | [diff] [blame] | 561 | uint32_t Weight = 0; |
| 562 | if (!fromMBB->Weights.empty()) |
| 563 | Weight = *fromMBB->Weights.begin(); |
| 564 | addSuccessor(Succ, Weight); |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 565 | fromMBB->removeSuccessor(Succ); |
| 566 | |
| 567 | // Fix up any PHI nodes in the successor. |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 568 | for (MachineBasicBlock::instr_iterator MI = Succ->instr_begin(), |
| 569 | ME = Succ->instr_end(); MI != ME && MI->isPHI(); ++MI) |
Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 570 | 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 Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Jakob Stoklund Olesen | f192b50 | 2012-07-30 17:36:47 +0000 | [diff] [blame] | 578 | bool MachineBasicBlock::isPredecessor(const MachineBasicBlock *MBB) const { |
| 579 | return std::find(pred_begin(), pred_end(), MBB) != pred_end(); |
| 580 | } |
| 581 | |
Dan Gohman | 6d1b89e | 2009-03-30 20:06:29 +0000 | [diff] [blame] | 582 | bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const { |
Jakob Stoklund Olesen | f192b50 | 2012-07-30 17:36:47 +0000 | [diff] [blame] | 583 | return std::find(succ_begin(), succ_end(), MBB) != succ_end(); |
Evan Cheng | 4f09878 | 2007-05-17 23:58:53 +0000 | [diff] [blame] | 584 | } |
Evan Cheng | 0370fad | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 585 | |
Dan Gohman | 6d1b89e | 2009-03-30 20:06:29 +0000 | [diff] [blame] | 586 | bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const { |
Dan Gohman | 6ade6f5 | 2008-10-02 22:09:09 +0000 | [diff] [blame] | 587 | MachineFunction::const_iterator I(this); |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 588 | return llvm::next(I) == MachineFunction::const_iterator(MBB); |
Dan Gohman | 6ade6f5 | 2008-10-02 22:09:09 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 591 | bool MachineBasicBlock::canFallThrough() { |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 592 | 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 Gohman | 735985f | 2009-12-05 00:32:59 +0000 | [diff] [blame] | 602 | // 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 Olesen | 33cc8d6 | 2010-01-15 20:00:12 +0000 | [diff] [blame] | 606 | if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) { |
Dan Gohman | 735985f | 2009-12-05 00:32:59 +0000 | [diff] [blame] | 607 | // 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 Rosier | 0162ff4 | 2012-01-26 18:24:25 +0000 | [diff] [blame] | 609 | // is possible. The isPredicated check is needed because this code can be |
Dan Gohman | 735985f | 2009-12-05 00:32:59 +0000 | [diff] [blame] | 610 | // called during IfConversion, where an instruction which is normally a |
Chad Rosier | 6a5d0e2 | 2012-01-26 20:19:05 +0000 | [diff] [blame] | 611 | // Barrier is predicated and thus no longer an actual control barrier. |
Chad Rosier | 0162ff4 | 2012-01-26 18:24:25 +0000 | [diff] [blame] | 612 | return empty() || !back().isBarrier() || TII->isPredicated(&back()); |
Dan Gohman | 735985f | 2009-12-05 00:32:59 +0000 | [diff] [blame] | 613 | } |
Bob Wilson | 15acadd | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 614 | |
| 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 Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 633 | MachineBasicBlock * |
| 634 | MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P) { |
Evan Cheng | ddb1420 | 2012-04-24 19:06:55 +0000 | [diff] [blame] | 635 | // 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 Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 640 | MachineFunction *MF = getParent(); |
| 641 | DebugLoc dl; // FIXME: this is nowhere |
| 642 | |
Jakob Stoklund Olesen | 371e82b | 2010-11-02 00:58:37 +0000 | [diff] [blame] | 643 | // 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 Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 645 | 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 Olesen | 371e82b | 2010-11-02 00:58:37 +0000 | [diff] [blame] | 651 | // 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 Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 661 | MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(); |
| 662 | MF->insert(llvm::next(MachineFunction::iterator(this)), NMBB); |
Evan Cheng | 087fbeb | 2010-08-17 17:15:14 +0000 | [diff] [blame] | 663 | DEBUG(dbgs() << "Splitting critical edge:" |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 664 | " BB#" << getNumber() |
| 665 | << " -- BB#" << NMBB->getNumber() |
| 666 | << " -- BB#" << Succ->getNumber() << '\n'); |
Cameron Zwarich | f5844a7 | 2013-02-10 23:29:54 +0000 | [diff] [blame] | 667 | SlotIndexes *Indexes = P->getAnalysisIfAvailable<SlotIndexes>(); |
| 668 | if (Indexes) |
| 669 | Indexes->insertMBBInMaps(NMBB); |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 670 | |
Jakob Stoklund Olesen | 5790335 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 671 | // On some targets like Mips, branches may kill virtual registers. Make sure |
| 672 | // that LiveVariables is properly updated after updateTerminator replaces the |
| 673 | // terminators. |
| 674 | LiveVariables *LV = P->getAnalysisIfAvailable<LiveVariables>(); |
| 675 | |
| 676 | // Collect a list of virtual registers killed by the terminators. |
| 677 | SmallVector<unsigned, 4> KilledRegs; |
| 678 | if (LV) |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 679 | for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); |
Evan Cheng | 7c2a4a3 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 680 | I != E; ++I) { |
Jakob Stoklund Olesen | 5790335 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 681 | MachineInstr *MI = I; |
| 682 | for (MachineInstr::mop_iterator OI = MI->operands_begin(), |
| 683 | OE = MI->operands_end(); OI != OE; ++OI) { |
Lang Hames | 72a043f | 2012-02-09 05:59:36 +0000 | [diff] [blame] | 684 | if (!OI->isReg() || OI->getReg() == 0 || |
| 685 | !OI->isUse() || !OI->isKill() || OI->isUndef()) |
Jakob Stoklund Olesen | 5790335 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 686 | continue; |
| 687 | unsigned Reg = OI->getReg(); |
Lang Hames | 72a043f | 2012-02-09 05:59:36 +0000 | [diff] [blame] | 688 | if (TargetRegisterInfo::isPhysicalRegister(Reg) || |
Jakob Stoklund Olesen | 5790335 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 689 | LV->getVarInfo(Reg).removeKill(MI)) { |
| 690 | KilledRegs.push_back(Reg); |
| 691 | DEBUG(dbgs() << "Removing terminator kill: " << *MI); |
| 692 | OI->setIsKill(false); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 697 | ReplaceUsesOfBlockWith(Succ, NMBB); |
Cameron Zwarich | cbe3f5e | 2013-02-11 09:24:45 +0000 | [diff] [blame] | 698 | |
| 699 | // If updateTerminator() removes instructions, we need to remove them from |
| 700 | // SlotIndexes. |
| 701 | SmallVector<MachineInstr*, 4> Terminators; |
| 702 | if (Indexes) { |
| 703 | for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); |
| 704 | I != E; ++I) |
| 705 | Terminators.push_back(I); |
| 706 | } |
| 707 | |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 708 | updateTerminator(); |
| 709 | |
Cameron Zwarich | cbe3f5e | 2013-02-11 09:24:45 +0000 | [diff] [blame] | 710 | if (Indexes) { |
| 711 | SmallVector<MachineInstr*, 4> NewTerminators; |
| 712 | for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); |
| 713 | I != E; ++I) |
| 714 | NewTerminators.push_back(I); |
| 715 | |
| 716 | for (SmallVectorImpl<MachineInstr*>::iterator I = Terminators.begin(), |
| 717 | E = Terminators.end(); I != E; ++I) { |
| 718 | if (std::find(NewTerminators.begin(), NewTerminators.end(), *I) == |
| 719 | NewTerminators.end()) |
| 720 | Indexes->removeMachineInstrFromMaps(*I); |
| 721 | } |
| 722 | } |
| 723 | |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 724 | // Insert unconditional "jump Succ" instruction in NMBB if necessary. |
| 725 | NMBB->addSuccessor(Succ); |
| 726 | if (!NMBB->isLayoutSuccessor(Succ)) { |
| 727 | Cond.clear(); |
| 728 | MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, Succ, NULL, Cond, dl); |
Cameron Zwarich | f5844a7 | 2013-02-10 23:29:54 +0000 | [diff] [blame] | 729 | |
| 730 | if (Indexes) { |
| 731 | for (instr_iterator I = NMBB->instr_begin(), E = NMBB->instr_end(); |
| 732 | I != E; ++I) { |
| 733 | // Some instructions may have been moved to NMBB by updateTerminator(), |
| 734 | // so we first remove any instruction that already has an index. |
| 735 | if (Indexes->hasIndex(I)) |
| 736 | Indexes->removeMachineInstrFromMaps(I); |
| 737 | Indexes->insertMachineInstrInMaps(I); |
| 738 | } |
| 739 | } |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | // Fix PHI nodes in Succ so they refer to NMBB instead of this |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 743 | for (MachineBasicBlock::instr_iterator |
| 744 | i = Succ->instr_begin(),e = Succ->instr_end(); |
| 745 | i != e && i->isPHI(); ++i) |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 746 | for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2) |
| 747 | if (i->getOperand(ni+1).getMBB() == this) |
| 748 | i->getOperand(ni+1).setMBB(NMBB); |
| 749 | |
Jakob Stoklund Olesen | ac7caa0 | 2011-10-14 17:25:46 +0000 | [diff] [blame] | 750 | // Inherit live-ins from the successor |
| 751 | for (MachineBasicBlock::livein_iterator I = Succ->livein_begin(), |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 752 | E = Succ->livein_end(); I != E; ++I) |
Jakob Stoklund Olesen | ac7caa0 | 2011-10-14 17:25:46 +0000 | [diff] [blame] | 753 | NMBB->addLiveIn(*I); |
| 754 | |
Jakob Stoklund Olesen | 5790335 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 755 | // Update LiveVariables. |
Lang Hames | 72a043f | 2012-02-09 05:59:36 +0000 | [diff] [blame] | 756 | const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); |
Jakob Stoklund Olesen | 5790335 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 757 | if (LV) { |
| 758 | // Restore kills of virtual registers that were killed by the terminators. |
| 759 | while (!KilledRegs.empty()) { |
| 760 | unsigned Reg = KilledRegs.pop_back_val(); |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 761 | for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) { |
Lang Hames | 72a043f | 2012-02-09 05:59:36 +0000 | [diff] [blame] | 762 | if (!(--I)->addRegisterKilled(Reg, TRI, /* addIfNotFound= */ false)) |
Jakob Stoklund Olesen | 5790335 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 763 | continue; |
Lang Hames | 72a043f | 2012-02-09 05:59:36 +0000 | [diff] [blame] | 764 | if (TargetRegisterInfo::isVirtualRegister(Reg)) |
| 765 | LV->getVarInfo(Reg).Kills.push_back(I); |
Jakob Stoklund Olesen | 5790335 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 766 | DEBUG(dbgs() << "Restored terminator kill: " << *I); |
| 767 | break; |
| 768 | } |
| 769 | } |
| 770 | // Update relevant live-through information. |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 771 | LV->addNewBlock(NMBB, this, Succ); |
Jakob Stoklund Olesen | 5790335 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 772 | } |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 773 | |
Cameron Zwarich | 8597c14 | 2013-02-11 09:24:47 +0000 | [diff] [blame^] | 774 | if (LiveIntervals *LIS = P->getAnalysisIfAvailable<LiveIntervals>()) { |
| 775 | // After splitting the edge and updating SlotIndexes, live intervals may be |
| 776 | // in one of two situations, depending on whether this block was the last in |
| 777 | // the function. If the original block was the last in the function, all live |
| 778 | // intervals will end prior to the beginning of the new split block. If the |
| 779 | // original block was not at the end of the function, all live intervals will |
| 780 | // extend to the end of the new split block. |
| 781 | |
| 782 | bool isLastMBB = |
| 783 | llvm::next(MachineFunction::iterator(NMBB)) == getParent()->end(); |
| 784 | |
| 785 | SlotIndex StartIndex = Indexes->getMBBEndIdx(this); |
| 786 | SlotIndex PrevIndex = StartIndex.getPrevSlot(); |
| 787 | SlotIndex EndIndex = Indexes->getMBBEndIdx(NMBB); |
| 788 | |
| 789 | // Find the registers used from NMBB in PHIs in Succ. |
| 790 | SmallSet<unsigned, 8> PHISrcRegs; |
| 791 | for (MachineBasicBlock::instr_iterator |
| 792 | I = Succ->instr_begin(), E = Succ->instr_end(); |
| 793 | I != E && I->isPHI(); ++I) { |
| 794 | for (unsigned ni = 1, ne = I->getNumOperands(); ni != ne; ni += 2) { |
| 795 | if (I->getOperand(ni+1).getMBB() == NMBB) { |
| 796 | MachineOperand &MO = I->getOperand(ni); |
| 797 | unsigned Reg = MO.getReg(); |
| 798 | PHISrcRegs.insert(Reg); |
| 799 | if (MO.isUndef() || !isLastMBB) |
| 800 | break; |
| 801 | |
| 802 | LiveInterval &LI = LIS->getInterval(Reg); |
| 803 | VNInfo *VNI = LI.getVNInfoAt(PrevIndex); |
| 804 | assert(VNI && "PHI sources should be live out of their predecessors."); |
| 805 | LI.addRange(LiveRange(StartIndex, EndIndex, VNI)); |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | MachineRegisterInfo *MRI = &getParent()->getRegInfo(); |
| 811 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 812 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
| 813 | if (PHISrcRegs.count(Reg) || !LIS->hasInterval(Reg)) |
| 814 | continue; |
| 815 | |
| 816 | LiveInterval &LI = LIS->getInterval(Reg); |
| 817 | if (!LI.liveAt(PrevIndex)) |
| 818 | continue; |
| 819 | |
| 820 | bool isLiveOut = false; |
| 821 | for (MachineBasicBlock::succ_iterator SI = succ_begin(), |
| 822 | SE = succ_end(); SI != SE; ++SI) { |
| 823 | MachineBasicBlock *SuccMBB = *SI == NMBB ? Succ : *SI; |
| 824 | if (LI.liveAt(LIS->getMBBStartIdx(SuccMBB))) { |
| 825 | isLiveOut = true; |
| 826 | break; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | if (isLiveOut && isLastMBB) { |
| 831 | VNInfo *VNI = LI.getVNInfoAt(PrevIndex); |
| 832 | assert(VNI && "LiveInterval should have VNInfo where it is live."); |
| 833 | LI.addRange(LiveRange(StartIndex, EndIndex, VNI)); |
| 834 | } else if (!isLiveOut && !isLastMBB) { |
| 835 | LI.removeRange(StartIndex, EndIndex); |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 840 | if (MachineDominatorTree *MDT = |
Evan Cheng | 1970892 | 2010-08-19 23:32:47 +0000 | [diff] [blame] | 841 | P->getAnalysisIfAvailable<MachineDominatorTree>()) { |
| 842 | // Update dominator information. |
| 843 | MachineDomTreeNode *SucccDTNode = MDT->getNode(Succ); |
| 844 | |
| 845 | bool IsNewIDom = true; |
| 846 | for (const_pred_iterator PI = Succ->pred_begin(), E = Succ->pred_end(); |
| 847 | PI != E; ++PI) { |
| 848 | MachineBasicBlock *PredBB = *PI; |
| 849 | if (PredBB == NMBB) |
| 850 | continue; |
| 851 | if (!MDT->dominates(SucccDTNode, MDT->getNode(PredBB))) { |
| 852 | IsNewIDom = false; |
| 853 | break; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | // We know "this" dominates the newly created basic block. |
| 858 | MachineDomTreeNode *NewDTNode = MDT->addNewBlock(NMBB, this); |
| 859 | |
| 860 | // If all the other predecessors of "Succ" are dominated by "Succ" itself |
| 861 | // then the new block is the new immediate dominator of "Succ". Otherwise, |
| 862 | // the new block doesn't dominate anything. |
| 863 | if (IsNewIDom) |
| 864 | MDT->changeImmediateDominator(SucccDTNode, NewDTNode); |
| 865 | } |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 866 | |
Evan Cheng | e008384 | 2010-08-17 17:43:50 +0000 | [diff] [blame] | 867 | if (MachineLoopInfo *MLI = P->getAnalysisIfAvailable<MachineLoopInfo>()) |
Dan Gohman | 853d3fb | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 868 | if (MachineLoop *TIL = MLI->getLoopFor(this)) { |
| 869 | // If one or the other blocks were not in a loop, the new block is not |
| 870 | // either, and thus LI doesn't need to be updated. |
| 871 | if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) { |
| 872 | if (TIL == DestLoop) { |
| 873 | // Both in the same loop, the NMBB joins loop. |
| 874 | DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); |
| 875 | } else if (TIL->contains(DestLoop)) { |
| 876 | // Edge from an outer loop to an inner loop. Add to the outer loop. |
| 877 | TIL->addBasicBlockToLoop(NMBB, MLI->getBase()); |
| 878 | } else if (DestLoop->contains(TIL)) { |
| 879 | // Edge from an inner loop to an outer loop. Add to the outer loop. |
| 880 | DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); |
| 881 | } else { |
| 882 | // Edge from two loops with no containment relation. Because these |
| 883 | // are natural loops, we know that the destination block must be the |
| 884 | // header of its loop (adding a branch into a loop elsewhere would |
| 885 | // create an irreducible loop). |
| 886 | assert(DestLoop->getHeader() == Succ && |
| 887 | "Should not create irreducible loops!"); |
| 888 | if (MachineLoop *P = DestLoop->getParentLoop()) |
| 889 | P->addBasicBlockToLoop(NMBB, MLI->getBase()); |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | return NMBB; |
| 895 | } |
| 896 | |
Jakob Stoklund Olesen | 9f4692d | 2012-12-17 23:55:38 +0000 | [diff] [blame] | 897 | /// Prepare MI to be removed from its bundle. This fixes bundle flags on MI's |
| 898 | /// neighboring instructions so the bundle won't be broken by removing MI. |
| 899 | static void unbundleSingleMI(MachineInstr *MI) { |
| 900 | // Removing the first instruction in a bundle. |
| 901 | if (MI->isBundledWithSucc() && !MI->isBundledWithPred()) |
| 902 | MI->unbundleFromSucc(); |
| 903 | // Removing the last instruction in a bundle. |
| 904 | if (MI->isBundledWithPred() && !MI->isBundledWithSucc()) |
| 905 | MI->unbundleFromPred(); |
| 906 | // If MI is not bundled, or if it is internal to a bundle, the neighbor flags |
| 907 | // are already fine. |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Jakob Stoklund Olesen | 9f4692d | 2012-12-17 23:55:38 +0000 | [diff] [blame] | 910 | MachineBasicBlock::instr_iterator |
| 911 | MachineBasicBlock::erase(MachineBasicBlock::instr_iterator I) { |
| 912 | unbundleSingleMI(I); |
| 913 | return Insts.erase(I); |
| 914 | } |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 915 | |
Jakob Stoklund Olesen | 9f4692d | 2012-12-17 23:55:38 +0000 | [diff] [blame] | 916 | MachineInstr *MachineBasicBlock::remove_instr(MachineInstr *MI) { |
| 917 | unbundleSingleMI(MI); |
| 918 | MI->clearFlag(MachineInstr::BundledPred); |
| 919 | MI->clearFlag(MachineInstr::BundledSucc); |
| 920 | return Insts.remove(MI); |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Jakob Stoklund Olesen | edc3503 | 2012-12-18 17:54:53 +0000 | [diff] [blame] | 923 | MachineBasicBlock::instr_iterator |
| 924 | MachineBasicBlock::insert(instr_iterator I, MachineInstr *MI) { |
| 925 | assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() && |
| 926 | "Cannot insert instruction with bundle flags"); |
| 927 | // Set the bundle flags when inserting inside a bundle. |
| 928 | if (I != instr_end() && I->isBundledWithPred()) { |
| 929 | MI->setFlag(MachineInstr::BundledPred); |
| 930 | MI->setFlag(MachineInstr::BundledSucc); |
| 931 | } |
| 932 | return Insts.insert(I, MI); |
| 933 | } |
| 934 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 935 | /// removeFromParent - This method unlinks 'this' from the containing function, |
| 936 | /// and returns it, but does not delete it. |
| 937 | MachineBasicBlock *MachineBasicBlock::removeFromParent() { |
| 938 | assert(getParent() && "Not embedded in a function!"); |
| 939 | getParent()->remove(this); |
| 940 | return this; |
| 941 | } |
| 942 | |
| 943 | |
| 944 | /// eraseFromParent - This method unlinks 'this' from the containing function, |
| 945 | /// and deletes it. |
| 946 | void MachineBasicBlock::eraseFromParent() { |
| 947 | assert(getParent() && "Not embedded in a function!"); |
| 948 | getParent()->erase(this); |
| 949 | } |
| 950 | |
| 951 | |
Evan Cheng | 0370fad | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 952 | /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to |
| 953 | /// 'Old', change the code and CFG so that it branches to 'New' instead. |
| 954 | void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, |
| 955 | MachineBasicBlock *New) { |
| 956 | assert(Old != New && "Cannot replace self with self!"); |
| 957 | |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 958 | MachineBasicBlock::instr_iterator I = instr_end(); |
| 959 | while (I != instr_begin()) { |
Evan Cheng | 0370fad | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 960 | --I; |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 961 | if (!I->isTerminator()) break; |
Evan Cheng | 0370fad | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 962 | |
| 963 | // Scan the operands of this machine instruction, replacing any uses of Old |
| 964 | // with New. |
| 965 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 966 | if (I->getOperand(i).isMBB() && |
Dan Gohman | 014278e | 2008-09-13 17:58:21 +0000 | [diff] [blame] | 967 | I->getOperand(i).getMBB() == Old) |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 968 | I->getOperand(i).setMBB(New); |
Evan Cheng | 0370fad | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Dan Gohman | 5412d06 | 2009-05-05 21:10:19 +0000 | [diff] [blame] | 971 | // Update the successor information. |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 972 | replaceSuccessor(Old, New); |
Evan Cheng | 0370fad | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 975 | /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the |
| 976 | /// CFG to be inserted. If we have proven that MBB can only branch to DestA and |
Bill Wendling | c70d3311 | 2009-12-16 00:08:36 +0000 | [diff] [blame] | 977 | /// DestB, remove any other MBB successors from the CFG. DestA and DestB can be |
| 978 | /// null. |
Jakub Staszak | 12af5ff | 2011-06-16 18:01:17 +0000 | [diff] [blame] | 979 | /// |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 980 | /// Besides DestA and DestB, retain other edges leading to LandingPads |
| 981 | /// (currently there can be only one; we don't check or require that here). |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 982 | /// Note it is possible that DestA and/or DestB are LandingPads. |
| 983 | bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA, |
| 984 | MachineBasicBlock *DestB, |
| 985 | bool isCond) { |
Bill Wendling | c70d3311 | 2009-12-16 00:08:36 +0000 | [diff] [blame] | 986 | // The values of DestA and DestB frequently come from a call to the |
| 987 | // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial |
| 988 | // values from there. |
| 989 | // |
| 990 | // 1. If both DestA and DestB are null, then the block ends with no branches |
| 991 | // (it falls through to its successor). |
| 992 | // 2. If DestA is set, DestB is null, and isCond is false, then the block ends |
| 993 | // with only an unconditional branch. |
| 994 | // 3. If DestA is set, DestB is null, and isCond is true, then the block ends |
| 995 | // with a conditional branch that falls through to a successor (DestB). |
| 996 | // 4. If DestA and DestB is set and isCond is true, then the block ends with a |
| 997 | // conditional branch followed by an unconditional branch. DestA is the |
| 998 | // 'true' destination and DestB is the 'false' destination. |
| 999 | |
Bill Wendling | e543d16 | 2010-04-01 00:00:43 +0000 | [diff] [blame] | 1000 | bool Changed = false; |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1001 | |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1002 | MachineFunction::iterator FallThru = |
| 1003 | llvm::next(MachineFunction::iterator(this)); |
Bill Wendling | e543d16 | 2010-04-01 00:00:43 +0000 | [diff] [blame] | 1004 | |
| 1005 | if (DestA == 0 && DestB == 0) { |
| 1006 | // Block falls through to successor. |
| 1007 | DestA = FallThru; |
| 1008 | DestB = FallThru; |
| 1009 | } else if (DestA != 0 && DestB == 0) { |
| 1010 | if (isCond) |
| 1011 | // Block ends in conditional jump that falls through to successor. |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1012 | DestB = FallThru; |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1013 | } else { |
Bill Wendling | e543d16 | 2010-04-01 00:00:43 +0000 | [diff] [blame] | 1014 | assert(DestA && DestB && isCond && |
| 1015 | "CFG in a bad state. Cannot correct CFG edges"); |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1016 | } |
Bill Wendling | e543d16 | 2010-04-01 00:00:43 +0000 | [diff] [blame] | 1017 | |
| 1018 | // Remove superfluous edges. I.e., those which aren't destinations of this |
| 1019 | // basic block, duplicate edges, or landing pads. |
| 1020 | SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs; |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1021 | MachineBasicBlock::succ_iterator SI = succ_begin(); |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1022 | while (SI != succ_end()) { |
Bill Wendling | c70d3311 | 2009-12-16 00:08:36 +0000 | [diff] [blame] | 1023 | const MachineBasicBlock *MBB = *SI; |
Bill Wendling | e543d16 | 2010-04-01 00:00:43 +0000 | [diff] [blame] | 1024 | if (!SeenMBBs.insert(MBB) || |
| 1025 | (MBB != DestA && MBB != DestB && !MBB->isLandingPad())) { |
| 1026 | // This is a superfluous edge, remove it. |
Bill Wendling | 9e9cca4 | 2010-03-31 23:26:26 +0000 | [diff] [blame] | 1027 | SI = removeSuccessor(SI); |
Bill Wendling | e543d16 | 2010-04-01 00:00:43 +0000 | [diff] [blame] | 1028 | Changed = true; |
| 1029 | } else { |
| 1030 | ++SI; |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1031 | } |
| 1032 | } |
Bill Wendling | c70d3311 | 2009-12-16 00:08:36 +0000 | [diff] [blame] | 1033 | |
Bill Wendling | e543d16 | 2010-04-01 00:00:43 +0000 | [diff] [blame] | 1034 | return Changed; |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1035 | } |
Evan Cheng | 2a085c3 | 2009-11-17 19:19:59 +0000 | [diff] [blame] | 1036 | |
Dale Johannesen | 73e884b | 2010-01-20 21:36:02 +0000 | [diff] [blame] | 1037 | /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping |
Dale Johannesen | 10fedd2 | 2010-02-10 00:11:11 +0000 | [diff] [blame] | 1038 | /// any DBG_VALUE instructions. Return UnknownLoc if there is none. |
Dale Johannesen | 73e884b | 2010-01-20 21:36:02 +0000 | [diff] [blame] | 1039 | DebugLoc |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 1040 | MachineBasicBlock::findDebugLoc(instr_iterator MBBI) { |
Dale Johannesen | 73e884b | 2010-01-20 21:36:02 +0000 | [diff] [blame] | 1041 | DebugLoc DL; |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 1042 | instr_iterator E = instr_end(); |
Evan Cheng | 7c2a4a3 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 1043 | if (MBBI == E) |
| 1044 | return DL; |
| 1045 | |
| 1046 | // Skip debug declarations, we don't want a DebugLoc from them. |
| 1047 | while (MBBI != E && MBBI->isDebugValue()) |
| 1048 | MBBI++; |
| 1049 | if (MBBI != E) |
| 1050 | DL = MBBI->getDebugLoc(); |
Dale Johannesen | 73e884b | 2010-01-20 21:36:02 +0000 | [diff] [blame] | 1051 | return DL; |
| 1052 | } |
| 1053 | |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1054 | /// getSuccWeight - Return weight of the edge from this block to MBB. |
| 1055 | /// |
Jakob Stoklund Olesen | 990ca55 | 2012-08-20 22:01:38 +0000 | [diff] [blame] | 1056 | uint32_t MachineBasicBlock::getSuccWeight(const_succ_iterator Succ) const { |
Jakub Staszak | 981d826 | 2011-06-17 18:00:21 +0000 | [diff] [blame] | 1057 | if (Weights.empty()) |
| 1058 | return 0; |
| 1059 | |
Jakob Stoklund Olesen | 990ca55 | 2012-08-20 22:01:38 +0000 | [diff] [blame] | 1060 | return *getWeightIterator(Succ); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | /// getWeightIterator - Return wight iterator corresonding to the I successor |
| 1064 | /// iterator |
| 1065 | MachineBasicBlock::weight_iterator MachineBasicBlock:: |
| 1066 | getWeightIterator(MachineBasicBlock::succ_iterator I) { |
Jakub Staszak | 981d826 | 2011-06-17 18:00:21 +0000 | [diff] [blame] | 1067 | assert(Weights.size() == Successors.size() && "Async weight list!"); |
Jakub Staszak | 7cc2b07 | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1068 | size_t index = std::distance(Successors.begin(), I); |
| 1069 | assert(index < Weights.size() && "Not a current successor!"); |
| 1070 | return Weights.begin() + index; |
| 1071 | } |
| 1072 | |
Jakub Staszak | 25101bb | 2011-12-20 20:03:10 +0000 | [diff] [blame] | 1073 | /// getWeightIterator - Return wight iterator corresonding to the I successor |
| 1074 | /// iterator |
| 1075 | MachineBasicBlock::const_weight_iterator MachineBasicBlock:: |
| 1076 | getWeightIterator(MachineBasicBlock::const_succ_iterator I) const { |
| 1077 | assert(Weights.size() == Successors.size() && "Async weight list!"); |
| 1078 | const size_t index = std::distance(Successors.begin(), I); |
| 1079 | assert(index < Weights.size() && "Not a current successor!"); |
| 1080 | return Weights.begin() + index; |
| 1081 | } |
| 1082 | |
James Molloy | c4f70d4 | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1083 | /// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed |
| 1084 | /// as of just before "MI". |
| 1085 | /// |
| 1086 | /// Search is localised to a neighborhood of |
| 1087 | /// Neighborhood instructions before (searching for defs or kills) and N |
| 1088 | /// instructions after (searching just for defs) MI. |
| 1089 | MachineBasicBlock::LivenessQueryResult |
| 1090 | MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI, |
| 1091 | unsigned Reg, MachineInstr *MI, |
| 1092 | unsigned Neighborhood) { |
James Molloy | c4f70d4 | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1093 | unsigned N = Neighborhood; |
| 1094 | MachineBasicBlock *MBB = MI->getParent(); |
| 1095 | |
| 1096 | // Start by searching backwards from MI, looking for kills, reads or defs. |
| 1097 | |
| 1098 | MachineBasicBlock::iterator I(MI); |
| 1099 | // If this is the first insn in the block, don't search backwards. |
| 1100 | if (I != MBB->begin()) { |
| 1101 | do { |
| 1102 | --I; |
| 1103 | |
| 1104 | MachineOperandIteratorBase::PhysRegInfo Analysis = |
| 1105 | MIOperands(I).analyzePhysReg(Reg, TRI); |
| 1106 | |
Tim Northover | 310f248 | 2012-11-20 09:56:11 +0000 | [diff] [blame] | 1107 | if (Analysis.Defines) |
| 1108 | // Outputs happen after inputs so they take precedence if both are |
| 1109 | // present. |
| 1110 | return Analysis.DefinesDead ? LQR_Dead : LQR_Live; |
| 1111 | |
| 1112 | if (Analysis.Kills || Analysis.Clobbers) |
James Molloy | c4f70d4 | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1113 | // Register killed, so isn't live. |
| 1114 | return LQR_Dead; |
| 1115 | |
Tim Northover | 310f248 | 2012-11-20 09:56:11 +0000 | [diff] [blame] | 1116 | else if (Analysis.ReadsOverlap) |
James Molloy | c4f70d4 | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1117 | // Defined or read without a previous kill - live. |
Tim Northover | 310f248 | 2012-11-20 09:56:11 +0000 | [diff] [blame] | 1118 | return Analysis.Reads ? LQR_Live : LQR_OverlappingLive; |
James Molloy | c4f70d4 | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1119 | |
| 1120 | } while (I != MBB->begin() && --N > 0); |
| 1121 | } |
| 1122 | |
| 1123 | // Did we get to the start of the block? |
| 1124 | if (I == MBB->begin()) { |
| 1125 | // If so, the register's state is definitely defined by the live-in state. |
| 1126 | for (MCRegAliasIterator RAI(Reg, TRI, /*IncludeSelf=*/true); |
| 1127 | RAI.isValid(); ++RAI) { |
| 1128 | if (MBB->isLiveIn(*RAI)) |
| 1129 | return (*RAI == Reg) ? LQR_Live : LQR_OverlappingLive; |
| 1130 | } |
| 1131 | |
| 1132 | return LQR_Dead; |
| 1133 | } |
| 1134 | |
| 1135 | N = Neighborhood; |
| 1136 | |
| 1137 | // Try searching forwards from MI, looking for reads or defs. |
| 1138 | I = MachineBasicBlock::iterator(MI); |
| 1139 | // If this is the last insn in the block, don't search forwards. |
| 1140 | if (I != MBB->end()) { |
| 1141 | for (++I; I != MBB->end() && N > 0; ++I, --N) { |
| 1142 | MachineOperandIteratorBase::PhysRegInfo Analysis = |
| 1143 | MIOperands(I).analyzePhysReg(Reg, TRI); |
| 1144 | |
| 1145 | if (Analysis.ReadsOverlap) |
| 1146 | // Used, therefore must have been live. |
| 1147 | return (Analysis.Reads) ? |
| 1148 | LQR_Live : LQR_OverlappingLive; |
| 1149 | |
Tim Northover | 310f248 | 2012-11-20 09:56:11 +0000 | [diff] [blame] | 1150 | else if (Analysis.Clobbers || Analysis.Defines) |
James Molloy | c4f70d4 | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1151 | // Defined (but not read) therefore cannot have been live. |
| 1152 | return LQR_Dead; |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | // At this point we have no idea of the liveness of the register. |
| 1157 | return LQR_Unknown; |
| 1158 | } |
| 1159 | |
Evan Cheng | 2a085c3 | 2009-11-17 19:19:59 +0000 | [diff] [blame] | 1160 | void llvm::WriteAsOperand(raw_ostream &OS, const MachineBasicBlock *MBB, |
| 1161 | bool t) { |
| 1162 | OS << "BB#" << MBB->getNumber(); |
| 1163 | } |
Dale Johannesen | 918f0f0 | 2010-01-20 00:19:24 +0000 | [diff] [blame] | 1164 | |