LLVM API Documentation
00001 //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Collect the sequence of machine instructions for a basic block. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/CodeGen/MachineBasicBlock.h" 00015 #include "llvm/BasicBlock.h" 00016 #include "llvm/CodeGen/MachineFunction.h" 00017 #include "llvm/Target/TargetRegisterInfo.h" 00018 #include "llvm/Target/TargetData.h" 00019 #include "llvm/Target/TargetInstrDesc.h" 00020 #include "llvm/Target/TargetMachine.h" 00021 #include "llvm/Support/LeakDetector.h" 00022 #include <algorithm> 00023 using namespace llvm; 00024 00025 MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb) 00026 : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false) { 00027 Insts.Parent = this; 00028 } 00029 00030 MachineBasicBlock::~MachineBasicBlock() { 00031 LeakDetector::removeGarbageObject(this); 00032 } 00033 00034 std::ostream& llvm::operator<<(std::ostream &OS, const MachineBasicBlock &MBB) { 00035 MBB.print(OS); 00036 return OS; 00037 } 00038 00039 /// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the 00040 /// parent pointer of the MBB, the MBB numbering, and any instructions in the 00041 /// MBB to be on the right operand list for registers. 00042 /// 00043 /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it 00044 /// gets the next available unique MBB number. If it is removed from a 00045 /// MachineFunction, it goes back to being #-1. 00046 void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) { 00047 MachineFunction &MF = *N->getParent(); 00048 N->Number = MF.addToMBBNumbering(N); 00049 00050 // Make sure the instructions have their operands in the reginfo lists. 00051 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 00052 for (MachineBasicBlock::iterator I = N->begin(), E = N->end(); I != E; ++I) 00053 I->AddRegOperandsToUseLists(RegInfo); 00054 00055 LeakDetector::removeGarbageObject(N); 00056 } 00057 00058 void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) { 00059 N->getParent()->removeFromMBBNumbering(N->Number); 00060 N->Number = -1; 00061 LeakDetector::addGarbageObject(N); 00062 } 00063 00064 00065 /// addNodeToList (MI) - When we add an instruction to a basic block 00066 /// list, we update its parent pointer and add its operands from reg use/def 00067 /// lists if appropriate. 00068 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) { 00069 assert(N->getParent() == 0 && "machine instruction already in a basic block"); 00070 N->setParent(Parent); 00071 00072 // Add the instruction's register operands to their corresponding 00073 // use/def lists. 00074 MachineFunction *MF = Parent->getParent(); 00075 N->AddRegOperandsToUseLists(MF->getRegInfo()); 00076 00077 LeakDetector::removeGarbageObject(N); 00078 } 00079 00080 /// removeNodeFromList (MI) - When we remove an instruction from a basic block 00081 /// list, we update its parent pointer and remove its operands from reg use/def 00082 /// lists if appropriate. 00083 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) { 00084 assert(N->getParent() != 0 && "machine instruction not in a basic block"); 00085 00086 // Remove from the use/def lists. 00087 N->RemoveRegOperandsFromUseLists(); 00088 00089 N->setParent(0); 00090 00091 LeakDetector::addGarbageObject(N); 00092 } 00093 00094 /// transferNodesFromList (MI) - When moving a range of instructions from one 00095 /// MBB list to another, we need to update the parent pointers and the use/def 00096 /// lists. 00097 void ilist_traits<MachineInstr>::transferNodesFromList( 00098 ilist_traits<MachineInstr>& fromList, 00099 MachineBasicBlock::iterator first, 00100 MachineBasicBlock::iterator last) { 00101 assert(Parent->getParent() == fromList.Parent->getParent() && 00102 "MachineInstr parent mismatch!"); 00103 00104 // Splice within the same MBB -> no change. 00105 if (Parent == fromList.Parent) return; 00106 00107 // If splicing between two blocks within the same function, just update the 00108 // parent pointers. 00109 for (; first != last; ++first) 00110 first->setParent(Parent); 00111 } 00112 00113 void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) { 00114 assert(!MI->getParent() && "MI is still in a block!"); 00115 Parent->getParent()->DeleteMachineInstr(MI); 00116 } 00117 00118 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 00119 iterator I = end(); 00120 while (I != begin() && (--I)->getDesc().isTerminator()) 00121 ; /*noop */ 00122 if (I != end() && !I->getDesc().isTerminator()) ++I; 00123 return I; 00124 } 00125 00126 void MachineBasicBlock::dump() const { 00127 print(*cerr.stream()); 00128 } 00129 00130 static inline void OutputReg(std::ostream &os, unsigned RegNo, 00131 const TargetRegisterInfo *TRI = 0) { 00132 if (!RegNo || TargetRegisterInfo::isPhysicalRegister(RegNo)) { 00133 if (TRI) 00134 os << " %" << TRI->get(RegNo).Name; 00135 else 00136 os << " %mreg(" << RegNo << ")"; 00137 } else 00138 os << " %reg" << RegNo; 00139 } 00140 00141 void MachineBasicBlock::print(std::ostream &OS) const { 00142 const MachineFunction *MF = getParent(); 00143 if(!MF) { 00144 OS << "Can't print out MachineBasicBlock because parent MachineFunction" 00145 << " is null\n"; 00146 return; 00147 } 00148 00149 const BasicBlock *LBB = getBasicBlock(); 00150 OS << "\n"; 00151 if (LBB) OS << LBB->getName() << ": "; 00152 OS << (const void*)this 00153 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber(); 00154 if (Alignment) OS << ", Alignment " << Alignment; 00155 if (isLandingPad()) OS << ", EH LANDING PAD"; 00156 OS << ":\n"; 00157 00158 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 00159 if (!livein_empty()) { 00160 OS << "Live Ins:"; 00161 for (const_livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I) 00162 OutputReg(OS, *I, TRI); 00163 OS << "\n"; 00164 } 00165 // Print the preds of this block according to the CFG. 00166 if (!pred_empty()) { 00167 OS << " Predecessors according to CFG:"; 00168 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) 00169 OS << " " << *PI << " (#" << (*PI)->getNumber() << ")"; 00170 OS << "\n"; 00171 } 00172 00173 for (const_iterator I = begin(); I != end(); ++I) { 00174 OS << "\t"; 00175 I->print(OS, &getParent()->getTarget()); 00176 } 00177 00178 // Print the successors of this block according to the CFG. 00179 if (!succ_empty()) { 00180 OS << " Successors according to CFG:"; 00181 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) 00182 OS << " " << *SI << " (#" << (*SI)->getNumber() << ")"; 00183 OS << "\n"; 00184 } 00185 } 00186 00187 void MachineBasicBlock::removeLiveIn(unsigned Reg) { 00188 livein_iterator I = std::find(livein_begin(), livein_end(), Reg); 00189 assert(I != livein_end() && "Not a live in!"); 00190 LiveIns.erase(I); 00191 } 00192 00193 bool MachineBasicBlock::isLiveIn(unsigned Reg) const { 00194 const_livein_iterator I = std::find(livein_begin(), livein_end(), Reg); 00195 return I != livein_end(); 00196 } 00197 00198 void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { 00199 getParent()->splice(NewAfter, this); 00200 } 00201 00202 void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { 00203 MachineFunction::iterator BBI = NewBefore; 00204 getParent()->splice(++BBI, this); 00205 } 00206 00207 00208 void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) { 00209 Successors.push_back(succ); 00210 succ->addPredecessor(this); 00211 } 00212 00213 void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { 00214 succ->removePredecessor(this); 00215 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 00216 assert(I != Successors.end() && "Not a current successor!"); 00217 Successors.erase(I); 00218 } 00219 00220 MachineBasicBlock::succ_iterator 00221 MachineBasicBlock::removeSuccessor(succ_iterator I) { 00222 assert(I != Successors.end() && "Not a current successor!"); 00223 (*I)->removePredecessor(this); 00224 return Successors.erase(I); 00225 } 00226 00227 void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { 00228 Predecessors.push_back(pred); 00229 } 00230 00231 void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { 00232 std::vector<MachineBasicBlock *>::iterator I = 00233 std::find(Predecessors.begin(), Predecessors.end(), pred); 00234 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 00235 Predecessors.erase(I); 00236 } 00237 00238 void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) 00239 { 00240 if (this == fromMBB) 00241 return; 00242 00243 for(MachineBasicBlock::succ_iterator iter = fromMBB->succ_begin(), 00244 end = fromMBB->succ_end(); iter != end; ++iter) { 00245 addSuccessor(*iter); 00246 } 00247 while(!fromMBB->succ_empty()) 00248 fromMBB->removeSuccessor(fromMBB->succ_begin()); 00249 } 00250 00251 bool MachineBasicBlock::isSuccessor(MachineBasicBlock *MBB) const { 00252 std::vector<MachineBasicBlock *>::const_iterator I = 00253 std::find(Successors.begin(), Successors.end(), MBB); 00254 return I != Successors.end(); 00255 } 00256 00257 bool MachineBasicBlock::isLayoutSuccessor(MachineBasicBlock *MBB) const { 00258 MachineFunction::const_iterator I(this); 00259 return next(I) == MachineFunction::const_iterator(MBB); 00260 } 00261 00262 /// removeFromParent - This method unlinks 'this' from the containing function, 00263 /// and returns it, but does not delete it. 00264 MachineBasicBlock *MachineBasicBlock::removeFromParent() { 00265 assert(getParent() && "Not embedded in a function!"); 00266 getParent()->remove(this); 00267 return this; 00268 } 00269 00270 00271 /// eraseFromParent - This method unlinks 'this' from the containing function, 00272 /// and deletes it. 00273 void MachineBasicBlock::eraseFromParent() { 00274 assert(getParent() && "Not embedded in a function!"); 00275 getParent()->erase(this); 00276 } 00277 00278 00279 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to 00280 /// 'Old', change the code and CFG so that it branches to 'New' instead. 00281 void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, 00282 MachineBasicBlock *New) { 00283 assert(Old != New && "Cannot replace self with self!"); 00284 00285 MachineBasicBlock::iterator I = end(); 00286 while (I != begin()) { 00287 --I; 00288 if (!I->getDesc().isTerminator()) break; 00289 00290 // Scan the operands of this machine instruction, replacing any uses of Old 00291 // with New. 00292 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 00293 if (I->getOperand(i).isMBB() && 00294 I->getOperand(i).getMBB() == Old) 00295 I->getOperand(i).setMBB(New); 00296 } 00297 00298 // Update the successor information. If New was already a successor, just 00299 // remove the link to Old instead of creating another one. PR 1444. 00300 removeSuccessor(Old); 00301 if (!isSuccessor(New)) 00302 addSuccessor(New); 00303 } 00304 00305 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the 00306 /// CFG to be inserted. If we have proven that MBB can only branch to DestA and 00307 /// DestB, remove any other MBB successors from the CFG. DestA and DestB can 00308 /// be null. 00309 /// Besides DestA and DestB, retain other edges leading to LandingPads 00310 /// (currently there can be only one; we don't check or require that here). 00311 /// Note it is possible that DestA and/or DestB are LandingPads. 00312 bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA, 00313 MachineBasicBlock *DestB, 00314 bool isCond) { 00315 bool MadeChange = false; 00316 bool AddedFallThrough = false; 00317 00318 MachineFunction::iterator FallThru = next(MachineFunction::iterator(this)); 00319 00320 // If this block ends with a conditional branch that falls through to its 00321 // successor, set DestB as the successor. 00322 if (isCond) { 00323 if (DestB == 0 && FallThru != getParent()->end()) { 00324 DestB = FallThru; 00325 AddedFallThrough = true; 00326 } 00327 } else { 00328 // If this is an unconditional branch with no explicit dest, it must just be 00329 // a fallthrough into DestB. 00330 if (DestA == 0 && FallThru != getParent()->end()) { 00331 DestA = FallThru; 00332 AddedFallThrough = true; 00333 } 00334 } 00335 00336 MachineBasicBlock::succ_iterator SI = succ_begin(); 00337 MachineBasicBlock *OrigDestA = DestA, *OrigDestB = DestB; 00338 while (SI != succ_end()) { 00339 if (*SI == DestA && DestA == DestB) { 00340 DestA = DestB = 0; 00341 ++SI; 00342 } else if (*SI == DestA) { 00343 DestA = 0; 00344 ++SI; 00345 } else if (*SI == DestB) { 00346 DestB = 0; 00347 ++SI; 00348 } else if ((*SI)->isLandingPad() && 00349 *SI!=OrigDestA && *SI!=OrigDestB) { 00350 ++SI; 00351 } else { 00352 // Otherwise, this is a superfluous edge, remove it. 00353 SI = removeSuccessor(SI); 00354 MadeChange = true; 00355 } 00356 } 00357 if (!AddedFallThrough) { 00358 assert(DestA == 0 && DestB == 0 && 00359 "MachineCFG is missing edges!"); 00360 } else if (isCond) { 00361 assert(DestA == 0 && "MachineCFG is missing edges!"); 00362 } 00363 return MadeChange; 00364 }