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