Alkis Evlogimenos | 14f3fe8 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | 14f3fe8 | 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 | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallPtrSet.h" |
| 16 | #include "llvm/ADT/SmallString.h" |
Cameron Zwarich | b47fb38 | 2013-02-11 09:24:47 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/LiveVariables.h" |
| 19 | #include "llvm/CodeGen/MachineDominators.h" |
Alkis Evlogimenos | 14f3fe8 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunction.h" |
Jakob Stoklund Olesen | 533c3bf | 2013-07-03 23:56:20 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Cameron Zwarich | b47fb38 | 2013-02-11 09:24:47 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Jakob Stoklund Olesen | b705023 | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/SlotIndexes.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/BasicBlock.h" |
| 26 | #include "llvm/IR/DataLayout.h" |
Duncan P. N. Exon Smith | 32692154 | 2015-06-26 22:04:20 +0000 | [diff] [blame] | 27 | #include "llvm/IR/ModuleSlotTracker.h" |
Chris Lattner | d051af7 | 2010-01-26 04:55:51 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCAsmInfo.h" |
| 29 | #include "llvm/MC/MCContext.h" |
David Greene | 6c56cef | 2010-01-04 23:22:07 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 796e43e | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 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" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetSubtargetInfo.h" |
Chris Lattner | 4336b87 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 36 | #include <algorithm> |
Alkis Evlogimenos | 14f3fe8 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 37 | using namespace llvm; |
| 38 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 39 | #define DEBUG_TYPE "codegen" |
| 40 | |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 41 | MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb) |
Cong Hou | ec10587 | 2015-08-06 18:17:29 +0000 | [diff] [blame] | 42 | : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false), |
| 43 | AddressTaken(false), CachedMCSymbol(nullptr) { |
Dan Gohman | 804c95d | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 44 | Insts.Parent = this; |
Tanya Lattner | 91fa3a9 | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 45 | } |
Tanya Lattner | 91fa3a9 | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 46 | |
Dan Gohman | 0ece943 | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 47 | MachineBasicBlock::~MachineBasicBlock() { |
Dan Gohman | 0ece943 | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Chris Lattner | d051af7 | 2010-01-26 04:55:51 +0000 | [diff] [blame] | 50 | /// getSymbol - Return the MCSymbol for this basic block. |
| 51 | /// |
Chris Lattner | 29bdac4 | 2010-03-13 21:04:28 +0000 | [diff] [blame] | 52 | MCSymbol *MachineBasicBlock::getSymbol() const { |
Eli Bendersky | 58b04b7 | 2013-04-22 21:21:08 +0000 | [diff] [blame] | 53 | if (!CachedMCSymbol) { |
| 54 | const MachineFunction *MF = getParent(); |
| 55 | MCContext &Ctx = MF->getContext(); |
Matt Arsenault | 4e27343 | 2014-12-04 00:06:57 +0000 | [diff] [blame] | 56 | const char *Prefix = Ctx.getAsmInfo()->getPrivateLabelPrefix(); |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 57 | CachedMCSymbol = Ctx.getOrCreateSymbol(Twine(Prefix) + "BB" + |
Eli Bendersky | 58b04b7 | 2013-04-22 21:21:08 +0000 | [diff] [blame] | 58 | Twine(MF->getFunctionNumber()) + |
| 59 | "_" + Twine(getNumber())); |
| 60 | } |
| 61 | |
| 62 | return CachedMCSymbol; |
Chris Lattner | d051af7 | 2010-01-26 04:55:51 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | |
Chris Lattner | af119ca | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 66 | raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) { |
Daniel Dunbar | 796e43e | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 67 | MBB.print(OS); |
| 68 | return OS; |
| 69 | } |
Tanya Lattner | 91fa3a9 | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 70 | |
Jakub Staszak | feadd43 | 2011-06-16 18:01:17 +0000 | [diff] [blame] | 71 | /// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 72 | /// parent pointer of the MBB, the MBB numbering, and any instructions in the |
| 73 | /// MBB to be on the right operand list for registers. |
| 74 | /// |
| 75 | /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it |
| 76 | /// gets the next available unique MBB number. If it is removed from a |
| 77 | /// MachineFunction, it goes back to being #-1. |
Chris Lattner | af119ca | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 78 | void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 79 | MachineFunction &MF = *N->getParent(); |
| 80 | N->Number = MF.addToMBBNumbering(N); |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 81 | |
| 82 | // Make sure the instructions have their operands in the reginfo lists. |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 83 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 84 | for (MachineBasicBlock::instr_iterator |
| 85 | I = N->instr_begin(), E = N->instr_end(); I != E; ++I) |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 86 | I->AddRegOperandsToUseLists(RegInfo); |
Brian Gaeke | cb5d22a | 2004-05-12 21:35:22 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Chris Lattner | af119ca | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 89 | void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) { |
Chris Lattner | 574e716 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 90 | N->getParent()->removeFromMBBNumbering(N->Number); |
Brian Gaeke | cb5d22a | 2004-05-12 21:35:22 +0000 | [diff] [blame] | 91 | N->Number = -1; |
| 92 | } |
| 93 | |
Chris Lattner | d23a882 | 2004-02-19 16:13:54 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 95 | /// addNodeToList (MI) - When we add an instruction to a basic block |
| 96 | /// list, we update its parent pointer and add its operands from reg use/def |
| 97 | /// lists if appropriate. |
Chris Lattner | af119ca | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 98 | void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 99 | assert(!N->getParent() && "machine instruction already in a basic block"); |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 100 | N->setParent(Parent); |
Jakub Staszak | feadd43 | 2011-06-16 18:01:17 +0000 | [diff] [blame] | 101 | |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 102 | // Add the instruction's register operands to their corresponding |
| 103 | // use/def lists. |
| 104 | MachineFunction *MF = Parent->getParent(); |
| 105 | N->AddRegOperandsToUseLists(MF->getRegInfo()); |
Alkis Evlogimenos | 14f3fe8 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 108 | /// removeNodeFromList (MI) - When we remove an instruction from a basic block |
| 109 | /// list, we update its parent pointer and remove its operands from reg use/def |
| 110 | /// lists if appropriate. |
Chris Lattner | af119ca | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 111 | void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 112 | assert(N->getParent() && "machine instruction not in a basic block"); |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 113 | |
| 114 | // Remove from the use/def lists. |
Jakob Stoklund Olesen | c4102d4 | 2012-08-09 22:49:37 +0000 | [diff] [blame] | 115 | if (MachineFunction *MF = N->getParent()->getParent()) |
| 116 | N->RemoveRegOperandsFromUseLists(MF->getRegInfo()); |
Jakub Staszak | feadd43 | 2011-06-16 18:01:17 +0000 | [diff] [blame] | 117 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 118 | N->setParent(nullptr); |
Alkis Evlogimenos | 14f3fe8 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 121 | /// transferNodesFromList (MI) - When moving a range of instructions from one |
| 122 | /// MBB list to another, we need to update the parent pointers and the use/def |
| 123 | /// lists. |
Chris Lattner | af119ca | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 124 | void ilist_traits<MachineInstr>:: |
| 125 | transferNodesFromList(ilist_traits<MachineInstr> &fromList, |
Evan Cheng | 2a81dd4 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 126 | ilist_iterator<MachineInstr> first, |
| 127 | ilist_iterator<MachineInstr> last) { |
Dan Gohman | 804c95d | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 128 | assert(Parent->getParent() == fromList.Parent->getParent() && |
| 129 | "MachineInstr parent mismatch!"); |
| 130 | |
Chris Lattner | 574e716 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 131 | // Splice within the same MBB -> no change. |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 132 | if (Parent == fromList.Parent) return; |
Chris Lattner | 961e742 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 133 | |
| 134 | // If splicing between two blocks within the same function, just update the |
| 135 | // parent pointers. |
Dan Gohman | 804c95d | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 136 | for (; first != last; ++first) |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 137 | first->setParent(Parent); |
Alkis Evlogimenos | 14f3fe8 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Dan Gohman | 804c95d | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 140 | void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 141 | assert(!MI->getParent() && "MI is still in a block!"); |
| 142 | Parent->getParent()->DeleteMachineInstr(MI); |
| 143 | } |
| 144 | |
Dan Gohman | 88c547e | 2010-07-07 14:33:51 +0000 | [diff] [blame] | 145 | MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() { |
Benjamin Kramer | baa41d4 | 2012-02-10 00:28:31 +0000 | [diff] [blame] | 146 | instr_iterator I = instr_begin(), E = instr_end(); |
| 147 | while (I != E && I->isPHI()) |
Dan Gohman | 88c547e | 2010-07-07 14:33:51 +0000 | [diff] [blame] | 148 | ++I; |
Akira Hatanaka | 6fe7aca | 2012-10-26 17:11:42 +0000 | [diff] [blame] | 149 | assert((I == E || !I->isInsideBundle()) && |
| 150 | "First non-phi MI cannot be inside a bundle!"); |
Dan Gohman | 88c547e | 2010-07-07 14:33:51 +0000 | [diff] [blame] | 151 | return I; |
| 152 | } |
| 153 | |
Jakob Stoklund Olesen | ef54185 | 2010-10-30 01:26:14 +0000 | [diff] [blame] | 154 | MachineBasicBlock::iterator |
| 155 | MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) { |
Benjamin Kramer | baa41d4 | 2012-02-10 00:28:31 +0000 | [diff] [blame] | 156 | iterator E = end(); |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 157 | while (I != E && (I->isPHI() || I->isPosition() || I->isDebugValue())) |
Jakob Stoklund Olesen | ef54185 | 2010-10-30 01:26:14 +0000 | [diff] [blame] | 158 | ++I; |
Evan Cheng | 2a81dd4 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 159 | // FIXME: This needs to change if we wish to bundle labels / dbg_values |
| 160 | // inside the bundle. |
Akira Hatanaka | 6fe7aca | 2012-10-26 17:11:42 +0000 | [diff] [blame] | 161 | assert((I == E || !I->isInsideBundle()) && |
Evan Cheng | 2a81dd4 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 162 | "First non-phi / non-label instruction is inside a bundle!"); |
Jakob Stoklund Olesen | ef54185 | 2010-10-30 01:26:14 +0000 | [diff] [blame] | 163 | return I; |
| 164 | } |
| 165 | |
Chris Lattner | 4336b87 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 166 | MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { |
Benjamin Kramer | baa41d4 | 2012-02-10 00:28:31 +0000 | [diff] [blame] | 167 | iterator B = begin(), E = end(), I = E; |
| 168 | while (I != B && ((--I)->isTerminator() || I->isDebugValue())) |
Jakob Stoklund Olesen | c381028 | 2011-01-14 02:12:54 +0000 | [diff] [blame] | 169 | ; /*noop */ |
Benjamin Kramer | baa41d4 | 2012-02-10 00:28:31 +0000 | [diff] [blame] | 170 | while (I != E && !I->isTerminator()) |
Evan Cheng | 2a81dd4 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 171 | ++I; |
| 172 | return I; |
| 173 | } |
| 174 | |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 175 | MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() { |
Benjamin Kramer | baa41d4 | 2012-02-10 00:28:31 +0000 | [diff] [blame] | 176 | instr_iterator B = instr_begin(), E = instr_end(), I = E; |
| 177 | while (I != B && ((--I)->isTerminator() || I->isDebugValue())) |
Evan Cheng | 2a81dd4 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 178 | ; /*noop */ |
Benjamin Kramer | baa41d4 | 2012-02-10 00:28:31 +0000 | [diff] [blame] | 179 | while (I != E && !I->isTerminator()) |
Jakob Stoklund Olesen | ab3d6ec | 2011-01-14 06:33:45 +0000 | [diff] [blame] | 180 | ++I; |
Jakob Stoklund Olesen | c381028 | 2011-01-14 02:12:54 +0000 | [diff] [blame] | 181 | return I; |
Alkis Evlogimenos | af2de48 | 2004-02-23 18:14:48 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Benjamin Kramer | 6b56896 | 2015-06-23 14:47:29 +0000 | [diff] [blame] | 184 | MachineBasicBlock::iterator MachineBasicBlock::getFirstNonDebugInstr() { |
| 185 | // Skip over begin-of-block dbg_value instructions. |
| 186 | iterator I = begin(), E = end(); |
| 187 | while (I != E && I->isDebugValue()) |
| 188 | ++I; |
| 189 | return I; |
| 190 | } |
| 191 | |
Jakob Stoklund Olesen | 4bc5e38 | 2011-01-13 21:28:52 +0000 | [diff] [blame] | 192 | MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() { |
Evan Cheng | 2a81dd4 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 193 | // Skip over end-of-block dbg_value instructions. |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 194 | instr_iterator B = instr_begin(), I = instr_end(); |
Jakob Stoklund Olesen | 4bc5e38 | 2011-01-13 21:28:52 +0000 | [diff] [blame] | 195 | while (I != B) { |
| 196 | --I; |
Evan Cheng | 2a81dd4 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 197 | // Return instruction that starts a bundle. |
| 198 | if (I->isDebugValue() || I->isInsideBundle()) |
| 199 | continue; |
| 200 | return I; |
| 201 | } |
| 202 | // The block is all debug values. |
| 203 | return end(); |
| 204 | } |
| 205 | |
Jakob Stoklund Olesen | 096bd88 | 2011-02-04 19:33:11 +0000 | [diff] [blame] | 206 | const MachineBasicBlock *MachineBasicBlock::getLandingPadSuccessor() const { |
| 207 | // A block with a landing pad successor only has one other successor. |
| 208 | if (succ_size() > 2) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 209 | return nullptr; |
Jakob Stoklund Olesen | 096bd88 | 2011-02-04 19:33:11 +0000 | [diff] [blame] | 210 | for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I) |
| 211 | if ((*I)->isLandingPad()) |
| 212 | return *I; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 213 | return nullptr; |
Jakob Stoklund Olesen | 096bd88 | 2011-02-04 19:33:11 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 216 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Chris Lattner | 4336b87 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 217 | void MachineBasicBlock::dump() const { |
David Greene | 6c56cef | 2010-01-04 23:22:07 +0000 | [diff] [blame] | 218 | print(dbgs()); |
Alkis Evlogimenos | 14f3fe8 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 219 | } |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 220 | #endif |
Alkis Evlogimenos | 14f3fe8 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 221 | |
Jakob Stoklund Olesen | 2bbeaa8 | 2009-11-20 01:17:03 +0000 | [diff] [blame] | 222 | StringRef MachineBasicBlock::getName() const { |
| 223 | if (const BasicBlock *LBB = getBasicBlock()) |
| 224 | return LBB->getName(); |
| 225 | else |
| 226 | return "(null)"; |
| 227 | } |
| 228 | |
Andrew Trick | 320c703 | 2012-03-07 00:18:18 +0000 | [diff] [blame] | 229 | /// Return a hopefully unique identifier for this block. |
| 230 | std::string MachineBasicBlock::getFullName() const { |
| 231 | std::string Name; |
| 232 | if (getParent()) |
Craig Topper | a538d83 | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 233 | Name = (getParent()->getName() + ":").str(); |
Andrew Trick | 320c703 | 2012-03-07 00:18:18 +0000 | [diff] [blame] | 234 | if (getBasicBlock()) |
| 235 | Name += getBasicBlock()->getName(); |
| 236 | else |
Yaron Keren | 75e0c4b | 2015-03-27 17:51:30 +0000 | [diff] [blame] | 237 | Name += ("BB" + Twine(getNumber())).str(); |
Andrew Trick | 320c703 | 2012-03-07 00:18:18 +0000 | [diff] [blame] | 238 | return Name; |
| 239 | } |
| 240 | |
Jakob Stoklund Olesen | b705023 | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 241 | void MachineBasicBlock::print(raw_ostream &OS, SlotIndexes *Indexes) const { |
Evan Cheng | bcf1d7f | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 242 | const MachineFunction *MF = getParent(); |
Chris Lattner | af119ca | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 243 | if (!MF) { |
Chris Lattner | 4336b87 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 244 | OS << "Can't print out MachineBasicBlock because parent MachineFunction" |
| 245 | << " is null\n"; |
Tanya Lattner | a578cb7 | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 246 | return; |
| 247 | } |
Duncan P. N. Exon Smith | 32692154 | 2015-06-26 22:04:20 +0000 | [diff] [blame] | 248 | const Function *F = MF->getFunction(); |
| 249 | const Module *M = F ? F->getParent() : nullptr; |
| 250 | ModuleSlotTracker MST(M); |
| 251 | print(OS, MST, Indexes); |
| 252 | } |
| 253 | |
| 254 | void MachineBasicBlock::print(raw_ostream &OS, ModuleSlotTracker &MST, |
| 255 | SlotIndexes *Indexes) const { |
| 256 | const MachineFunction *MF = getParent(); |
| 257 | if (!MF) { |
| 258 | OS << "Can't print out MachineBasicBlock because parent MachineFunction" |
| 259 | << " is null\n"; |
| 260 | return; |
| 261 | } |
Alkis Evlogimenos | fcb3f51 | 2004-09-05 18:39:20 +0000 | [diff] [blame] | 262 | |
Jakob Stoklund Olesen | b705023 | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 263 | if (Indexes) |
| 264 | OS << Indexes->getMBBStartIdx(this) << '\t'; |
| 265 | |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 266 | OS << "BB#" << getNumber() << ": "; |
| 267 | |
| 268 | const char *Comma = ""; |
| 269 | if (const BasicBlock *LBB = getBasicBlock()) { |
| 270 | OS << Comma << "derived from LLVM BB "; |
Duncan P. N. Exon Smith | 32692154 | 2015-06-26 22:04:20 +0000 | [diff] [blame] | 271 | LBB->printAsOperand(OS, /*PrintType=*/false, MST); |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 272 | Comma = ", "; |
| 273 | } |
| 274 | if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; } |
| 275 | if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; } |
Bill Wendling | 4fd9663 | 2012-06-15 19:30:42 +0000 | [diff] [blame] | 276 | if (Alignment) |
Jakob Stoklund Olesen | 2a2b37e | 2011-12-06 21:08:39 +0000 | [diff] [blame] | 277 | OS << Comma << "Align " << Alignment << " (" << (1u << Alignment) |
| 278 | << " bytes)"; |
Jakob Stoklund Olesen | 2a2b37e | 2011-12-06 21:08:39 +0000 | [diff] [blame] | 279 | |
Chris Lattner | af119ca | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 280 | OS << '\n'; |
Evan Cheng | bcf1d7f | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 281 | |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 282 | const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); |
Dan Gohman | c731c97 | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 283 | if (!livein_empty()) { |
Jakob Stoklund Olesen | b705023 | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 284 | if (Indexes) OS << '\t'; |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 285 | OS << " Live Ins:"; |
Dan Gohman | 9d2d053 | 2010-04-13 16:57:55 +0000 | [diff] [blame] | 286 | for (livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I) |
Jakob Stoklund Olesen | 9472847 | 2011-01-13 00:57:35 +0000 | [diff] [blame] | 287 | OS << ' ' << PrintReg(*I, TRI); |
Chris Lattner | af119ca | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 288 | OS << '\n'; |
Evan Cheng | bcf1d7f | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 289 | } |
Chris Lattner | 9a1e91b | 2006-09-26 03:41:59 +0000 | [diff] [blame] | 290 | // Print the preds of this block according to the CFG. |
| 291 | if (!pred_empty()) { |
Jakob Stoklund Olesen | b705023 | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 292 | if (Indexes) OS << '\t'; |
Chris Lattner | 9a1e91b | 2006-09-26 03:41:59 +0000 | [diff] [blame] | 293 | OS << " Predecessors according to CFG:"; |
| 294 | for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 295 | OS << " BB#" << (*PI)->getNumber(); |
Chris Lattner | af119ca | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 296 | OS << '\n'; |
Chris Lattner | 9a1e91b | 2006-09-26 03:41:59 +0000 | [diff] [blame] | 297 | } |
Jakob Stoklund Olesen | b705023 | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 298 | |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 299 | for (const_instr_iterator I = instr_begin(); I != instr_end(); ++I) { |
Jakob Stoklund Olesen | b705023 | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 300 | if (Indexes) { |
| 301 | if (Indexes->hasIndex(I)) |
| 302 | OS << Indexes->getInstructionIndex(I); |
| 303 | OS << '\t'; |
| 304 | } |
Chris Lattner | 1216f54 | 2009-08-23 00:47:04 +0000 | [diff] [blame] | 305 | OS << '\t'; |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 306 | if (I->isInsideBundle()) |
| 307 | OS << " * "; |
Duncan P. N. Exon Smith | f48e982 | 2015-06-26 22:06:47 +0000 | [diff] [blame] | 308 | I->print(OS, MST); |
Alkis Evlogimenos | fcb3f51 | 2004-09-05 18:39:20 +0000 | [diff] [blame] | 309 | } |
Chris Lattner | 329c14a | 2005-04-01 06:48:38 +0000 | [diff] [blame] | 310 | |
| 311 | // Print the successors of this block according to the CFG. |
| 312 | if (!succ_empty()) { |
Jakob Stoklund Olesen | b705023 | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 313 | if (Indexes) OS << '\t'; |
Chris Lattner | 329c14a | 2005-04-01 06:48:38 +0000 | [diff] [blame] | 314 | OS << " Successors according to CFG:"; |
Jakob Stoklund Olesen | 1dc107a | 2012-08-13 23:13:23 +0000 | [diff] [blame] | 315 | for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) { |
Dan Gohman | 34341e6 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 316 | OS << " BB#" << (*SI)->getNumber(); |
Jakob Stoklund Olesen | 1dc107a | 2012-08-13 23:13:23 +0000 | [diff] [blame] | 317 | if (!Weights.empty()) |
| 318 | OS << '(' << *getWeightIterator(SI) << ')'; |
| 319 | } |
Chris Lattner | af119ca | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 320 | OS << '\n'; |
Chris Lattner | 329c14a | 2005-04-01 06:48:38 +0000 | [diff] [blame] | 321 | } |
Alkis Evlogimenos | 14f3fe8 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 322 | } |
Chris Lattner | 4336b87 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 323 | |
Cong Hou | 2793e72 | 2015-08-10 22:27:10 +0000 | [diff] [blame^] | 324 | void MachineBasicBlock::printAsOperand(raw_ostream &OS, |
| 325 | bool /*PrintType*/) const { |
Chandler Carruth | d48cdbf | 2014-01-09 02:29:41 +0000 | [diff] [blame] | 326 | OS << "BB#" << getNumber(); |
| 327 | } |
| 328 | |
Evan Cheng | f7ed82d | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 329 | void MachineBasicBlock::removeLiveIn(unsigned Reg) { |
Dan Gohman | 9d2d053 | 2010-04-13 16:57:55 +0000 | [diff] [blame] | 330 | std::vector<unsigned>::iterator I = |
| 331 | std::find(LiveIns.begin(), LiveIns.end(), Reg); |
Jakob Stoklund Olesen | 8e58c90 | 2012-03-28 20:11:42 +0000 | [diff] [blame] | 332 | if (I != LiveIns.end()) |
| 333 | LiveIns.erase(I); |
Evan Cheng | f7ed82d | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Aaron Ballman | 9f154f6 | 2015-07-29 15:57:49 +0000 | [diff] [blame] | 336 | bool MachineBasicBlock::isLiveIn(unsigned Reg) const { |
| 337 | livein_iterator I = std::find(livein_begin(), livein_end(), Reg); |
| 338 | return I != livein_end(); |
| 339 | } |
| 340 | |
Jakob Stoklund Olesen | 533c3bf | 2013-07-03 23:56:20 +0000 | [diff] [blame] | 341 | unsigned |
| 342 | MachineBasicBlock::addLiveIn(unsigned PhysReg, const TargetRegisterClass *RC) { |
| 343 | assert(getParent() && "MBB must be inserted in function"); |
| 344 | assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) && "Expected physreg"); |
| 345 | assert(RC && "Register class is required"); |
| 346 | assert((isLandingPad() || this == &getParent()->front()) && |
| 347 | "Only the entry block and landing pads can have physreg live ins"); |
| 348 | |
| 349 | bool LiveIn = isLiveIn(PhysReg); |
Jakob Stoklund Olesen | bbbb532 | 2013-07-04 04:32:35 +0000 | [diff] [blame] | 350 | iterator I = SkipPHIsAndLabels(begin()), E = end(); |
Jakob Stoklund Olesen | 533c3bf | 2013-07-03 23:56:20 +0000 | [diff] [blame] | 351 | MachineRegisterInfo &MRI = getParent()->getRegInfo(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 352 | const TargetInstrInfo &TII = *getParent()->getSubtarget().getInstrInfo(); |
Jakob Stoklund Olesen | 533c3bf | 2013-07-03 23:56:20 +0000 | [diff] [blame] | 353 | |
| 354 | // Look for an existing copy. |
| 355 | if (LiveIn) |
| 356 | for (;I != E && I->isCopy(); ++I) |
| 357 | if (I->getOperand(1).getReg() == PhysReg) { |
| 358 | unsigned VirtReg = I->getOperand(0).getReg(); |
| 359 | if (!MRI.constrainRegClass(VirtReg, RC)) |
| 360 | llvm_unreachable("Incompatible live-in register class."); |
| 361 | return VirtReg; |
| 362 | } |
| 363 | |
| 364 | // No luck, create a virtual register. |
| 365 | unsigned VirtReg = MRI.createVirtualRegister(RC); |
| 366 | BuildMI(*this, I, DebugLoc(), TII.get(TargetOpcode::COPY), VirtReg) |
| 367 | .addReg(PhysReg, RegState::Kill); |
| 368 | if (!LiveIn) |
| 369 | addLiveIn(PhysReg); |
| 370 | return VirtReg; |
| 371 | } |
| 372 | |
Chris Lattner | 94866be | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 373 | void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 374 | getParent()->splice(NewAfter, this); |
Chris Lattner | 94866be | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { |
Chris Lattner | 94866be | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 378 | MachineFunction::iterator BBI = NewBefore; |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 379 | getParent()->splice(++BBI, this); |
Chris Lattner | 94866be | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Jim Grosbach | 801b33b | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 382 | void MachineBasicBlock::updateTerminator() { |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 383 | const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); |
Jim Grosbach | 801b33b | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 384 | // A block with no successors has no concerns with fall-through edges. |
| 385 | if (this->succ_empty()) return; |
| 386 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 387 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; |
Jim Grosbach | 801b33b | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 388 | SmallVector<MachineOperand, 4> Cond; |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 389 | DebugLoc dl; // FIXME: this is nowhere |
Jim Grosbach | 801b33b | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 390 | bool B = TII->AnalyzeBranch(*this, TBB, FBB, Cond); |
| 391 | (void) B; |
| 392 | assert(!B && "UpdateTerminators requires analyzable predecessors!"); |
| 393 | if (Cond.empty()) { |
| 394 | if (TBB) { |
| 395 | // The block has an unconditional branch. If its successor is now |
| 396 | // its layout successor, delete the branch. |
| 397 | if (isLayoutSuccessor(TBB)) |
| 398 | TII->RemoveBranch(*this); |
| 399 | } else { |
| 400 | // The block has an unconditional fallthrough. If its successor is not |
Chandler Carruth | ee54feb | 2011-11-22 13:13:16 +0000 | [diff] [blame] | 401 | // its layout successor, insert a branch. First we have to locate the |
| 402 | // only non-landing-pad successor, as that is the fallthrough block. |
| 403 | for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) { |
| 404 | if ((*SI)->isLandingPad()) |
| 405 | continue; |
| 406 | assert(!TBB && "Found more than one non-landing-pad successor!"); |
| 407 | TBB = *SI; |
| 408 | } |
Chandler Carruth | 8c68f1f | 2011-11-23 08:23:54 +0000 | [diff] [blame] | 409 | |
| 410 | // If there is no non-landing-pad successor, the block has no |
| 411 | // fall-through edges to be concerned with. |
| 412 | if (!TBB) |
| 413 | return; |
| 414 | |
| 415 | // Finally update the unconditional successor to be reached via a branch |
| 416 | // if it would not be reached by fallthrough. |
Jim Grosbach | 801b33b | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 417 | if (!isLayoutSuccessor(TBB)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 418 | TII->InsertBranch(*this, TBB, nullptr, Cond, dl); |
Jim Grosbach | 801b33b | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 419 | } |
| 420 | } else { |
| 421 | if (FBB) { |
| 422 | // The block has a non-fallthrough conditional branch. If one of its |
| 423 | // successors is its layout successor, rewrite it to a fallthrough |
| 424 | // conditional branch. |
| 425 | if (isLayoutSuccessor(TBB)) { |
Jakob Stoklund Olesen | 8a3fdae | 2009-11-22 18:28:04 +0000 | [diff] [blame] | 426 | if (TII->ReverseBranchCondition(Cond)) |
| 427 | return; |
Jim Grosbach | 801b33b | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 428 | TII->RemoveBranch(*this); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 429 | TII->InsertBranch(*this, FBB, nullptr, Cond, dl); |
Jim Grosbach | 801b33b | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 430 | } else if (isLayoutSuccessor(FBB)) { |
| 431 | TII->RemoveBranch(*this); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 432 | TII->InsertBranch(*this, TBB, nullptr, Cond, dl); |
Jim Grosbach | 801b33b | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 433 | } |
| 434 | } else { |
Chandler Carruth | 1f5580b | 2012-04-16 22:03:00 +0000 | [diff] [blame] | 435 | // Walk through the successors and find the successor which is not |
| 436 | // a landing pad and is not the conditional branch destination (in TBB) |
| 437 | // as the fallthrough successor. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 438 | MachineBasicBlock *FallthroughBB = nullptr; |
Chandler Carruth | 1f5580b | 2012-04-16 22:03:00 +0000 | [diff] [blame] | 439 | for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) { |
| 440 | if ((*SI)->isLandingPad() || *SI == TBB) |
| 441 | continue; |
| 442 | assert(!FallthroughBB && "Found more than one fallthrough successor."); |
| 443 | FallthroughBB = *SI; |
| 444 | } |
| 445 | if (!FallthroughBB && canFallThrough()) { |
| 446 | // We fallthrough to the same basic block as the conditional jump |
| 447 | // targets. Remove the conditional jump, leaving unconditional |
| 448 | // fallthrough. |
Cong Hou | 2793e72 | 2015-08-10 22:27:10 +0000 | [diff] [blame^] | 449 | // FIXME: This does not seem like a reasonable pattern to support, but |
| 450 | // it has been seen in the wild coming out of degenerate ARM test cases. |
Chandler Carruth | 1f5580b | 2012-04-16 22:03:00 +0000 | [diff] [blame] | 451 | TII->RemoveBranch(*this); |
| 452 | |
| 453 | // Finally update the unconditional successor to be reached via a branch |
| 454 | // if it would not be reached by fallthrough. |
| 455 | if (!isLayoutSuccessor(TBB)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 456 | TII->InsertBranch(*this, TBB, nullptr, Cond, dl); |
Chandler Carruth | 1f5580b | 2012-04-16 22:03:00 +0000 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | |
Jim Grosbach | 801b33b | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 460 | // The block has a fallthrough conditional branch. |
Jim Grosbach | 801b33b | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 461 | if (isLayoutSuccessor(TBB)) { |
Jakob Stoklund Olesen | 8a3fdae | 2009-11-22 18:28:04 +0000 | [diff] [blame] | 462 | if (TII->ReverseBranchCondition(Cond)) { |
| 463 | // We can't reverse the condition, add an unconditional branch. |
| 464 | Cond.clear(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 465 | TII->InsertBranch(*this, FallthroughBB, nullptr, Cond, dl); |
Jakob Stoklund Olesen | 8a3fdae | 2009-11-22 18:28:04 +0000 | [diff] [blame] | 466 | return; |
| 467 | } |
Jim Grosbach | 801b33b | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 468 | TII->RemoveBranch(*this); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 469 | TII->InsertBranch(*this, FallthroughBB, nullptr, Cond, dl); |
Chandler Carruth | 1f5580b | 2012-04-16 22:03:00 +0000 | [diff] [blame] | 470 | } else if (!isLayoutSuccessor(FallthroughBB)) { |
Jim Grosbach | 801b33b | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 471 | TII->RemoveBranch(*this); |
Chandler Carruth | 1f5580b | 2012-04-16 22:03:00 +0000 | [diff] [blame] | 472 | TII->InsertBranch(*this, TBB, FallthroughBB, Cond, dl); |
Jim Grosbach | 801b33b | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | } |
| 476 | } |
Chris Lattner | 94866be | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 477 | |
Jakub Staszak | 12a43bd | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 478 | void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ, uint32_t weight) { |
| 479 | |
| 480 | // If we see non-zero value for the first time it means we actually use Weight |
| 481 | // list, so we fill all Weights with 0's. |
| 482 | if (weight != 0 && Weights.empty()) |
| 483 | Weights.resize(Successors.size()); |
| 484 | |
Cong Hou | ec10587 | 2015-08-06 18:17:29 +0000 | [diff] [blame] | 485 | if (weight != 0 || !Weights.empty()) |
Jakub Staszak | 12a43bd | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 486 | Weights.push_back(weight); |
| 487 | |
Cong Hou | 2793e72 | 2015-08-10 22:27:10 +0000 | [diff] [blame^] | 488 | Successors.push_back(succ); |
| 489 | succ->addPredecessor(this); |
| 490 | } |
Chris Lattner | 4336b87 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 491 | |
| 492 | void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { |
| 493 | succ->removePredecessor(this); |
| 494 | succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); |
| 495 | assert(I != Successors.end() && "Not a current successor!"); |
Jakub Staszak | 12a43bd | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 496 | |
| 497 | // If Weight list is empty it means we don't use it (disabled optimization). |
| 498 | if (!Weights.empty()) { |
| 499 | weight_iterator WI = getWeightIterator(I); |
| 500 | Weights.erase(WI); |
| 501 | } |
| 502 | |
Chris Lattner | 4336b87 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 503 | Successors.erase(I); |
| 504 | } |
| 505 | |
Jakub Staszak | feadd43 | 2011-06-16 18:01:17 +0000 | [diff] [blame] | 506 | MachineBasicBlock::succ_iterator |
Chris Lattner | 574e716 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 507 | MachineBasicBlock::removeSuccessor(succ_iterator I) { |
Chris Lattner | 4336b87 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 508 | assert(I != Successors.end() && "Not a current successor!"); |
Jakub Staszak | 12a43bd | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 509 | |
| 510 | // If Weight list is empty it means we don't use it (disabled optimization). |
| 511 | if (!Weights.empty()) { |
| 512 | weight_iterator WI = getWeightIterator(I); |
| 513 | Weights.erase(WI); |
| 514 | } |
| 515 | |
Chris Lattner | 4336b87 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 516 | (*I)->removePredecessor(this); |
Dan Gohman | f87dc92 | 2009-01-08 22:19:34 +0000 | [diff] [blame] | 517 | return Successors.erase(I); |
Chris Lattner | 4336b87 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Jakub Staszak | 12a43bd | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 520 | void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old, |
| 521 | MachineBasicBlock *New) { |
Jakob Stoklund Olesen | 8c28ac9 | 2012-08-10 03:23:27 +0000 | [diff] [blame] | 522 | if (Old == New) |
| 523 | return; |
Jakub Staszak | 12a43bd | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 524 | |
Jakob Stoklund Olesen | 8c28ac9 | 2012-08-10 03:23:27 +0000 | [diff] [blame] | 525 | succ_iterator E = succ_end(); |
| 526 | succ_iterator NewI = E; |
| 527 | succ_iterator OldI = E; |
| 528 | for (succ_iterator I = succ_begin(); I != E; ++I) { |
| 529 | if (*I == Old) { |
| 530 | OldI = I; |
| 531 | if (NewI != E) |
| 532 | break; |
| 533 | } |
| 534 | if (*I == New) { |
| 535 | NewI = I; |
| 536 | if (OldI != E) |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | assert(OldI != E && "Old is not a successor of this block"); |
| 541 | Old->removePredecessor(this); |
| 542 | |
| 543 | // If New isn't already a successor, let it take Old's place. |
| 544 | if (NewI == E) { |
| 545 | New->addPredecessor(this); |
| 546 | *OldI = New; |
| 547 | return; |
Jakub Staszak | 12a43bd | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Jakob Stoklund Olesen | 8c28ac9 | 2012-08-10 03:23:27 +0000 | [diff] [blame] | 550 | // New is already a successor. |
| 551 | // Update its weight instead of adding a duplicate edge. |
| 552 | if (!Weights.empty()) { |
| 553 | weight_iterator OldWI = getWeightIterator(OldI); |
| 554 | *getWeightIterator(NewI) += *OldWI; |
| 555 | Weights.erase(OldWI); |
| 556 | } |
| 557 | Successors.erase(OldI); |
Jakub Staszak | 12a43bd | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Chris Lattner | 4336b87 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 560 | void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { |
| 561 | Predecessors.push_back(pred); |
| 562 | } |
| 563 | |
| 564 | void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { |
Eli Friedman | ec138b4 | 2011-04-18 21:21:37 +0000 | [diff] [blame] | 565 | pred_iterator I = std::find(Predecessors.begin(), Predecessors.end(), pred); |
Chris Lattner | 4336b87 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 566 | assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); |
| 567 | Predecessors.erase(I); |
| 568 | } |
Evan Cheng | a92b2b3 | 2007-05-17 23:58:53 +0000 | [diff] [blame] | 569 | |
Chris Lattner | af119ca | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 570 | void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) { |
Mon P Wang | 3e58393 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 571 | if (this == fromMBB) |
| 572 | return; |
Jakub Staszak | feadd43 | 2011-06-16 18:01:17 +0000 | [diff] [blame] | 573 | |
Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 574 | while (!fromMBB->succ_empty()) { |
| 575 | MachineBasicBlock *Succ = *fromMBB->succ_begin(); |
Jakob Stoklund Olesen | 396b595 | 2012-08-13 23:13:25 +0000 | [diff] [blame] | 576 | uint32_t Weight = 0; |
Jakub Staszak | 12a43bd | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 577 | |
| 578 | // If Weight list is empty it means we don't use it (disabled optimization). |
| 579 | if (!fromMBB->Weights.empty()) |
Jakob Stoklund Olesen | 396b595 | 2012-08-13 23:13:25 +0000 | [diff] [blame] | 580 | Weight = *fromMBB->Weights.begin(); |
Jakub Staszak | 12a43bd | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 581 | |
Jakob Stoklund Olesen | 396b595 | 2012-08-13 23:13:25 +0000 | [diff] [blame] | 582 | addSuccessor(Succ, Weight); |
Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 583 | fromMBB->removeSuccessor(Succ); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | void |
| 588 | MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB) { |
| 589 | if (this == fromMBB) |
| 590 | return; |
Jakub Staszak | feadd43 | 2011-06-16 18:01:17 +0000 | [diff] [blame] | 591 | |
Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 592 | while (!fromMBB->succ_empty()) { |
| 593 | MachineBasicBlock *Succ = *fromMBB->succ_begin(); |
Jakob Stoklund Olesen | 396b595 | 2012-08-13 23:13:25 +0000 | [diff] [blame] | 594 | uint32_t Weight = 0; |
| 595 | if (!fromMBB->Weights.empty()) |
| 596 | Weight = *fromMBB->Weights.begin(); |
| 597 | addSuccessor(Succ, Weight); |
Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 598 | fromMBB->removeSuccessor(Succ); |
| 599 | |
| 600 | // Fix up any PHI nodes in the successor. |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 601 | for (MachineBasicBlock::instr_iterator MI = Succ->instr_begin(), |
| 602 | ME = Succ->instr_end(); MI != ME && MI->isPHI(); ++MI) |
Dan Gohman | 3439629 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 603 | for (unsigned i = 2, e = MI->getNumOperands()+1; i != e; i += 2) { |
| 604 | MachineOperand &MO = MI->getOperand(i); |
| 605 | if (MO.getMBB() == fromMBB) |
| 606 | MO.setMBB(this); |
| 607 | } |
| 608 | } |
Mon P Wang | 3e58393 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Jakob Stoklund Olesen | fee94ca | 2012-07-30 17:36:47 +0000 | [diff] [blame] | 611 | bool MachineBasicBlock::isPredecessor(const MachineBasicBlock *MBB) const { |
| 612 | return std::find(pred_begin(), pred_end(), MBB) != pred_end(); |
| 613 | } |
| 614 | |
Dan Gohman | ff62c62 | 2009-03-30 20:06:29 +0000 | [diff] [blame] | 615 | bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const { |
Jakob Stoklund Olesen | fee94ca | 2012-07-30 17:36:47 +0000 | [diff] [blame] | 616 | return std::find(succ_begin(), succ_end(), MBB) != succ_end(); |
Evan Cheng | a92b2b3 | 2007-05-17 23:58:53 +0000 | [diff] [blame] | 617 | } |
Evan Cheng | df75785 | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 618 | |
Dan Gohman | ff62c62 | 2009-03-30 20:06:29 +0000 | [diff] [blame] | 619 | bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const { |
Dan Gohman | a78bae3 | 2008-10-02 22:09:09 +0000 | [diff] [blame] | 620 | MachineFunction::const_iterator I(this); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 621 | return std::next(I) == MachineFunction::const_iterator(MBB); |
Dan Gohman | a78bae3 | 2008-10-02 22:09:09 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Bob Wilson | 2d4ff12 | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 624 | bool MachineBasicBlock::canFallThrough() { |
Bob Wilson | 2d4ff12 | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 625 | MachineFunction::iterator Fallthrough = this; |
| 626 | ++Fallthrough; |
| 627 | // If FallthroughBlock is off the end of the function, it can't fall through. |
| 628 | if (Fallthrough == getParent()->end()) |
| 629 | return false; |
| 630 | |
| 631 | // If FallthroughBlock isn't a successor, no fallthrough is possible. |
| 632 | if (!isSuccessor(Fallthrough)) |
| 633 | return false; |
| 634 | |
Dan Gohman | 0b44cb0 | 2009-12-05 00:32:59 +0000 | [diff] [blame] | 635 | // Analyze the branches, if any, at the end of the block. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 636 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; |
Dan Gohman | 0b44cb0 | 2009-12-05 00:32:59 +0000 | [diff] [blame] | 637 | SmallVector<MachineOperand, 4> Cond; |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 638 | const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); |
Jakob Stoklund Olesen | 834d70d | 2010-01-15 20:00:12 +0000 | [diff] [blame] | 639 | if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) { |
Dan Gohman | 0b44cb0 | 2009-12-05 00:32:59 +0000 | [diff] [blame] | 640 | // If we couldn't analyze the branch, examine the last instruction. |
| 641 | // If the block doesn't end in a known control barrier, assume fallthrough |
Chad Rosier | 1a1531d | 2012-01-26 18:24:25 +0000 | [diff] [blame] | 642 | // is possible. The isPredicated check is needed because this code can be |
Dan Gohman | 0b44cb0 | 2009-12-05 00:32:59 +0000 | [diff] [blame] | 643 | // called during IfConversion, where an instruction which is normally a |
Chad Rosier | 9b61cf3 | 2012-01-26 20:19:05 +0000 | [diff] [blame] | 644 | // Barrier is predicated and thus no longer an actual control barrier. |
Chad Rosier | 1a1531d | 2012-01-26 18:24:25 +0000 | [diff] [blame] | 645 | return empty() || !back().isBarrier() || TII->isPredicated(&back()); |
Dan Gohman | 0b44cb0 | 2009-12-05 00:32:59 +0000 | [diff] [blame] | 646 | } |
Bob Wilson | 2d4ff12 | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 647 | |
| 648 | // If there is no branch, control always falls through. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 649 | if (!TBB) return true; |
Bob Wilson | 2d4ff12 | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 650 | |
| 651 | // If there is some explicit branch to the fallthrough block, it can obviously |
| 652 | // reach, even though the branch should get folded to fall through implicitly. |
| 653 | if (MachineFunction::iterator(TBB) == Fallthrough || |
| 654 | MachineFunction::iterator(FBB) == Fallthrough) |
| 655 | return true; |
| 656 | |
| 657 | // If it's an unconditional branch to some block not the fall through, it |
| 658 | // doesn't fall through. |
| 659 | if (Cond.empty()) return false; |
| 660 | |
| 661 | // Otherwise, if it is conditional and has no explicit false block, it falls |
| 662 | // through. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 663 | return FBB == nullptr; |
Bob Wilson | 2d4ff12 | 2009-11-26 00:32:21 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 666 | MachineBasicBlock * |
| 667 | MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P) { |
Evan Cheng | 2d14d8a | 2012-04-24 19:06:55 +0000 | [diff] [blame] | 668 | // Splitting the critical edge to a landing pad block is non-trivial. Don't do |
| 669 | // it in this generic function. |
| 670 | if (Succ->isLandingPad()) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 671 | return nullptr; |
Evan Cheng | 2d14d8a | 2012-04-24 19:06:55 +0000 | [diff] [blame] | 672 | |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 673 | MachineFunction *MF = getParent(); |
| 674 | DebugLoc dl; // FIXME: this is nowhere |
| 675 | |
Vincent Lejeune | 92b0a64 | 2013-12-07 01:49:19 +0000 | [diff] [blame] | 676 | // Performance might be harmed on HW that implements branching using exec mask |
| 677 | // where both sides of the branches are always executed. |
| 678 | if (MF->getTarget().requiresStructuredCFG()) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 679 | return nullptr; |
Vincent Lejeune | 92b0a64 | 2013-12-07 01:49:19 +0000 | [diff] [blame] | 680 | |
Jakob Stoklund Olesen | ea26319 | 2010-11-02 00:58:37 +0000 | [diff] [blame] | 681 | // We may need to update this's terminator, but we can't do that if |
| 682 | // AnalyzeBranch fails. If this uses a jump table, we won't touch it. |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 683 | const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 684 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 685 | SmallVector<MachineOperand, 4> Cond; |
| 686 | if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 687 | return nullptr; |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 688 | |
Jakob Stoklund Olesen | ea26319 | 2010-11-02 00:58:37 +0000 | [diff] [blame] | 689 | // Avoid bugpoint weirdness: A block may end with a conditional branch but |
| 690 | // jumps to the same MBB is either case. We have duplicate CFG edges in that |
| 691 | // case that we can't handle. Since this never happens in properly optimized |
| 692 | // code, just skip those edges. |
| 693 | if (TBB && TBB == FBB) { |
| 694 | DEBUG(dbgs() << "Won't split critical edge after degenerate BB#" |
| 695 | << getNumber() << '\n'); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 696 | return nullptr; |
Jakob Stoklund Olesen | ea26319 | 2010-11-02 00:58:37 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 699 | MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 700 | MF->insert(std::next(MachineFunction::iterator(this)), NMBB); |
Evan Cheng | a684824 | 2010-08-17 17:15:14 +0000 | [diff] [blame] | 701 | DEBUG(dbgs() << "Splitting critical edge:" |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 702 | " BB#" << getNumber() |
| 703 | << " -- BB#" << NMBB->getNumber() |
| 704 | << " -- BB#" << Succ->getNumber() << '\n'); |
Cameron Zwarich | cdcab38 | 2013-02-12 03:49:20 +0000 | [diff] [blame] | 705 | |
| 706 | LiveIntervals *LIS = P->getAnalysisIfAvailable<LiveIntervals>(); |
Cameron Zwarich | 21beaf6 | 2013-02-10 23:29:54 +0000 | [diff] [blame] | 707 | SlotIndexes *Indexes = P->getAnalysisIfAvailable<SlotIndexes>(); |
Cameron Zwarich | cdcab38 | 2013-02-12 03:49:20 +0000 | [diff] [blame] | 708 | if (LIS) |
| 709 | LIS->insertMBBInMaps(NMBB); |
| 710 | else if (Indexes) |
Cameron Zwarich | 21beaf6 | 2013-02-10 23:29:54 +0000 | [diff] [blame] | 711 | Indexes->insertMBBInMaps(NMBB); |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 712 | |
Jakob Stoklund Olesen | dd6fcc4 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 713 | // On some targets like Mips, branches may kill virtual registers. Make sure |
| 714 | // that LiveVariables is properly updated after updateTerminator replaces the |
| 715 | // terminators. |
| 716 | LiveVariables *LV = P->getAnalysisIfAvailable<LiveVariables>(); |
| 717 | |
| 718 | // Collect a list of virtual registers killed by the terminators. |
| 719 | SmallVector<unsigned, 4> KilledRegs; |
| 720 | if (LV) |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 721 | for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); |
Evan Cheng | 2a81dd4 | 2011-12-06 22:12:01 +0000 | [diff] [blame] | 722 | I != E; ++I) { |
Jakob Stoklund Olesen | dd6fcc4 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 723 | MachineInstr *MI = I; |
| 724 | for (MachineInstr::mop_iterator OI = MI->operands_begin(), |
| 725 | OE = MI->operands_end(); OI != OE; ++OI) { |
Lang Hames | edeea17 | 2012-02-09 05:59:36 +0000 | [diff] [blame] | 726 | if (!OI->isReg() || OI->getReg() == 0 || |
| 727 | !OI->isUse() || !OI->isKill() || OI->isUndef()) |
Jakob Stoklund Olesen | dd6fcc4 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 728 | continue; |
| 729 | unsigned Reg = OI->getReg(); |
Lang Hames | edeea17 | 2012-02-09 05:59:36 +0000 | [diff] [blame] | 730 | if (TargetRegisterInfo::isPhysicalRegister(Reg) || |
Jakob Stoklund Olesen | dd6fcc4 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 731 | LV->getVarInfo(Reg).removeKill(MI)) { |
| 732 | KilledRegs.push_back(Reg); |
| 733 | DEBUG(dbgs() << "Removing terminator kill: " << *MI); |
| 734 | OI->setIsKill(false); |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | |
Cameron Zwarich | bfebb41 | 2013-02-17 00:10:44 +0000 | [diff] [blame] | 739 | SmallVector<unsigned, 4> UsedRegs; |
| 740 | if (LIS) { |
| 741 | for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); |
| 742 | I != E; ++I) { |
| 743 | MachineInstr *MI = I; |
| 744 | |
| 745 | for (MachineInstr::mop_iterator OI = MI->operands_begin(), |
| 746 | OE = MI->operands_end(); OI != OE; ++OI) { |
| 747 | if (!OI->isReg() || OI->getReg() == 0) |
| 748 | continue; |
| 749 | |
| 750 | unsigned Reg = OI->getReg(); |
| 751 | if (std::find(UsedRegs.begin(), UsedRegs.end(), Reg) == UsedRegs.end()) |
| 752 | UsedRegs.push_back(Reg); |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 757 | ReplaceUsesOfBlockWith(Succ, NMBB); |
Cameron Zwarich | ba378ce | 2013-02-11 09:24:45 +0000 | [diff] [blame] | 758 | |
| 759 | // If updateTerminator() removes instructions, we need to remove them from |
| 760 | // SlotIndexes. |
| 761 | SmallVector<MachineInstr*, 4> Terminators; |
| 762 | if (Indexes) { |
| 763 | for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); |
| 764 | I != E; ++I) |
| 765 | Terminators.push_back(I); |
| 766 | } |
| 767 | |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 768 | updateTerminator(); |
| 769 | |
Cameron Zwarich | ba378ce | 2013-02-11 09:24:45 +0000 | [diff] [blame] | 770 | if (Indexes) { |
| 771 | SmallVector<MachineInstr*, 4> NewTerminators; |
| 772 | for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); |
| 773 | I != E; ++I) |
| 774 | NewTerminators.push_back(I); |
| 775 | |
| 776 | for (SmallVectorImpl<MachineInstr*>::iterator I = Terminators.begin(), |
| 777 | E = Terminators.end(); I != E; ++I) { |
| 778 | if (std::find(NewTerminators.begin(), NewTerminators.end(), *I) == |
| 779 | NewTerminators.end()) |
| 780 | Indexes->removeMachineInstrFromMaps(*I); |
| 781 | } |
| 782 | } |
| 783 | |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 784 | // Insert unconditional "jump Succ" instruction in NMBB if necessary. |
| 785 | NMBB->addSuccessor(Succ); |
| 786 | if (!NMBB->isLayoutSuccessor(Succ)) { |
| 787 | Cond.clear(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 788 | MF->getSubtarget().getInstrInfo()->InsertBranch(*NMBB, Succ, nullptr, Cond, |
| 789 | dl); |
Cameron Zwarich | 21beaf6 | 2013-02-10 23:29:54 +0000 | [diff] [blame] | 790 | |
| 791 | if (Indexes) { |
| 792 | for (instr_iterator I = NMBB->instr_begin(), E = NMBB->instr_end(); |
| 793 | I != E; ++I) { |
| 794 | // Some instructions may have been moved to NMBB by updateTerminator(), |
| 795 | // so we first remove any instruction that already has an index. |
| 796 | if (Indexes->hasIndex(I)) |
| 797 | Indexes->removeMachineInstrFromMaps(I); |
| 798 | Indexes->insertMachineInstrInMaps(I); |
| 799 | } |
| 800 | } |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | // Fix PHI nodes in Succ so they refer to NMBB instead of this |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 804 | for (MachineBasicBlock::instr_iterator |
| 805 | i = Succ->instr_begin(),e = Succ->instr_end(); |
| 806 | i != e && i->isPHI(); ++i) |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 807 | for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2) |
| 808 | if (i->getOperand(ni+1).getMBB() == this) |
| 809 | i->getOperand(ni+1).setMBB(NMBB); |
| 810 | |
Jakob Stoklund Olesen | 06b6ccf | 2011-10-14 17:25:46 +0000 | [diff] [blame] | 811 | // Inherit live-ins from the successor |
| 812 | for (MachineBasicBlock::livein_iterator I = Succ->livein_begin(), |
Bill Wendling | d163405 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 813 | E = Succ->livein_end(); I != E; ++I) |
Jakob Stoklund Olesen | 06b6ccf | 2011-10-14 17:25:46 +0000 | [diff] [blame] | 814 | NMBB->addLiveIn(*I); |
| 815 | |
Jakob Stoklund Olesen | dd6fcc4 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 816 | // Update LiveVariables. |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 817 | const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); |
Jakob Stoklund Olesen | dd6fcc4 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 818 | if (LV) { |
| 819 | // Restore kills of virtual registers that were killed by the terminators. |
| 820 | while (!KilledRegs.empty()) { |
| 821 | unsigned Reg = KilledRegs.pop_back_val(); |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 822 | for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) { |
Lang Hames | edeea17 | 2012-02-09 05:59:36 +0000 | [diff] [blame] | 823 | if (!(--I)->addRegisterKilled(Reg, TRI, /* addIfNotFound= */ false)) |
Jakob Stoklund Olesen | dd6fcc4 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 824 | continue; |
Lang Hames | edeea17 | 2012-02-09 05:59:36 +0000 | [diff] [blame] | 825 | if (TargetRegisterInfo::isVirtualRegister(Reg)) |
| 826 | LV->getVarInfo(Reg).Kills.push_back(I); |
Jakob Stoklund Olesen | dd6fcc4 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 827 | DEBUG(dbgs() << "Restored terminator kill: " << *I); |
| 828 | break; |
| 829 | } |
| 830 | } |
| 831 | // Update relevant live-through information. |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 832 | LV->addNewBlock(NMBB, this, Succ); |
Jakob Stoklund Olesen | dd6fcc4 | 2011-05-29 20:10:28 +0000 | [diff] [blame] | 833 | } |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 834 | |
Cameron Zwarich | cdcab38 | 2013-02-12 03:49:20 +0000 | [diff] [blame] | 835 | if (LIS) { |
Cameron Zwarich | b47fb38 | 2013-02-11 09:24:47 +0000 | [diff] [blame] | 836 | // After splitting the edge and updating SlotIndexes, live intervals may be |
| 837 | // in one of two situations, depending on whether this block was the last in |
Cong Hou | 2793e72 | 2015-08-10 22:27:10 +0000 | [diff] [blame^] | 838 | // the function. If the original block was the last in the function, all |
| 839 | // live intervals will end prior to the beginning of the new split block. If |
| 840 | // the original block was not at the end of the function, all live intervals |
| 841 | // will extend to the end of the new split block. |
Cameron Zwarich | b47fb38 | 2013-02-11 09:24:47 +0000 | [diff] [blame] | 842 | |
| 843 | bool isLastMBB = |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 844 | std::next(MachineFunction::iterator(NMBB)) == getParent()->end(); |
Cameron Zwarich | b47fb38 | 2013-02-11 09:24:47 +0000 | [diff] [blame] | 845 | |
| 846 | SlotIndex StartIndex = Indexes->getMBBEndIdx(this); |
| 847 | SlotIndex PrevIndex = StartIndex.getPrevSlot(); |
| 848 | SlotIndex EndIndex = Indexes->getMBBEndIdx(NMBB); |
| 849 | |
| 850 | // Find the registers used from NMBB in PHIs in Succ. |
| 851 | SmallSet<unsigned, 8> PHISrcRegs; |
| 852 | for (MachineBasicBlock::instr_iterator |
| 853 | I = Succ->instr_begin(), E = Succ->instr_end(); |
| 854 | I != E && I->isPHI(); ++I) { |
| 855 | for (unsigned ni = 1, ne = I->getNumOperands(); ni != ne; ni += 2) { |
| 856 | if (I->getOperand(ni+1).getMBB() == NMBB) { |
| 857 | MachineOperand &MO = I->getOperand(ni); |
| 858 | unsigned Reg = MO.getReg(); |
| 859 | PHISrcRegs.insert(Reg); |
Cameron Zwarich | af34931 | 2013-02-12 03:49:17 +0000 | [diff] [blame] | 860 | if (MO.isUndef()) |
| 861 | continue; |
Cameron Zwarich | b47fb38 | 2013-02-11 09:24:47 +0000 | [diff] [blame] | 862 | |
| 863 | LiveInterval &LI = LIS->getInterval(Reg); |
| 864 | VNInfo *VNI = LI.getVNInfoAt(PrevIndex); |
Cong Hou | 2793e72 | 2015-08-10 22:27:10 +0000 | [diff] [blame^] | 865 | assert(VNI && |
| 866 | "PHI sources should be live out of their predecessors."); |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 867 | LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI)); |
Cameron Zwarich | b47fb38 | 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 | af34931 | 2013-02-12 03:49:17 +0000 | [diff] [blame] | 882 | bool isLiveOut = LI.liveAt(LIS->getMBBStartIdx(Succ)); |
Cameron Zwarich | b47fb38 | 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 | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 886 | LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI)); |
Cameron Zwarich | b47fb38 | 2013-02-11 09:24:47 +0000 | [diff] [blame] | 887 | } else if (!isLiveOut && !isLastMBB) { |
Matthias Braun | 13ddb7c | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 888 | LI.removeSegment(StartIndex, EndIndex); |
Cameron Zwarich | b47fb38 | 2013-02-11 09:24:47 +0000 | [diff] [blame] | 889 | } |
| 890 | } |
Cameron Zwarich | bfebb41 | 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 | 2495596 | 2013-02-17 11:09:00 +0000 | [diff] [blame] | 894 | LIS->repairIntervalsInRange(this, getFirstTerminator(), end(), UsedRegs); |
Cameron Zwarich | b47fb38 | 2013-02-11 09:24:47 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 897 | if (MachineDominatorTree *MDT = |
Quentin Colombet | abea99f | 2014-08-13 21:00:07 +0000 | [diff] [blame] | 898 | P->getAnalysisIfAvailable<MachineDominatorTree>()) |
| 899 | MDT->recordSplitCriticalEdge(this, Succ, NMBB); |
Dan Gohman | 3570f81 | 2010-06-22 17:25:57 +0000 | [diff] [blame] | 900 | |
Evan Cheng | 647c559 | 2010-08-17 17:43:50 +0000 | [diff] [blame] | 901 | if (MachineLoopInfo *MLI = P->getAnalysisIfAvailable<MachineLoopInfo>()) |
Dan Gohman | 3570f81 | 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 | ccfb5fb | 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 | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Jakob Stoklund Olesen | ccfb5fb | 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 | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 949 | |
Jakob Stoklund Olesen | ccfb5fb | 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 | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Jakob Stoklund Olesen | 422e07b | 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 | 3b46030 | 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 | df75785 | 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 | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 992 | MachineBasicBlock::instr_iterator I = instr_end(); |
| 993 | while (I != instr_begin()) { |
Evan Cheng | df75785 | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 994 | --I; |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 995 | if (!I->isTerminator()) break; |
Evan Cheng | df75785 | 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 | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1000 | if (I->getOperand(i).isMBB() && |
Dan Gohman | 38453ee | 2008-09-13 17:58:21 +0000 | [diff] [blame] | 1001 | I->getOperand(i).getMBB() == Old) |
Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1002 | I->getOperand(i).setMBB(New); |
Evan Cheng | df75785 | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Dan Gohman | bb2f107 | 2009-05-05 21:10:19 +0000 | [diff] [blame] | 1005 | // Update the successor information. |
Jakub Staszak | 12a43bd | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1006 | replaceSuccessor(Old, New); |
Evan Cheng | df75785 | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Evan Cheng | 2afd702 | 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 | 2d5967d | 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 | feadd43 | 2011-06-16 18:01:17 +0000 | [diff] [blame] | 1013 | /// |
Chris Lattner | 574e716 | 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 | 2afd702 | 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 | 2d5967d | 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 | 9564340 | 2010-04-01 00:00:43 +0000 | [diff] [blame] | 1034 | bool Changed = false; |
Evan Cheng | 2afd702 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1035 | |
Chris Lattner | a48f44d | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1036 | MachineFunction::iterator FallThru = |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1037 | std::next(MachineFunction::iterator(this)); |
Bill Wendling | 9564340 | 2010-04-01 00:00:43 +0000 | [diff] [blame] | 1038 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1039 | if (!DestA && !DestB) { |
Bill Wendling | 9564340 | 2010-04-01 00:00:43 +0000 | [diff] [blame] | 1040 | // Block falls through to successor. |
| 1041 | DestA = FallThru; |
| 1042 | DestB = FallThru; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1043 | } else if (DestA && !DestB) { |
Bill Wendling | 9564340 | 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 | 2afd702 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1046 | DestB = FallThru; |
Evan Cheng | 2afd702 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1047 | } else { |
Bill Wendling | 9564340 | 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 | 2afd702 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1050 | } |
Bill Wendling | 9564340 | 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 | 2afd702 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1055 | MachineBasicBlock::succ_iterator SI = succ_begin(); |
Evan Cheng | 2afd702 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1056 | while (SI != succ_end()) { |
Bill Wendling | 2d5967d | 2009-12-16 00:08:36 +0000 | [diff] [blame] | 1057 | const MachineBasicBlock *MBB = *SI; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1058 | if (!SeenMBBs.insert(MBB).second || |
Bill Wendling | 9564340 | 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 | fa9810d | 2010-03-31 23:26:26 +0000 | [diff] [blame] | 1061 | SI = removeSuccessor(SI); |
Bill Wendling | 9564340 | 2010-04-01 00:00:43 +0000 | [diff] [blame] | 1062 | Changed = true; |
| 1063 | } else { |
| 1064 | ++SI; |
Evan Cheng | 2afd702 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1065 | } |
| 1066 | } |
Bill Wendling | 2d5967d | 2009-12-16 00:08:36 +0000 | [diff] [blame] | 1067 | |
Bill Wendling | 9564340 | 2010-04-01 00:00:43 +0000 | [diff] [blame] | 1068 | return Changed; |
Evan Cheng | 2afd702 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 1069 | } |
Evan Cheng | 57be2f2 | 2009-11-17 19:19:59 +0000 | [diff] [blame] | 1070 | |
Dale Johannesen | c5db599 | 2010-01-20 21:36:02 +0000 | [diff] [blame] | 1071 | /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping |
Dale Johannesen | 3d1f1cc | 2010-02-10 00:11:11 +0000 | [diff] [blame] | 1072 | /// any DBG_VALUE instructions. Return UnknownLoc if there is none. |
Dale Johannesen | c5db599 | 2010-01-20 21:36:02 +0000 | [diff] [blame] | 1073 | DebugLoc |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 1074 | MachineBasicBlock::findDebugLoc(instr_iterator MBBI) { |
Dale Johannesen | c5db599 | 2010-01-20 21:36:02 +0000 | [diff] [blame] | 1075 | DebugLoc DL; |
Evan Cheng | 7fae11b | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 1076 | instr_iterator E = instr_end(); |
Evan Cheng | 2a81dd4 | 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 | c5db599 | 2010-01-20 21:36:02 +0000 | [diff] [blame] | 1085 | return DL; |
| 1086 | } |
| 1087 | |
Jakub Staszak | 12a43bd | 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 | 6bae2a5 | 2012-08-20 22:01:38 +0000 | [diff] [blame] | 1090 | uint32_t MachineBasicBlock::getSuccWeight(const_succ_iterator Succ) const { |
Jakub Staszak | 5f45dc7 | 2011-06-17 18:00:21 +0000 | [diff] [blame] | 1091 | if (Weights.empty()) |
| 1092 | return 0; |
| 1093 | |
Jakob Stoklund Olesen | 6bae2a5 | 2012-08-20 22:01:38 +0000 | [diff] [blame] | 1094 | return *getWeightIterator(Succ); |
Jakub Staszak | 12a43bd | 2011-06-16 20:22:37 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Manman Ren | b681918 | 2014-01-29 23:18:47 +0000 | [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; |
Cong Hou | ec10587 | 2015-08-06 18:17:29 +0000 | [diff] [blame] | 1101 | *getWeightIterator(I) = weight; |
Manman Ren | b681918 | 2014-01-29 23:18:47 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Jakub Staszak | 12a43bd | 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 | 5f45dc7 | 2011-06-17 18:00:21 +0000 | [diff] [blame] | 1108 | assert(Weights.size() == Successors.size() && "Async weight list!"); |
Jakub Staszak | 12a43bd | 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 | 96f8c55 | 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 | c747cda | 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, |
Matthias Braun | 07a07ba | 2015-05-27 05:12:39 +0000 | [diff] [blame] | 1132 | unsigned Reg, const_iterator Before, |
| 1133 | unsigned Neighborhood) const { |
James Molloy | c747cda | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1134 | unsigned N = Neighborhood; |
James Molloy | c747cda | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1135 | |
Matthias Braun | 07a07ba | 2015-05-27 05:12:39 +0000 | [diff] [blame] | 1136 | // Start by searching backwards from Before, looking for kills, reads or defs. |
| 1137 | const_iterator I(Before); |
James Molloy | c747cda | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1138 | // If this is the first insn in the block, don't search backwards. |
Matthias Braun | 07a07ba | 2015-05-27 05:12:39 +0000 | [diff] [blame] | 1139 | if (I != begin()) { |
James Molloy | c747cda | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1140 | do { |
| 1141 | --I; |
| 1142 | |
| 1143 | MachineOperandIteratorBase::PhysRegInfo Analysis = |
Matthias Braun | 07a07ba | 2015-05-27 05:12:39 +0000 | [diff] [blame] | 1144 | ConstMIOperands(I).analyzePhysReg(Reg, TRI); |
James Molloy | c747cda | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1145 | |
Tim Northover | dd219d0 | 2012-11-20 09:56:11 +0000 | [diff] [blame] | 1146 | if (Analysis.Defines) |
| 1147 | // Outputs happen after inputs so they take precedence if both are |
| 1148 | // present. |
| 1149 | return Analysis.DefinesDead ? LQR_Dead : LQR_Live; |
| 1150 | |
| 1151 | if (Analysis.Kills || Analysis.Clobbers) |
James Molloy | c747cda | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1152 | // Register killed, so isn't live. |
| 1153 | return LQR_Dead; |
| 1154 | |
Tim Northover | dd219d0 | 2012-11-20 09:56:11 +0000 | [diff] [blame] | 1155 | else if (Analysis.ReadsOverlap) |
James Molloy | c747cda | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1156 | // Defined or read without a previous kill - live. |
Tim Northover | dd219d0 | 2012-11-20 09:56:11 +0000 | [diff] [blame] | 1157 | return Analysis.Reads ? LQR_Live : LQR_OverlappingLive; |
James Molloy | c747cda | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1158 | |
Matthias Braun | 07a07ba | 2015-05-27 05:12:39 +0000 | [diff] [blame] | 1159 | } while (I != begin() && --N > 0); |
James Molloy | c747cda | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | // Did we get to the start of the block? |
Matthias Braun | 07a07ba | 2015-05-27 05:12:39 +0000 | [diff] [blame] | 1163 | if (I == begin()) { |
James Molloy | c747cda | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1164 | // If so, the register's state is definitely defined by the live-in state. |
| 1165 | for (MCRegAliasIterator RAI(Reg, TRI, /*IncludeSelf=*/true); |
| 1166 | RAI.isValid(); ++RAI) { |
Matthias Braun | 07a07ba | 2015-05-27 05:12:39 +0000 | [diff] [blame] | 1167 | if (isLiveIn(*RAI)) |
James Molloy | c747cda | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1168 | return (*RAI == Reg) ? LQR_Live : LQR_OverlappingLive; |
| 1169 | } |
| 1170 | |
| 1171 | return LQR_Dead; |
| 1172 | } |
| 1173 | |
| 1174 | N = Neighborhood; |
| 1175 | |
Matthias Braun | 07a07ba | 2015-05-27 05:12:39 +0000 | [diff] [blame] | 1176 | // Try searching forwards from Before, looking for reads or defs. |
| 1177 | I = const_iterator(Before); |
James Molloy | c747cda | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1178 | // If this is the last insn in the block, don't search forwards. |
Matthias Braun | 07a07ba | 2015-05-27 05:12:39 +0000 | [diff] [blame] | 1179 | if (I != end()) { |
| 1180 | for (++I; I != end() && N > 0; ++I, --N) { |
James Molloy | c747cda | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1181 | MachineOperandIteratorBase::PhysRegInfo Analysis = |
Matthias Braun | 07a07ba | 2015-05-27 05:12:39 +0000 | [diff] [blame] | 1182 | ConstMIOperands(I).analyzePhysReg(Reg, TRI); |
James Molloy | c747cda | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1183 | |
| 1184 | if (Analysis.ReadsOverlap) |
| 1185 | // Used, therefore must have been live. |
| 1186 | return (Analysis.Reads) ? |
| 1187 | LQR_Live : LQR_OverlappingLive; |
| 1188 | |
Tim Northover | dd219d0 | 2012-11-20 09:56:11 +0000 | [diff] [blame] | 1189 | else if (Analysis.Clobbers || Analysis.Defines) |
James Molloy | c747cda | 2012-09-12 10:18:23 +0000 | [diff] [blame] | 1190 | // Defined (but not read) therefore cannot have been live. |
| 1191 | return LQR_Dead; |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | // At this point we have no idea of the liveness of the register. |
| 1196 | return LQR_Unknown; |
| 1197 | } |