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/Passes.h" 22 #include "llvm/Target/TargetMachine.h" 23 #include "llvm/Target/TargetFrameInfo.h" 24 #include "llvm/Function.h" 25 #include "llvm/Instructions.h" 26 #include "Support/LeakDetector.h" 27 #include "Support/GraphWriter.h" 28 #include <fstream> 29 #include <iostream> 30 #include <sstream> 31 32 using namespace llvm; 33 34 static AnnotationID MF_AID( 35 AnnotationManager::getID("CodeGen::MachineCodeForFunction")); 36 37 38 namespace { 39 struct Printer : public MachineFunctionPass { 40 std::ostream *OS; 41 const std::string Banner; 42 43 Printer (std::ostream *_OS, const std::string &_Banner) : 44 OS (_OS), Banner (_Banner) { } 45 46 const char *getPassName() const { return "MachineFunction Printer"; } 47 48 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 49 AU.setPreservesAll(); 50 } 51 52 bool runOnMachineFunction(MachineFunction &MF) { 53 (*OS) << Banner; 54 MF.print (*OS); 55 return false; 56 } 57 }; 58 } 59 60 /// Returns a newly-created MachineFunction Printer pass. The default output 61 /// stream is std::cerr; the default banner is empty. 62 /// 63 FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS, 64 const std::string &Banner) { 65 return new Printer(OS, Banner); 66 } 67 68 namespace { 69 struct Deleter : public MachineFunctionPass { 70 const char *getPassName() const { return "Machine Code Deleter"; } 71 72 bool runOnMachineFunction(MachineFunction &MF) { 73 // Delete the annotation from the function now. 74 MachineFunction::destruct(MF.getFunction()); 75 return true; 76 } 77 }; 78 } 79 80 /// MachineCodeDeletion Pass - This pass deletes all of the machine code for 81 /// the current function, which should happen after the function has been 82 /// emitted to a .s file or to memory. 83 FunctionPass *llvm::createMachineCodeDeleter() { 84 return new Deleter(); 85 } 86 87 88 89 //===---------------------------------------------------------------------===// 90 // MachineFunction implementation 91 //===---------------------------------------------------------------------===// 92 MachineBasicBlock* ilist_traits<MachineBasicBlock>::createNode() 93 { 94 MachineBasicBlock* dummy = new MachineBasicBlock(); 95 LeakDetector::removeGarbageObject(dummy); 96 return dummy; 97 } 98 99 void ilist_traits<MachineBasicBlock>::transferNodesFromList( 100 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList, 101 ilist_iterator<MachineBasicBlock> first, 102 ilist_iterator<MachineBasicBlock> last) 103 { 104 if (Parent != toList.Parent) 105 for (; first != last; ++first) 106 first->Parent = toList.Parent; 107 } 108 109 MachineFunction::MachineFunction(const Function *F, 110 const TargetMachine &TM) 111 : Annotation(MF_AID), Fn(F), Target(TM) { 112 SSARegMapping = new SSARegMap(); 113 MFInfo = 0; 114 FrameInfo = new MachineFrameInfo(); 115 ConstantPool = new MachineConstantPool(); 116 BasicBlocks.Parent = this; 117 } 118 119 MachineFunction::~MachineFunction() { 120 BasicBlocks.clear(); 121 delete SSARegMapping; 122 delete MFInfo; 123 delete FrameInfo; 124 delete ConstantPool; 125 } 126 127 void MachineFunction::dump() const { print(std::cerr); } 128 129 void MachineFunction::print(std::ostream &OS) const { 130 OS << "# Machine code for " << Fn->getName () << "():\n"; 131 132 // Print Frame Information 133 getFrameInfo()->print(*this, OS); 134 135 // Print Constant Pool 136 getConstantPool()->print(OS); 137 138 for (const_iterator BB = begin(); BB != end(); ++BB) 139 BB->print(OS); 140 141 OS << "\n# End machine code for " << Fn->getName () << "().\n\n"; 142 } 143 144 /// CFGOnly flag - This is used to control whether or not the CFG graph printer 145 /// prints out the contents of basic blocks or not. This is acceptable because 146 /// this code is only really used for debugging purposes. 147 /// 148 static bool CFGOnly = false; 149 150 namespace llvm { 151 template<> 152 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { 153 static std::string getGraphName(const MachineFunction *F) { 154 return "CFG for '" + F->getFunction()->getName() + "' function"; 155 } 156 157 static std::string getNodeLabel(const MachineBasicBlock *Node, 158 const MachineFunction *Graph) { 159 if (CFGOnly && Node->getBasicBlock() && 160 !Node->getBasicBlock()->getName().empty()) 161 return Node->getBasicBlock()->getName() + ":"; 162 163 std::ostringstream Out; 164 if (CFGOnly) { 165 Out << Node->getNumber() << ':'; 166 return Out.str(); 167 } 168 169 Node->print(Out); 170 171 std::string OutStr = Out.str(); 172 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 173 174 // Process string output to make it nicer... 175 for (unsigned i = 0; i != OutStr.length(); ++i) 176 if (OutStr[i] == '\n') { // Left justify 177 OutStr[i] = '\\'; 178 OutStr.insert(OutStr.begin()+i+1, 'l'); 179 } 180 return OutStr; 181 } 182 }; 183 } 184 185 void MachineFunction::viewCFG() const 186 { 187 std::string Filename = "/tmp/cfg." + getFunction()->getName() + ".dot"; 188 std::cerr << "Writing '" << Filename << "'... "; 189 std::ofstream F(Filename.c_str()); 190 191 if (!F) { 192 std::cerr << " error opening file for writing!\n"; 193 return; 194 } 195 196 WriteGraph(F, this); 197 F.close(); 198 std::cerr << "\n"; 199 200 std::cerr << "Running 'dot' program... " << std::flush; 201 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename 202 + " > /tmp/cfg.tempgraph.ps").c_str())) { 203 std::cerr << "Error running dot: 'dot' not in path?\n"; 204 } else { 205 std::cerr << "\n"; 206 system("gv /tmp/cfg.tempgraph.ps"); 207 } 208 system(("rm " + Filename + " /tmp/cfg.tempgraph.ps").c_str()); 209 } 210 211 void MachineFunction::viewCFGOnly() const 212 { 213 CFGOnly = true; 214 viewCFG(); 215 CFGOnly = false; 216 } 217 218 // The next two methods are used to construct and to retrieve 219 // the MachineCodeForFunction object for the given function. 220 // construct() -- Allocates and initializes for a given function and target 221 // get() -- Returns a handle to the object. 222 // This should not be called before "construct()" 223 // for a given Function. 224 // 225 MachineFunction& 226 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar) 227 { 228 assert(Fn->getAnnotation(MF_AID) == 0 && 229 "Object already exists for this function!"); 230 MachineFunction* mcInfo = new MachineFunction(Fn, Tar); 231 Fn->addAnnotation(mcInfo); 232 return *mcInfo; 233 } 234 235 void MachineFunction::destruct(const Function *Fn) { 236 bool Deleted = Fn->deleteAnnotation(MF_AID); 237 assert(Deleted && "Machine code did not exist for function!"); 238 } 239 240 MachineFunction& MachineFunction::get(const Function *F) 241 { 242 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID); 243 assert(mc && "Call construct() method first to allocate the object"); 244 return *mc; 245 } 246 247 void MachineFunction::clearSSARegMap() { 248 delete SSARegMapping; 249 SSARegMapping = 0; 250 } 251 252 //===----------------------------------------------------------------------===// 253 // MachineFrameInfo implementation 254 //===----------------------------------------------------------------------===// 255 256 /// CreateStackObject - Create a stack object for a value of the specified type. 257 /// 258 int MachineFrameInfo::CreateStackObject(const Type *Ty, const TargetData &TD) { 259 return CreateStackObject(TD.getTypeSize(Ty), TD.getTypeAlignment(Ty)); 260 } 261 262 263 void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{ 264 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea(); 265 266 for (unsigned i = 0, e = Objects.size(); i != e; ++i) { 267 const StackObject &SO = Objects[i]; 268 OS << " <fi #" << (int)(i-NumFixedObjects) << "> is "; 269 if (SO.Size == 0) 270 OS << "variable sized"; 271 else 272 OS << SO.Size << " byte" << (SO.Size != 1 ? "s" : " "); 273 274 if (i < NumFixedObjects) 275 OS << " fixed"; 276 if (i < NumFixedObjects || SO.SPOffset != -1) { 277 int Off = SO.SPOffset - ValOffset; 278 OS << " at location [SP"; 279 if (Off > 0) 280 OS << "+" << Off; 281 else if (Off < 0) 282 OS << Off; 283 OS << "]"; 284 } 285 OS << "\n"; 286 } 287 288 if (HasVarSizedObjects) 289 OS << " Stack frame contains variable sized objects\n"; 290 } 291 292 void MachineFrameInfo::dump(const MachineFunction &MF) const { 293 print(MF, std::cerr); 294 } 295 296 297 //===----------------------------------------------------------------------===// 298 // MachineConstantPool implementation 299 //===----------------------------------------------------------------------===// 300 301 void MachineConstantPool::print(std::ostream &OS) const { 302 for (unsigned i = 0, e = Constants.size(); i != e; ++i) 303 OS << " <cp #" << i << "> is" << *(Value*)Constants[i] << "\n"; 304 } 305 306 void MachineConstantPool::dump() const { print(std::cerr); } 307 308