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