1 //===-- MachineFunction.cpp -----------------------------------------------===// 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 native machine code information for a function. This allows 11 // target-specific information about the generated code to be stored with each 12 // function. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/CodeGen/MachineFunctionPass.h" 17 #include "llvm/CodeGen/MachineInstr.h" 18 #include "llvm/CodeGen/SSARegMap.h" 19 #include "llvm/CodeGen/MachineFrameInfo.h" 20 #include "llvm/CodeGen/MachineConstantPool.h" 21 #include "llvm/CodeGen/MachineJumpTableInfo.h" 22 #include "llvm/CodeGen/Passes.h" 23 #include "llvm/Target/TargetData.h" 24 #include "llvm/Target/TargetMachine.h" 25 #include "llvm/Target/TargetFrameInfo.h" 26 #include "llvm/Function.h" 27 #include "llvm/Instructions.h" 28 #include "llvm/Support/Compiler.h" 29 #include "llvm/Support/GraphWriter.h" 30 #include "llvm/Support/LeakDetector.h" 31 #include "llvm/ADT/STLExtras.h" 32 #include "llvm/Config/config.h" 33 #include <fstream> 34 #include <iostream> 35 #include <sstream> 36 37 using namespace llvm; 38 39 static AnnotationID MF_AID( 40 AnnotationManager::getID("CodeGen::MachineCodeForFunction")); 41 42 // Out of line virtual function to home classes. 43 void MachineFunctionPass::virtfn() {} 44 45 namespace { 46 struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass { 47 std::ostream *OS; 48 const std::string Banner; 49 50 Printer (std::ostream *_OS, const std::string &_Banner) : 51 OS (_OS), Banner (_Banner) { } 52 53 const char *getPassName() const { return "MachineFunction Printer"; } 54 55 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 56 AU.setPreservesAll(); 57 } 58 59 bool runOnMachineFunction(MachineFunction &MF) { 60 (*OS) << Banner; 61 MF.print (*OS); 62 return false; 63 } 64 }; 65 } 66 67 /// Returns a newly-created MachineFunction Printer pass. The default output 68 /// stream is std::cerr; the default banner is empty. 69 /// 70 FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS, 71 const std::string &Banner){ 72 return new Printer(OS, Banner); 73 } 74 75 namespace { 76 struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass { 77 const char *getPassName() const { return "Machine Code Deleter"; } 78 79 bool runOnMachineFunction(MachineFunction &MF) { 80 // Delete the annotation from the function now. 81 MachineFunction::destruct(MF.getFunction()); 82 return true; 83 } 84 }; 85 } 86 87 /// MachineCodeDeletion Pass - This pass deletes all of the machine code for 88 /// the current function, which should happen after the function has been 89 /// emitted to a .s file or to memory. 90 FunctionPass *llvm::createMachineCodeDeleter() { 91 return new Deleter(); 92 } 93 94 95 96 //===---------------------------------------------------------------------===// 97 // MachineFunction implementation 98 //===---------------------------------------------------------------------===// 99 100 MachineBasicBlock* ilist_traits<MachineBasicBlock>::createSentinel() { 101 MachineBasicBlock* dummy = new MachineBasicBlock(); 102 LeakDetector::removeGarbageObject(dummy); 103 return dummy; 104 } 105 106 void ilist_traits<MachineBasicBlock>::transferNodesFromList( 107 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList, 108 ilist_iterator<MachineBasicBlock> first, 109 ilist_iterator<MachineBasicBlock> last) { 110 if (Parent != toList.Parent) 111 for (; first != last; ++first) 112 first->Parent = toList.Parent; 113 } 114 115 MachineFunction::MachineFunction(const Function *F, 116 const TargetMachine &TM) 117 : Annotation(MF_AID), Fn(F), Target(TM), UsedPhysRegs(0) { 118 SSARegMapping = new SSARegMap(); 119 MFInfo = 0; 120 FrameInfo = new MachineFrameInfo(); 121 ConstantPool = new MachineConstantPool(TM.getTargetData()); 122 JumpTableInfo = new MachineJumpTableInfo(TM.getTargetData()); 123 BasicBlocks.Parent = this; 124 } 125 126 MachineFunction::~MachineFunction() { 127 BasicBlocks.clear(); 128 delete SSARegMapping; 129 delete MFInfo; 130 delete FrameInfo; 131 delete ConstantPool; 132 delete JumpTableInfo; 133 delete[] UsedPhysRegs; 134 } 135 136 137 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and 138 /// recomputes them. This guarantees that the MBB numbers are sequential, 139 /// dense, and match the ordering of the blocks within the function. If a 140 /// specific MachineBasicBlock is specified, only that block and those after 141 /// it are renumbered. 142 void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { 143 if (empty()) { MBBNumbering.clear(); return; } 144 MachineFunction::iterator MBBI, E = end(); 145 if (MBB == 0) 146 MBBI = begin(); 147 else 148 MBBI = MBB; 149 150 // Figure out the block number this should have. 151 unsigned BlockNo = 0; 152 if (MBBI != begin()) 153 BlockNo = prior(MBBI)->getNumber()+1; 154 155 for (; MBBI != E; ++MBBI, ++BlockNo) { 156 if (MBBI->getNumber() != (int)BlockNo) { 157 // Remove use of the old number. 158 if (MBBI->getNumber() != -1) { 159 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && 160 "MBB number mismatch!"); 161 MBBNumbering[MBBI->getNumber()] = 0; 162 } 163 164 // If BlockNo is already taken, set that block's number to -1. 165 if (MBBNumbering[BlockNo]) 166 MBBNumbering[BlockNo]->setNumber(-1); 167 168 MBBNumbering[BlockNo] = MBBI; 169 MBBI->setNumber(BlockNo); 170 } 171 } 172 173 // Okay, all the blocks are renumbered. If we have compactified the block 174 // numbering, shrink MBBNumbering now. 175 assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); 176 MBBNumbering.resize(BlockNo); 177 } 178 179 180 void MachineFunction::dump() const { print(std::cerr); } 181 182 void MachineFunction::print(std::ostream &OS) const { 183 OS << "# Machine code for " << Fn->getName () << "():\n"; 184 185 // Print Frame Information 186 getFrameInfo()->print(*this, OS); 187 188 // Print JumpTable Information 189 getJumpTableInfo()->print(OS); 190 191 // Print Constant Pool 192 getConstantPool()->print(OS); 193 194 const MRegisterInfo *MRI = getTarget().getRegisterInfo(); 195 196 if (livein_begin() != livein_end()) { 197 OS << "Live Ins:"; 198 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) { 199 if (MRI) 200 OS << " " << MRI->getName(I->first); 201 else 202 OS << " Reg #" << I->first; 203 204 if (I->second) 205 OS << " in VR#" << I->second << " "; 206 } 207 OS << "\n"; 208 } 209 if (liveout_begin() != liveout_end()) { 210 OS << "Live Outs:"; 211 for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I) 212 if (MRI) 213 OS << " " << MRI->getName(*I); 214 else 215 OS << " Reg #" << *I; 216 OS << "\n"; 217 } 218 219 for (const_iterator BB = begin(); BB != end(); ++BB) 220 BB->print(OS); 221 222 OS << "\n# End machine code for " << Fn->getName () << "().\n\n"; 223 } 224 225 /// CFGOnly flag - This is used to control whether or not the CFG graph printer 226 /// prints out the contents of basic blocks or not. This is acceptable because 227 /// this code is only really used for debugging purposes. 228 /// 229 static bool CFGOnly = false; 230 231 namespace llvm { 232 template<> 233 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { 234 static std::string getGraphName(const MachineFunction *F) { 235 return "CFG for '" + F->getFunction()->getName() + "' function"; 236 } 237 238 static std::string getNodeLabel(const MachineBasicBlock *Node, 239 const MachineFunction *Graph) { 240 if (CFGOnly && Node->getBasicBlock() && 241 !Node->getBasicBlock()->getName().empty()) 242 return Node->getBasicBlock()->getName() + ":"; 243 244 std::ostringstream Out; 245 if (CFGOnly) { 246 Out << Node->getNumber() << ':'; 247 return Out.str(); 248 } 249 250 Node->print(Out); 251 252 std::string OutStr = Out.str(); 253 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 254 255 // Process string output to make it nicer... 256 for (unsigned i = 0; i != OutStr.length(); ++i) 257 if (OutStr[i] == '\n') { // Left justify 258 OutStr[i] = '\\'; 259 OutStr.insert(OutStr.begin()+i+1, 'l'); 260 } 261 return OutStr; 262 } 263 }; 264 } 265 266 void MachineFunction::viewCFG() const 267 { 268 #ifndef NDEBUG 269 ViewGraph(this, "mf" + getFunction()->getName()); 270 #else 271 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on " 272 << "systems with Graphviz or gv!\n"; 273 #endif // NDEBUG 274 } 275 276 void MachineFunction::viewCFGOnly() const 277 { 278 CFGOnly = true; 279 viewCFG(); 280 CFGOnly = false; 281 } 282 283 // The next two methods are used to construct and to retrieve 284 // the MachineCodeForFunction object for the given function. 285 // construct() -- Allocates and initializes for a given function and target 286 // get() -- Returns a handle to the object. 287 // This should not be called before "construct()" 288 // for a given Function. 289 // 290 MachineFunction& 291 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar) 292 { 293 assert(Fn->getAnnotation(MF_AID) == 0 && 294 "Object already exists for this function!"); 295 MachineFunction* mcInfo = new MachineFunction(Fn, Tar); 296 Fn->addAnnotation(mcInfo); 297 return *mcInfo; 298 } 299 300 void MachineFunction::destruct(const Function *Fn) { 301 bool Deleted = Fn->deleteAnnotation(MF_AID); 302 assert(Deleted && "Machine code did not exist for function!"); 303 } 304 305 MachineFunction& MachineFunction::get(const Function *F) 306 { 307 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID); 308 assert(mc && "Call construct() method first to allocate the object"); 309 return *mc; 310 } 311 312 void MachineFunction::clearSSARegMap() { 313 delete SSARegMapping; 314 SSARegMapping = 0; 315 } 316 317 //===----------------------------------------------------------------------===// 318 // MachineFrameInfo implementation 319 //===----------------------------------------------------------------------===// 320 321 void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{ 322 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea(); 323 324 for (unsigned i = 0, e = Objects.size(); i != e; ++i) { 325 const StackObject &SO = Objects[i]; 326 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: "; 327 if (SO.Size == 0) 328 OS << "variable sized"; 329 else 330 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ","); 331 OS << " alignment is " << SO.Alignment << " byte" 332 << (SO.Alignment != 1 ? "s," : ","); 333 334 if (i < NumFixedObjects) 335 OS << " fixed"; 336 if (i < NumFixedObjects || SO.SPOffset != -1) { 337 int Off = SO.SPOffset - ValOffset; 338 OS << " at location [SP"; 339 if (Off > 0) 340 OS << "+" << Off; 341 else if (Off < 0) 342 OS << Off; 343 OS << "]"; 344 } 345 OS << "\n"; 346 } 347 348 if (HasVarSizedObjects) 349 OS << " Stack frame contains variable sized objects\n"; 350 } 351 352 void MachineFrameInfo::dump(const MachineFunction &MF) const { 353 print(MF, std::cerr); 354 } 355 356 357 //===----------------------------------------------------------------------===// 358 // MachineJumpTableInfo implementation 359 //===----------------------------------------------------------------------===// 360 361 /// getJumpTableIndex - Create a new jump table entry in the jump table info 362 /// or return an existing one. 363 /// 364 unsigned MachineJumpTableInfo::getJumpTableIndex( 365 std::vector<MachineBasicBlock*> &DestBBs) { 366 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) 367 if (JumpTables[i].MBBs == DestBBs) 368 return i; 369 370 JumpTables.push_back(MachineJumpTableEntry(DestBBs)); 371 return JumpTables.size()-1; 372 } 373 374 375 void MachineJumpTableInfo::print(std::ostream &OS) const { 376 // FIXME: this is lame, maybe we could print out the MBB numbers or something 377 // like {1, 2, 4, 5, 3, 0} 378 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { 379 OS << " <jt #" << i << "> has " << JumpTables[i].MBBs.size() 380 << " entries\n"; 381 } 382 } 383 384 unsigned MachineJumpTableInfo::getEntrySize() const { 385 return TD->getPointerSize(); 386 } 387 388 unsigned MachineJumpTableInfo::getAlignment() const { 389 return TD->getPointerAlignment(); 390 } 391 392 void MachineJumpTableInfo::dump() const { print(std::cerr); } 393 394 395 //===----------------------------------------------------------------------===// 396 // MachineConstantPool implementation 397 //===----------------------------------------------------------------------===// 398 399 const Type *MachineConstantPoolEntry::getType() const { 400 if (isMachineConstantPoolEntry()) 401 return Val.MachineCPVal->getType(); 402 return Val.ConstVal->getType(); 403 } 404 405 MachineConstantPool::~MachineConstantPool() { 406 for (unsigned i = 0, e = Constants.size(); i != e; ++i) 407 if (Constants[i].isMachineConstantPoolEntry()) 408 delete Constants[i].Val.MachineCPVal; 409 } 410 411 /// getConstantPoolIndex - Create a new entry in the constant pool or return 412 /// an existing one. User must specify an alignment in bytes for the object. 413 /// 414 unsigned MachineConstantPool::getConstantPoolIndex(Constant *C, 415 unsigned Alignment) { 416 assert(Alignment && "Alignment must be specified!"); 417 if (Alignment > PoolAlignment) PoolAlignment = Alignment; 418 419 // Check to see if we already have this constant. 420 // 421 // FIXME, this could be made much more efficient for large constant pools. 422 unsigned AlignMask = (1 << Alignment)-1; 423 for (unsigned i = 0, e = Constants.size(); i != e; ++i) 424 if (Constants[i].Val.ConstVal == C && (Constants[i].Offset & AlignMask)== 0) 425 return i; 426 427 unsigned Offset = 0; 428 if (!Constants.empty()) { 429 Offset = Constants.back().getOffset(); 430 Offset += TD->getTypeSize(Constants.back().getType()); 431 Offset = (Offset+AlignMask)&~AlignMask; 432 } 433 434 Constants.push_back(MachineConstantPoolEntry(C, Offset)); 435 return Constants.size()-1; 436 } 437 438 unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, 439 unsigned Alignment) { 440 assert(Alignment && "Alignment must be specified!"); 441 if (Alignment > PoolAlignment) PoolAlignment = Alignment; 442 443 // Check to see if we already have this constant. 444 // 445 // FIXME, this could be made much more efficient for large constant pools. 446 unsigned AlignMask = (1 << Alignment)-1; 447 int Idx = V->getExistingMachineCPValue(this, Alignment); 448 if (Idx != -1) 449 return (unsigned)Idx; 450 451 unsigned Offset = 0; 452 if (!Constants.empty()) { 453 Offset = Constants.back().getOffset(); 454 Offset += TD->getTypeSize(Constants.back().getType()); 455 Offset = (Offset+AlignMask)&~AlignMask; 456 } 457 458 Constants.push_back(MachineConstantPoolEntry(V, Offset)); 459 return Constants.size()-1; 460 } 461 462 463 void MachineConstantPool::print(std::ostream &OS) const { 464 for (unsigned i = 0, e = Constants.size(); i != e; ++i) { 465 OS << " <cp #" << i << "> is"; 466 if (Constants[i].isMachineConstantPoolEntry()) 467 Constants[i].Val.MachineCPVal->print(OS); 468 else 469 OS << *(Value*)Constants[i].Val.ConstVal; 470 OS << " , offset=" << Constants[i].Offset; 471 OS << "\n"; 472 } 473 } 474 475 void MachineConstantPool::dump() const { print(std::cerr); } 476