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