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