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