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" |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 15 | #include "llvm/BasicBlock.h" |
| 16 | #include "llvm/CodeGen/MachineFunction.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetRegisterInfo.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetData.h" |
Chris Lattner | f14cf85 | 2008-01-07 07:42:25 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetInstrDesc.h" |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetInstrInfo.h" |
Alkis Evlogimenos | 743d0a1 | 2004-02-23 18:14:48 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 22 | #include "llvm/Support/LeakDetector.h" |
Daniel Dunbar | 1cd1d98 | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 24 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 28 | MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb) |
Dan Gohman | 8c2b525 | 2009-10-30 01:27:03 +0000 | [diff] [blame] | 29 | : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false), |
| 30 | AddressTaken(false) { |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 31 | Insts.Parent = this; |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 32 | } |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 33 | |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 34 | MachineBasicBlock::~MachineBasicBlock() { |
| 35 | LeakDetector::removeGarbageObject(this); |
| 36 | } |
| 37 | |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 38 | raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) { |
Daniel Dunbar | 1cd1d98 | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 39 | MBB.print(OS); |
| 40 | return OS; |
| 41 | } |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 43 | /// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the |
| 44 | /// parent pointer of the MBB, the MBB numbering, and any instructions in the |
| 45 | /// MBB to be on the right operand list for registers. |
| 46 | /// |
| 47 | /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it |
| 48 | /// gets the next available unique MBB number. If it is removed from a |
| 49 | /// MachineFunction, it goes back to being #-1. |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 50 | void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 51 | MachineFunction &MF = *N->getParent(); |
| 52 | N->Number = MF.addToMBBNumbering(N); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 53 | |
| 54 | // Make sure the instructions have their operands in the reginfo lists. |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 55 | MachineRegisterInfo &RegInfo = MF.getRegInfo(); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 56 | for (MachineBasicBlock::iterator I = N->begin(), E = N->end(); I != E; ++I) |
| 57 | I->AddRegOperandsToUseLists(RegInfo); |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 58 | |
| 59 | LeakDetector::removeGarbageObject(N); |
Brian Gaeke | 0bcb1ad | 2004-05-12 21:35:22 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 62 | void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) { |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 63 | N->getParent()->removeFromMBBNumbering(N->Number); |
Brian Gaeke | 0bcb1ad | 2004-05-12 21:35:22 +0000 | [diff] [blame] | 64 | N->Number = -1; |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 65 | LeakDetector::addGarbageObject(N); |
Brian Gaeke | 0bcb1ad | 2004-05-12 21:35:22 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | 5e61fa9 | 2004-02-19 16:13:54 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 69 | /// addNodeToList (MI) - When we add an instruction to a basic block |
| 70 | /// list, we update its parent pointer and add its operands from reg use/def |
| 71 | /// lists if appropriate. |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 72 | void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) { |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 73 | assert(N->getParent() == 0 && "machine instruction already in a basic block"); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 74 | N->setParent(Parent); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 75 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 76 | // Add the instruction's register operands to their corresponding |
| 77 | // use/def lists. |
| 78 | MachineFunction *MF = Parent->getParent(); |
| 79 | N->AddRegOperandsToUseLists(MF->getRegInfo()); |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 80 | |
| 81 | LeakDetector::removeGarbageObject(N); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 84 | /// removeNodeFromList (MI) - When we remove an instruction from a basic block |
| 85 | /// list, we update its parent pointer and remove its operands from reg use/def |
| 86 | /// lists if appropriate. |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 87 | void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) { |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 88 | assert(N->getParent() != 0 && "machine instruction not in a basic block"); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 89 | |
| 90 | // Remove from the use/def lists. |
| 91 | N->RemoveRegOperandsFromUseLists(); |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 92 | |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 93 | N->setParent(0); |
Dan Gohman | 2c3f7ae | 2008-07-17 23:49:46 +0000 | [diff] [blame] | 94 | |
| 95 | LeakDetector::addGarbageObject(N); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 98 | /// transferNodesFromList (MI) - When moving a range of instructions from one |
| 99 | /// MBB list to another, we need to update the parent pointers and the use/def |
| 100 | /// lists. |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 101 | void ilist_traits<MachineInstr>:: |
| 102 | transferNodesFromList(ilist_traits<MachineInstr> &fromList, |
| 103 | MachineBasicBlock::iterator first, |
| 104 | MachineBasicBlock::iterator last) { |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 105 | assert(Parent->getParent() == fromList.Parent->getParent() && |
| 106 | "MachineInstr parent mismatch!"); |
| 107 | |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 108 | // Splice within the same MBB -> no change. |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 109 | if (Parent == fromList.Parent) return; |
Chris Lattner | 62ed6b9 | 2008-01-01 01:12:31 +0000 | [diff] [blame] | 110 | |
| 111 | // If splicing between two blocks within the same function, just update the |
| 112 | // parent pointers. |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 113 | for (; first != last; ++first) |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 114 | first->setParent(Parent); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Dan Gohman | fed90b6 | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 117 | void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 118 | assert(!MI->getParent() && "MI is still in a block!"); |
| 119 | Parent->getParent()->DeleteMachineInstr(MI); |
| 120 | } |
| 121 | |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 122 | MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { |
Alkis Evlogimenos | 743d0a1 | 2004-02-23 18:14:48 +0000 | [diff] [blame] | 123 | iterator I = end(); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 124 | while (I != begin() && (--I)->getDesc().isTerminator()) |
Anton Korobeynikov | 406452d | 2007-09-02 22:11:14 +0000 | [diff] [blame] | 125 | ; /*noop */ |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 126 | if (I != end() && !I->getDesc().isTerminator()) ++I; |
Alkis Evlogimenos | 743d0a1 | 2004-02-23 18:14:48 +0000 | [diff] [blame] | 127 | return I; |
| 128 | } |
| 129 | |
Chris Lattner | b49a30c | 2009-08-18 04:33:15 +0000 | [diff] [blame] | 130 | /// isOnlyReachableViaFallthough - Return true if this basic block has |
| 131 | /// exactly one predecessor and the control transfer mechanism between |
| 132 | /// the predecessor and this block is a fall-through. |
Chris Lattner | a006d4e | 2009-08-18 04:30:35 +0000 | [diff] [blame] | 133 | bool MachineBasicBlock::isOnlyReachableByFallthrough() const { |
| 134 | // If this is a landing pad, it isn't a fall through. If it has no preds, |
| 135 | // then nothing falls through to it. |
| 136 | if (isLandingPad() || pred_empty()) |
| 137 | return false; |
| 138 | |
| 139 | // If there isn't exactly one predecessor, it can't be a fall through. |
| 140 | const_pred_iterator PI = pred_begin(), PI2 = PI; |
Chris Lattner | 1f50fc7 | 2009-08-18 04:34:36 +0000 | [diff] [blame] | 141 | ++PI2; |
| 142 | if (PI2 != pred_end()) |
Chris Lattner | a006d4e | 2009-08-18 04:30:35 +0000 | [diff] [blame] | 143 | return false; |
| 144 | |
| 145 | // The predecessor has to be immediately before this block. |
| 146 | const MachineBasicBlock *Pred = *PI; |
| 147 | |
| 148 | if (!Pred->isLayoutSuccessor(this)) |
| 149 | return false; |
| 150 | |
| 151 | // If the block is completely empty, then it definitely does fall through. |
| 152 | if (Pred->empty()) |
| 153 | return true; |
| 154 | |
| 155 | // Otherwise, check the last instruction. |
| 156 | const MachineInstr &LastInst = Pred->back(); |
Chris Lattner | b49a30c | 2009-08-18 04:33:15 +0000 | [diff] [blame] | 157 | return !LastInst.getDesc().isBarrier(); |
Dan Gohman | 968dc7a | 2009-03-31 18:39:13 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 160 | void MachineBasicBlock::dump() const { |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 161 | print(errs()); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Daniel Dunbar | 1cd1d98 | 2009-07-24 10:36:58 +0000 | [diff] [blame] | 164 | static inline void OutputReg(raw_ostream &os, unsigned RegNo, |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 165 | const TargetRegisterInfo *TRI = 0) { |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 166 | if (RegNo != 0 && TargetRegisterInfo::isPhysicalRegister(RegNo)) { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 167 | if (TRI) |
Bill Wendling | e6d088a | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 168 | os << " %" << TRI->get(RegNo).Name; |
Evan Cheng | 13d8285 | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 169 | else |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 170 | os << " %physreg" << RegNo; |
Evan Cheng | 13d8285 | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 171 | } else |
| 172 | os << " %reg" << RegNo; |
| 173 | } |
| 174 | |
Jakob Stoklund Olesen | 324da76 | 2009-11-20 01:17:03 +0000 | [diff] [blame] | 175 | StringRef MachineBasicBlock::getName() const { |
| 176 | if (const BasicBlock *LBB = getBasicBlock()) |
| 177 | return LBB->getName(); |
| 178 | else |
| 179 | return "(null)"; |
| 180 | } |
| 181 | |
Chris Lattner | 2d8e3d2 | 2009-08-23 00:47:04 +0000 | [diff] [blame] | 182 | void MachineBasicBlock::print(raw_ostream &OS) const { |
Evan Cheng | 13d8285 | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 183 | const MachineFunction *MF = getParent(); |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 184 | if (!MF) { |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 185 | OS << "Can't print out MachineBasicBlock because parent MachineFunction" |
| 186 | << " is null\n"; |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 187 | return; |
| 188 | } |
Alkis Evlogimenos | a638286 | 2004-09-05 18:39:20 +0000 | [diff] [blame] | 189 | |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 190 | if (Alignment) { OS << "Alignment " << Alignment << "\n"; } |
| 191 | |
| 192 | OS << "BB#" << getNumber() << ": "; |
| 193 | |
| 194 | const char *Comma = ""; |
| 195 | if (const BasicBlock *LBB = getBasicBlock()) { |
| 196 | OS << Comma << "derived from LLVM BB "; |
| 197 | WriteAsOperand(OS, LBB, /*PrintType=*/false); |
| 198 | Comma = ", "; |
| 199 | } |
| 200 | if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; } |
| 201 | if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; } |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 202 | OS << '\n'; |
Evan Cheng | 13d8285 | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 203 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 204 | const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); |
Dan Gohman | cb406c2 | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 205 | if (!livein_empty()) { |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 206 | OS << " Live Ins:"; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 207 | for (const_livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I) |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 208 | OutputReg(OS, *I, TRI); |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 209 | OS << '\n'; |
Evan Cheng | 13d8285 | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 210 | } |
Chris Lattner | 681764b | 2006-09-26 03:41:59 +0000 | [diff] [blame] | 211 | // Print the preds of this block according to the CFG. |
| 212 | if (!pred_empty()) { |
| 213 | OS << " Predecessors according to CFG:"; |
| 214 | 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] | 215 | OS << " BB#" << (*PI)->getNumber(); |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 216 | OS << '\n'; |
Chris Lattner | 681764b | 2006-09-26 03:41:59 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Alkis Evlogimenos | a638286 | 2004-09-05 18:39:20 +0000 | [diff] [blame] | 219 | for (const_iterator I = begin(); I != end(); ++I) { |
Chris Lattner | 2d8e3d2 | 2009-08-23 00:47:04 +0000 | [diff] [blame] | 220 | OS << '\t'; |
Alkis Evlogimenos | a638286 | 2004-09-05 18:39:20 +0000 | [diff] [blame] | 221 | I->print(OS, &getParent()->getTarget()); |
| 222 | } |
Chris Lattner | 380ae49 | 2005-04-01 06:48:38 +0000 | [diff] [blame] | 223 | |
| 224 | // Print the successors of this block according to the CFG. |
| 225 | if (!succ_empty()) { |
| 226 | OS << " Successors according to CFG:"; |
| 227 | 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] | 228 | OS << " BB#" << (*SI)->getNumber(); |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 229 | OS << '\n'; |
Chris Lattner | 380ae49 | 2005-04-01 06:48:38 +0000 | [diff] [blame] | 230 | } |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 231 | } |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 232 | |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 233 | void MachineBasicBlock::removeLiveIn(unsigned Reg) { |
| 234 | livein_iterator I = std::find(livein_begin(), livein_end(), Reg); |
| 235 | assert(I != livein_end() && "Not a live in!"); |
| 236 | LiveIns.erase(I); |
| 237 | } |
| 238 | |
Evan Cheng | a971dbd | 2008-04-24 09:06:33 +0000 | [diff] [blame] | 239 | bool MachineBasicBlock::isLiveIn(unsigned Reg) const { |
| 240 | const_livein_iterator I = std::find(livein_begin(), livein_end(), Reg); |
| 241 | return I != livein_end(); |
| 242 | } |
| 243 | |
Chris Lattner | c585a3f | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 244 | void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 245 | getParent()->splice(NewAfter, this); |
Chris Lattner | c585a3f | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { |
Chris Lattner | c585a3f | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 249 | MachineFunction::iterator BBI = NewBefore; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 250 | getParent()->splice(++BBI, this); |
Chris Lattner | c585a3f | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 253 | void MachineBasicBlock::updateTerminator() { |
| 254 | const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo(); |
| 255 | // A block with no successors has no concerns with fall-through edges. |
| 256 | if (this->succ_empty()) return; |
| 257 | |
| 258 | MachineBasicBlock *TBB = 0, *FBB = 0; |
| 259 | SmallVector<MachineOperand, 4> Cond; |
| 260 | bool B = TII->AnalyzeBranch(*this, TBB, FBB, Cond); |
| 261 | (void) B; |
| 262 | assert(!B && "UpdateTerminators requires analyzable predecessors!"); |
| 263 | if (Cond.empty()) { |
| 264 | if (TBB) { |
| 265 | // The block has an unconditional branch. If its successor is now |
| 266 | // its layout successor, delete the branch. |
| 267 | if (isLayoutSuccessor(TBB)) |
| 268 | TII->RemoveBranch(*this); |
| 269 | } else { |
| 270 | // The block has an unconditional fallthrough. If its successor is not |
| 271 | // its layout successor, insert a branch. |
| 272 | TBB = *succ_begin(); |
| 273 | if (!isLayoutSuccessor(TBB)) |
| 274 | TII->InsertBranch(*this, TBB, 0, Cond); |
| 275 | } |
| 276 | } else { |
| 277 | if (FBB) { |
| 278 | // The block has a non-fallthrough conditional branch. If one of its |
| 279 | // successors is its layout successor, rewrite it to a fallthrough |
| 280 | // conditional branch. |
| 281 | if (isLayoutSuccessor(TBB)) { |
Jakob Stoklund Olesen | e023993 | 2009-11-22 18:28:04 +0000 | [diff] [blame^] | 282 | if (TII->ReverseBranchCondition(Cond)) |
| 283 | return; |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 284 | TII->RemoveBranch(*this); |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 285 | TII->InsertBranch(*this, FBB, 0, Cond); |
| 286 | } else if (isLayoutSuccessor(FBB)) { |
| 287 | TII->RemoveBranch(*this); |
| 288 | TII->InsertBranch(*this, TBB, 0, Cond); |
| 289 | } |
| 290 | } else { |
| 291 | // The block has a fallthrough conditional branch. |
| 292 | MachineBasicBlock *MBBA = *succ_begin(); |
| 293 | MachineBasicBlock *MBBB = *next(succ_begin()); |
| 294 | if (MBBA == TBB) std::swap(MBBB, MBBA); |
| 295 | if (isLayoutSuccessor(TBB)) { |
Jakob Stoklund Olesen | e023993 | 2009-11-22 18:28:04 +0000 | [diff] [blame^] | 296 | if (TII->ReverseBranchCondition(Cond)) { |
| 297 | // We can't reverse the condition, add an unconditional branch. |
| 298 | Cond.clear(); |
| 299 | TII->InsertBranch(*this, MBBA, 0, Cond); |
| 300 | return; |
| 301 | } |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 302 | TII->RemoveBranch(*this); |
Jim Grosbach | 7707a0d | 2009-11-12 03:55:33 +0000 | [diff] [blame] | 303 | TII->InsertBranch(*this, MBBA, 0, Cond); |
| 304 | } else if (!isLayoutSuccessor(MBBA)) { |
| 305 | TII->RemoveBranch(*this); |
| 306 | TII->InsertBranch(*this, TBB, MBBA, Cond); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | } |
Chris Lattner | c585a3f | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 312 | void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) { |
| 313 | Successors.push_back(succ); |
| 314 | succ->addPredecessor(this); |
| 315 | } |
| 316 | |
| 317 | void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { |
| 318 | succ->removePredecessor(this); |
| 319 | succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); |
| 320 | assert(I != Successors.end() && "Not a current successor!"); |
| 321 | Successors.erase(I); |
| 322 | } |
| 323 | |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 324 | MachineBasicBlock::succ_iterator |
| 325 | MachineBasicBlock::removeSuccessor(succ_iterator I) { |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 326 | assert(I != Successors.end() && "Not a current successor!"); |
| 327 | (*I)->removePredecessor(this); |
Dan Gohman | 5d5ee80 | 2009-01-08 22:19:34 +0000 | [diff] [blame] | 328 | return Successors.erase(I); |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { |
| 332 | Predecessors.push_back(pred); |
| 333 | } |
| 334 | |
| 335 | void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 336 | std::vector<MachineBasicBlock *>::iterator I = |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 337 | std::find(Predecessors.begin(), Predecessors.end(), pred); |
| 338 | assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); |
| 339 | Predecessors.erase(I); |
| 340 | } |
Evan Cheng | 4f09878 | 2007-05-17 23:58:53 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 342 | void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) { |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 343 | if (this == fromMBB) |
| 344 | return; |
| 345 | |
Chris Lattner | 6371ed5 | 2009-08-23 00:35:30 +0000 | [diff] [blame] | 346 | for (MachineBasicBlock::succ_iterator I = fromMBB->succ_begin(), |
| 347 | E = fromMBB->succ_end(); I != E; ++I) |
| 348 | addSuccessor(*I); |
| 349 | |
| 350 | while (!fromMBB->succ_empty()) |
Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 351 | fromMBB->removeSuccessor(fromMBB->succ_begin()); |
| 352 | } |
| 353 | |
Dan Gohman | 6d1b89e | 2009-03-30 20:06:29 +0000 | [diff] [blame] | 354 | bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const { |
Evan Cheng | 4f09878 | 2007-05-17 23:58:53 +0000 | [diff] [blame] | 355 | std::vector<MachineBasicBlock *>::const_iterator I = |
| 356 | std::find(Successors.begin(), Successors.end(), MBB); |
| 357 | return I != Successors.end(); |
| 358 | } |
Evan Cheng | 0370fad | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 359 | |
Dan Gohman | 6d1b89e | 2009-03-30 20:06:29 +0000 | [diff] [blame] | 360 | bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const { |
Dan Gohman | 6ade6f5 | 2008-10-02 22:09:09 +0000 | [diff] [blame] | 361 | MachineFunction::const_iterator I(this); |
| 362 | return next(I) == MachineFunction::const_iterator(MBB); |
| 363 | } |
| 364 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 365 | /// removeFromParent - This method unlinks 'this' from the containing function, |
| 366 | /// and returns it, but does not delete it. |
| 367 | MachineBasicBlock *MachineBasicBlock::removeFromParent() { |
| 368 | assert(getParent() && "Not embedded in a function!"); |
| 369 | getParent()->remove(this); |
| 370 | return this; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | /// eraseFromParent - This method unlinks 'this' from the containing function, |
| 375 | /// and deletes it. |
| 376 | void MachineBasicBlock::eraseFromParent() { |
| 377 | assert(getParent() && "Not embedded in a function!"); |
| 378 | getParent()->erase(this); |
| 379 | } |
| 380 | |
| 381 | |
Evan Cheng | 0370fad | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 382 | /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to |
| 383 | /// 'Old', change the code and CFG so that it branches to 'New' instead. |
| 384 | void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, |
| 385 | MachineBasicBlock *New) { |
| 386 | assert(Old != New && "Cannot replace self with self!"); |
| 387 | |
| 388 | MachineBasicBlock::iterator I = end(); |
| 389 | while (I != begin()) { |
| 390 | --I; |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 391 | if (!I->getDesc().isTerminator()) break; |
Evan Cheng | 0370fad | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 392 | |
| 393 | // Scan the operands of this machine instruction, replacing any uses of Old |
| 394 | // with New. |
| 395 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 396 | if (I->getOperand(i).isMBB() && |
Dan Gohman | 014278e | 2008-09-13 17:58:21 +0000 | [diff] [blame] | 397 | I->getOperand(i).getMBB() == Old) |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 398 | I->getOperand(i).setMBB(New); |
Evan Cheng | 0370fad | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Dan Gohman | 5412d06 | 2009-05-05 21:10:19 +0000 | [diff] [blame] | 401 | // Update the successor information. |
Evan Cheng | 0370fad | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 402 | removeSuccessor(Old); |
Dan Gohman | 5412d06 | 2009-05-05 21:10:19 +0000 | [diff] [blame] | 403 | addSuccessor(New); |
Evan Cheng | 0370fad | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 406 | /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the |
| 407 | /// CFG to be inserted. If we have proven that MBB can only branch to DestA and |
| 408 | /// DestB, remove any other MBB successors from the CFG. DestA and DestB can |
| 409 | /// be null. |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame] | 410 | /// Besides DestA and DestB, retain other edges leading to LandingPads |
| 411 | /// (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] | 412 | /// Note it is possible that DestA and/or DestB are LandingPads. |
| 413 | bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA, |
| 414 | MachineBasicBlock *DestB, |
| 415 | bool isCond) { |
| 416 | bool MadeChange = false; |
| 417 | bool AddedFallThrough = false; |
| 418 | |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 419 | MachineFunction::iterator FallThru = next(MachineFunction::iterator(this)); |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 420 | |
| 421 | // If this block ends with a conditional branch that falls through to its |
| 422 | // successor, set DestB as the successor. |
| 423 | if (isCond) { |
| 424 | if (DestB == 0 && FallThru != getParent()->end()) { |
| 425 | DestB = FallThru; |
| 426 | AddedFallThrough = true; |
| 427 | } |
| 428 | } else { |
| 429 | // If this is an unconditional branch with no explicit dest, it must just be |
| 430 | // a fallthrough into DestB. |
| 431 | if (DestA == 0 && FallThru != getParent()->end()) { |
| 432 | DestA = FallThru; |
| 433 | AddedFallThrough = true; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | MachineBasicBlock::succ_iterator SI = succ_begin(); |
| 438 | MachineBasicBlock *OrigDestA = DestA, *OrigDestB = DestB; |
| 439 | while (SI != succ_end()) { |
Dan Gohman | 2210c0b | 2009-11-11 19:48:59 +0000 | [diff] [blame] | 440 | if (*SI == DestA) { |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 441 | DestA = 0; |
| 442 | ++SI; |
| 443 | } else if (*SI == DestB) { |
| 444 | DestB = 0; |
| 445 | ++SI; |
| 446 | } else if ((*SI)->isLandingPad() && |
| 447 | *SI!=OrigDestA && *SI!=OrigDestB) { |
| 448 | ++SI; |
| 449 | } else { |
| 450 | // Otherwise, this is a superfluous edge, remove it. |
David Greene | 8a46d34 | 2007-06-29 02:45:24 +0000 | [diff] [blame] | 451 | SI = removeSuccessor(SI); |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 452 | MadeChange = true; |
| 453 | } |
| 454 | } |
| 455 | if (!AddedFallThrough) { |
| 456 | assert(DestA == 0 && DestB == 0 && |
| 457 | "MachineCFG is missing edges!"); |
| 458 | } else if (isCond) { |
| 459 | assert(DestA == 0 && "MachineCFG is missing edges!"); |
| 460 | } |
| 461 | return MadeChange; |
| 462 | } |
Evan Cheng | 2a085c3 | 2009-11-17 19:19:59 +0000 | [diff] [blame] | 463 | |
| 464 | void llvm::WriteAsOperand(raw_ostream &OS, const MachineBasicBlock *MBB, |
| 465 | bool t) { |
| 466 | OS << "BB#" << MBB->getNumber(); |
| 467 | } |