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" |
Evan Cheng | 13d8285 | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 17 | #include "llvm/Target/MRegisterInfo.h" |
Owen Anderson | 07000c6 | 2006-05-12 06:33:49 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetData.h" |
Alkis Evlogimenos | 743d0a1 | 2004-02-23 18:14:48 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetInstrInfo.h" |
| 20 | #include "llvm/Target/TargetMachine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 25 | MachineBasicBlock::~MachineBasicBlock() { |
| 26 | LeakDetector::removeGarbageObject(this); |
| 27 | } |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 1ccc468 | 2006-11-18 21:47:36 +0000 | [diff] [blame] | 29 | std::ostream& llvm::operator<<(std::ostream &OS, const MachineBasicBlock &MBB) { |
| 30 | MBB.print(OS); |
| 31 | return OS; |
| 32 | } |
Tanya Lattner | 17fb34b | 2004-05-24 07:14:35 +0000 | [diff] [blame] | 33 | |
Alkis Evlogimenos | a638286 | 2004-09-05 18:39:20 +0000 | [diff] [blame] | 34 | // MBBs start out as #-1. When a MBB is added to a MachineFunction, it |
Brian Gaeke | 0bcb1ad | 2004-05-12 21:35:22 +0000 | [diff] [blame] | 35 | // gets the next available unique MBB number. If it is removed from a |
| 36 | // MachineFunction, it goes back to being #-1. |
Chris Lattner | ca48eb9 | 2004-07-01 06:02:27 +0000 | [diff] [blame] | 37 | void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) { |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame^] | 38 | assert(N->getParent() == 0 && "machine instruction already in a basic block"); |
| 39 | N->setParent(Parent); |
Chris Lattner | ca48eb9 | 2004-07-01 06:02:27 +0000 | [diff] [blame] | 40 | N->Number = Parent->addToMBBNumbering(N); |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 41 | LeakDetector::removeGarbageObject(N); |
Brian Gaeke | 0bcb1ad | 2004-05-12 21:35:22 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Chris Lattner | ca48eb9 | 2004-07-01 06:02:27 +0000 | [diff] [blame] | 44 | void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) { |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame^] | 45 | assert(N->getParent() != 0 && "machine instruction not in a basic block"); |
| 46 | N->getParent()->removeFromMBBNumbering(N->Number); |
Brian Gaeke | 0bcb1ad | 2004-05-12 21:35:22 +0000 | [diff] [blame] | 47 | N->Number = -1; |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame^] | 48 | N->setParent(0); |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 49 | LeakDetector::addGarbageObject(N); |
Brian Gaeke | 0bcb1ad | 2004-05-12 21:35:22 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Chris Lattner | 5e61fa9 | 2004-02-19 16:13:54 +0000 | [diff] [blame] | 52 | |
Chris Lattner | bca8144 | 2005-01-30 00:09:23 +0000 | [diff] [blame] | 53 | MachineInstr* ilist_traits<MachineInstr>::createSentinel() { |
Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 54 | MachineInstr* dummy = new MachineInstr(); |
Alkis Evlogimenos | a638286 | 2004-09-05 18:39:20 +0000 | [diff] [blame] | 55 | LeakDetector::removeGarbageObject(dummy); |
| 56 | return dummy; |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 59 | void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) { |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame^] | 60 | assert(N->getParent() == 0 && "machine instruction already in a basic block"); |
| 61 | N->setParent(parent); |
Alkis Evlogimenos | a638286 | 2004-09-05 18:39:20 +0000 | [diff] [blame] | 62 | LeakDetector::removeGarbageObject(N); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 65 | void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) { |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame^] | 66 | assert(N->getParent() != 0 && "machine instruction not in a basic block"); |
| 67 | N->setParent(0); |
Alkis Evlogimenos | a638286 | 2004-09-05 18:39:20 +0000 | [diff] [blame] | 68 | LeakDetector::addGarbageObject(N); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void ilist_traits<MachineInstr>::transferNodesFromList( |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame^] | 72 | iplist<MachineInstr, ilist_traits<MachineInstr> >& fromList, |
| 73 | ilist_iterator<MachineInstr> first, |
| 74 | ilist_iterator<MachineInstr> last) { |
| 75 | // Splice within the same MBB -> no change. |
| 76 | if (parent == fromList.parent) return; |
| 77 | |
| 78 | for (; first != last; ++first) |
| 79 | first->setParent(parent); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 82 | MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { |
Chris Lattner | 9bcdcd1 | 2004-06-02 05:57:12 +0000 | [diff] [blame] | 83 | const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo(); |
Alkis Evlogimenos | 743d0a1 | 2004-02-23 18:14:48 +0000 | [diff] [blame] | 84 | iterator I = end(); |
Anton Korobeynikov | 406452d | 2007-09-02 22:11:14 +0000 | [diff] [blame] | 85 | while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode())) |
| 86 | ; /*noop */ |
Alkis Evlogimenos | 743d0a1 | 2004-02-23 18:14:48 +0000 | [diff] [blame] | 87 | if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I; |
| 88 | return I; |
| 89 | } |
| 90 | |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 91 | void MachineBasicBlock::dump() const { |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 92 | print(*cerr.stream()); |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Evan Cheng | 13d8285 | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 95 | static inline void OutputReg(std::ostream &os, unsigned RegNo, |
| 96 | const MRegisterInfo *MRI = 0) { |
| 97 | if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) { |
| 98 | if (MRI) |
| 99 | os << " %" << MRI->get(RegNo).Name; |
| 100 | else |
| 101 | os << " %mreg(" << RegNo << ")"; |
| 102 | } else |
| 103 | os << " %reg" << RegNo; |
| 104 | } |
| 105 | |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 106 | void MachineBasicBlock::print(std::ostream &OS) const { |
Evan Cheng | 13d8285 | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 107 | const MachineFunction *MF = getParent(); |
| 108 | if(!MF) { |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 109 | OS << "Can't print out MachineBasicBlock because parent MachineFunction" |
| 110 | << " is null\n"; |
Tanya Lattner | 792699c | 2004-05-24 06:11:51 +0000 | [diff] [blame] | 111 | return; |
| 112 | } |
Alkis Evlogimenos | a638286 | 2004-09-05 18:39:20 +0000 | [diff] [blame] | 113 | |
| 114 | const BasicBlock *LBB = getBasicBlock(); |
Chris Lattner | db3ea67 | 2006-10-06 21:28:17 +0000 | [diff] [blame] | 115 | OS << "\n"; |
Chris Lattner | c11ce86 | 2007-04-30 23:12:53 +0000 | [diff] [blame] | 116 | if (LBB) OS << LBB->getName() << ": "; |
| 117 | OS << (const void*)this |
| 118 | << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber(); |
| 119 | if (isLandingPad()) OS << ", EH LANDING PAD"; |
| 120 | OS << ":\n"; |
Evan Cheng | 13d8285 | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 121 | |
| 122 | const MRegisterInfo *MRI = MF->getTarget().getRegisterInfo(); |
Dan Gohman | cb406c2 | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 123 | if (!livein_empty()) { |
Evan Cheng | 13d8285 | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 124 | OS << "Live Ins:"; |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 125 | for (const_livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I) |
Evan Cheng | 13d8285 | 2007-02-10 02:38:19 +0000 | [diff] [blame] | 126 | OutputReg(OS, *I, MRI); |
| 127 | OS << "\n"; |
| 128 | } |
Chris Lattner | 681764b | 2006-09-26 03:41:59 +0000 | [diff] [blame] | 129 | // Print the preds of this block according to the CFG. |
| 130 | if (!pred_empty()) { |
| 131 | OS << " Predecessors according to CFG:"; |
| 132 | for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) |
Evan Cheng | 2197051 | 2007-03-09 08:29:08 +0000 | [diff] [blame] | 133 | OS << " " << *PI << " (#" << (*PI)->getNumber() << ")"; |
Chris Lattner | 681764b | 2006-09-26 03:41:59 +0000 | [diff] [blame] | 134 | OS << "\n"; |
| 135 | } |
| 136 | |
Alkis Evlogimenos | a638286 | 2004-09-05 18:39:20 +0000 | [diff] [blame] | 137 | for (const_iterator I = begin(); I != end(); ++I) { |
| 138 | OS << "\t"; |
| 139 | I->print(OS, &getParent()->getTarget()); |
| 140 | } |
Chris Lattner | 380ae49 | 2005-04-01 06:48:38 +0000 | [diff] [blame] | 141 | |
| 142 | // Print the successors of this block according to the CFG. |
| 143 | if (!succ_empty()) { |
| 144 | OS << " Successors according to CFG:"; |
| 145 | for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) |
Evan Cheng | 2197051 | 2007-03-09 08:29:08 +0000 | [diff] [blame] | 146 | OS << " " << *SI << " (#" << (*SI)->getNumber() << ")"; |
Chris Lattner | 380ae49 | 2005-04-01 06:48:38 +0000 | [diff] [blame] | 147 | OS << "\n"; |
| 148 | } |
Alkis Evlogimenos | aad5c05 | 2004-02-16 07:17:43 +0000 | [diff] [blame] | 149 | } |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 150 | |
Evan Cheng | b371f45 | 2007-02-19 21:49:54 +0000 | [diff] [blame] | 151 | void MachineBasicBlock::removeLiveIn(unsigned Reg) { |
| 152 | livein_iterator I = std::find(livein_begin(), livein_end(), Reg); |
| 153 | assert(I != livein_end() && "Not a live in!"); |
| 154 | LiveIns.erase(I); |
| 155 | } |
| 156 | |
Chris Lattner | c585a3f | 2006-10-24 00:02:26 +0000 | [diff] [blame] | 157 | void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { |
| 158 | MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList(); |
| 159 | getParent()->getBasicBlockList().splice(NewAfter, BBList, this); |
| 160 | } |
| 161 | |
| 162 | void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { |
| 163 | MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList(); |
| 164 | MachineFunction::iterator BBI = NewBefore; |
| 165 | getParent()->getBasicBlockList().splice(++BBI, BBList, this); |
| 166 | } |
| 167 | |
| 168 | |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 169 | void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) { |
| 170 | Successors.push_back(succ); |
| 171 | succ->addPredecessor(this); |
| 172 | } |
| 173 | |
| 174 | void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { |
| 175 | succ->removePredecessor(this); |
| 176 | succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); |
| 177 | assert(I != Successors.end() && "Not a current successor!"); |
| 178 | Successors.erase(I); |
| 179 | } |
| 180 | |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame^] | 181 | MachineBasicBlock::succ_iterator |
| 182 | MachineBasicBlock::removeSuccessor(succ_iterator I) { |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 183 | assert(I != Successors.end() && "Not a current successor!"); |
| 184 | (*I)->removePredecessor(this); |
David Greene | 8a46d34 | 2007-06-29 02:45:24 +0000 | [diff] [blame] | 185 | return(Successors.erase(I)); |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { |
| 189 | Predecessors.push_back(pred); |
| 190 | } |
| 191 | |
| 192 | void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 193 | std::vector<MachineBasicBlock *>::iterator I = |
Chris Lattner | 52c09d7 | 2004-10-26 15:43:42 +0000 | [diff] [blame] | 194 | std::find(Predecessors.begin(), Predecessors.end(), pred); |
| 195 | assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); |
| 196 | Predecessors.erase(I); |
| 197 | } |
Evan Cheng | 4f09878 | 2007-05-17 23:58:53 +0000 | [diff] [blame] | 198 | |
| 199 | bool MachineBasicBlock::isSuccessor(MachineBasicBlock *MBB) const { |
| 200 | std::vector<MachineBasicBlock *>::const_iterator I = |
| 201 | std::find(Successors.begin(), Successors.end(), MBB); |
| 202 | return I != Successors.end(); |
| 203 | } |
Evan Cheng | 0370fad | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 204 | |
| 205 | /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to |
| 206 | /// 'Old', change the code and CFG so that it branches to 'New' instead. |
| 207 | void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, |
| 208 | MachineBasicBlock *New) { |
| 209 | assert(Old != New && "Cannot replace self with self!"); |
| 210 | |
| 211 | MachineBasicBlock::iterator I = end(); |
| 212 | while (I != begin()) { |
| 213 | --I; |
| 214 | if (!(I->getInstrDescriptor()->Flags & M_TERMINATOR_FLAG)) break; |
| 215 | |
| 216 | // Scan the operands of this machine instruction, replacing any uses of Old |
| 217 | // with New. |
| 218 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 219 | if (I->getOperand(i).isMBB() && I->getOperand(i).getMBB() == Old) |
| 220 | I->getOperand(i).setMBB(New); |
Evan Cheng | 0370fad | 2007-06-04 06:44:01 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // Update the successor information. If New was already a successor, just |
| 224 | // remove the link to Old instead of creating another one. PR 1444. |
| 225 | removeSuccessor(Old); |
| 226 | if (!isSuccessor(New)) |
| 227 | addSuccessor(New); |
| 228 | } |
| 229 | |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 230 | /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the |
| 231 | /// CFG to be inserted. If we have proven that MBB can only branch to DestA and |
| 232 | /// DestB, remove any other MBB successors from the CFG. DestA and DestB can |
| 233 | /// be null. |
Chris Lattner | f20c1a4 | 2007-12-31 04:56:33 +0000 | [diff] [blame^] | 234 | /// Besides DestA and DestB, retain other edges leading to LandingPads |
| 235 | /// (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] | 236 | /// Note it is possible that DestA and/or DestB are LandingPads. |
| 237 | bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA, |
| 238 | MachineBasicBlock *DestB, |
| 239 | bool isCond) { |
| 240 | bool MadeChange = false; |
| 241 | bool AddedFallThrough = false; |
| 242 | |
| 243 | MachineBasicBlock *FallThru = getNext(); |
| 244 | |
| 245 | // If this block ends with a conditional branch that falls through to its |
| 246 | // successor, set DestB as the successor. |
| 247 | if (isCond) { |
| 248 | if (DestB == 0 && FallThru != getParent()->end()) { |
| 249 | DestB = FallThru; |
| 250 | AddedFallThrough = true; |
| 251 | } |
| 252 | } else { |
| 253 | // If this is an unconditional branch with no explicit dest, it must just be |
| 254 | // a fallthrough into DestB. |
| 255 | if (DestA == 0 && FallThru != getParent()->end()) { |
| 256 | DestA = FallThru; |
| 257 | AddedFallThrough = true; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | MachineBasicBlock::succ_iterator SI = succ_begin(); |
| 262 | MachineBasicBlock *OrigDestA = DestA, *OrigDestB = DestB; |
| 263 | while (SI != succ_end()) { |
| 264 | if (*SI == DestA && DestA == DestB) { |
| 265 | DestA = DestB = 0; |
| 266 | ++SI; |
| 267 | } else if (*SI == DestA) { |
| 268 | DestA = 0; |
| 269 | ++SI; |
| 270 | } else if (*SI == DestB) { |
| 271 | DestB = 0; |
| 272 | ++SI; |
| 273 | } else if ((*SI)->isLandingPad() && |
| 274 | *SI!=OrigDestA && *SI!=OrigDestB) { |
| 275 | ++SI; |
| 276 | } else { |
| 277 | // Otherwise, this is a superfluous edge, remove it. |
David Greene | 8a46d34 | 2007-06-29 02:45:24 +0000 | [diff] [blame] | 278 | SI = removeSuccessor(SI); |
Evan Cheng | 2bdb7d0 | 2007-06-18 22:43:58 +0000 | [diff] [blame] | 279 | MadeChange = true; |
| 280 | } |
| 281 | } |
| 282 | if (!AddedFallThrough) { |
| 283 | assert(DestA == 0 && DestB == 0 && |
| 284 | "MachineCFG is missing edges!"); |
| 285 | } else if (isCond) { |
| 286 | assert(DestA == 0 && "MachineCFG is missing edges!"); |
| 287 | } |
| 288 | return MadeChange; |
| 289 | } |