Switched to rendering after allocation (but before rewriting) in PBQP.
[oota-llvm.git] / lib / CodeGen / RenderMachineFunction.cpp
1 //===-- llvm/CodeGen/RenderMachineFunction.cpp - MF->HTML -----s-----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #define DEBUG_TYPE "rendermf"
11
12 #include "RenderMachineFunction.h"
13
14 #include "VirtRegMap.h"
15
16 #include "llvm/Function.h"
17 #include "llvm/Module.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstr.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetMachine.h"
27
28 #include <sstream>
29
30 using namespace llvm;
31
32 char RenderMachineFunction::ID = 0;
33 static RegisterPass<RenderMachineFunction>
34 X("rendermf", "Render machine functions (and related info) to HTML pages");
35
36 static cl::opt<std::string>
37 outputFileSuffix("rmf-file-suffix",
38                  cl::desc("Appended to function name to get output file name "
39                           "(default: \".html\")"),
40                  cl::init(".html"), cl::Hidden);
41
42 static cl::opt<std::string>
43 machineFuncsToRender("rmf-funcs",
44                      cl::desc("Coma seperated list of functions to render"
45                               ", or \"*\"."),
46                      cl::init(""), cl::Hidden);
47
48 static cl::opt<std::string>
49 pressureClasses("rmf-classes",
50                 cl::desc("Register classes to render pressure for."),
51                 cl::init(""), cl::Hidden);
52
53 static cl::opt<std::string>
54 showIntervals("rmf-intervals",
55               cl::desc("Live intervals to show alongside code."),
56               cl::init(""), cl::Hidden);
57
58 static cl::opt<bool>
59 showEmptyIndexes("rmf-empty-indexes",
60                  cl::desc("Render indexes not associated with instructions or "
61                           "MBB starts."),
62                  cl::init(false), cl::Hidden);
63
64 static cl::opt<bool>
65 useFancyVerticals("rmf-fancy-verts",
66                   cl::desc("Use SVG for vertical text."),
67                   cl::init(true), cl::Hidden);
68
69 namespace llvm {
70
71   bool MFRenderingOptions::renderingOptionsProcessed;
72   std::set<std::string> MFRenderingOptions::mfNamesToRender;
73   bool MFRenderingOptions::renderAllMFs = false;
74
75   std::set<std::string> MFRenderingOptions::classNamesToRender;
76   bool MFRenderingOptions::renderAllClasses = false;
77
78   std::set<std::pair<unsigned, unsigned> >
79     MFRenderingOptions::intervalNumsToRender;
80   unsigned MFRenderingOptions::intervalTypesToRender = ExplicitOnly;
81
82   template <typename OutputItr>
83   void MFRenderingOptions::splitComaSeperatedList(const std::string &s,
84                                                          OutputItr outItr) {
85     std::string::const_iterator curPos = s.begin();
86     std::string::const_iterator nextComa = std::find(curPos, s.end(), ',');
87     while (nextComa != s.end()) {
88       std::string elem;
89       std::copy(curPos, nextComa, std::back_inserter(elem));
90       *outItr = elem;
91       ++outItr;
92       curPos = llvm::next(nextComa);
93       nextComa = std::find(curPos, s.end(), ',');
94     }
95
96     if (curPos != s.end()) {
97       std::string elem;
98       std::copy(curPos, s.end(), std::back_inserter(elem));
99       *outItr = elem;
100       ++outItr;
101     }
102   }
103
104   void MFRenderingOptions::processOptions() {
105     if (!renderingOptionsProcessed) {
106       processFuncNames();
107       processRegClassNames();
108       processIntervalNumbers();
109       renderingOptionsProcessed = true;
110     }
111   }
112
113   void MFRenderingOptions::processFuncNames() {
114     if (machineFuncsToRender == "*") {
115       renderAllMFs = true;
116     } else {
117       splitComaSeperatedList(machineFuncsToRender,
118                              std::inserter(mfNamesToRender,
119                                            mfNamesToRender.begin()));
120     }
121   }
122
123   void MFRenderingOptions::processRegClassNames() {
124     if (pressureClasses == "*") {
125       renderAllClasses = true;
126     } else {
127       splitComaSeperatedList(pressureClasses,
128                              std::inserter(classNamesToRender,
129                                            classNamesToRender.begin()));
130     }
131   }
132
133   void MFRenderingOptions::processIntervalNumbers() {
134     std::set<std::string> intervalRanges;
135     splitComaSeperatedList(showIntervals,
136                            std::inserter(intervalRanges,
137                                          intervalRanges.begin()));
138     std::for_each(intervalRanges.begin(), intervalRanges.end(),
139                   processIntervalRange);
140   }
141
142   void MFRenderingOptions::processIntervalRange(
143                                           const std::string &intervalRangeStr) {
144     if (intervalRangeStr == "*") {
145       intervalTypesToRender |= All;
146     } else if (intervalRangeStr == "virt*") {
147       intervalTypesToRender |= VirtPlusExplicit;
148     } else if (intervalRangeStr == "phys*") {
149       intervalTypesToRender |= PhysPlusExplicit;
150     } else {
151       std::istringstream iss(intervalRangeStr);
152       unsigned reg1, reg2;
153       if ((iss >> reg1 >> std::ws)) {
154         if (iss.eof()) {
155           intervalNumsToRender.insert(std::make_pair(reg1, reg1 + 1));
156         } else {
157           char c;
158           iss >> c;
159           if (c == '-' && (iss >> reg2)) {
160             intervalNumsToRender.insert(std::make_pair(reg1, reg2 + 1));
161           } else {
162             dbgs() << "Warning: Invalid interval range \""
163                    << intervalRangeStr << "\" in -rmf-intervals. Skipping.\n";
164           }
165         }
166       } else {
167         dbgs() << "Warning: Invalid interval number \""
168                << intervalRangeStr << "\" in -rmf-intervals. Skipping.\n";
169       }
170     }
171   }
172
173   void MFRenderingOptions::setup(MachineFunction *mf,
174                                  const TargetRegisterInfo *tri,
175                                  LiveIntervals *lis) {
176     this->mf = mf;
177     this->tri = tri;
178     this->lis = lis;
179
180     clear();
181   }
182
183   void MFRenderingOptions::clear() {
184     regClassesTranslatedToCurrentFunction = false;
185     regClassSet.clear();
186
187     intervalsTranslatedToCurrentFunction = false;
188     intervalSet.clear();
189   }
190
191   void MFRenderingOptions::resetRenderSpecificOptions() {
192     intervalSet.clear();
193     intervalsTranslatedToCurrentFunction = false;
194   }
195
196   bool MFRenderingOptions::shouldRenderCurrentMachineFunction() const {
197     processOptions();
198
199     return (renderAllMFs ||
200             mfNamesToRender.find(mf->getFunction()->getName()) !=
201               mfNamesToRender.end());    
202   }
203
204   const MFRenderingOptions::RegClassSet& MFRenderingOptions::regClasses() const{
205     translateRegClassNamesToCurrentFunction();
206     return regClassSet;
207   }
208
209   const MFRenderingOptions::IntervalSet& MFRenderingOptions::intervals() const {
210     translateIntervalNumbersToCurrentFunction();
211     return intervalSet;
212   }
213
214   bool MFRenderingOptions::renderEmptyIndexes() const {
215     return showEmptyIndexes;
216   }
217
218   bool MFRenderingOptions::fancyVerticals() const {
219     return useFancyVerticals;
220   }
221
222   void MFRenderingOptions::translateRegClassNamesToCurrentFunction() const {
223     if (!regClassesTranslatedToCurrentFunction) {
224       processOptions();
225       for (TargetRegisterInfo::regclass_iterator rcItr = tri->regclass_begin(),
226                                                  rcEnd = tri->regclass_end();
227            rcItr != rcEnd; ++rcItr) {
228         const TargetRegisterClass *trc = *rcItr;
229         if (renderAllClasses ||
230             classNamesToRender.find(trc->getName()) !=
231               classNamesToRender.end()) {
232           regClassSet.insert(trc);
233         }
234       }
235       regClassesTranslatedToCurrentFunction = true;
236     }
237   }
238
239   void MFRenderingOptions::translateIntervalNumbersToCurrentFunction() const {
240     if (!intervalsTranslatedToCurrentFunction) {
241       processOptions();
242
243       // If we're not just doing explicit then do a copy over all matching
244       // types.
245       if (intervalTypesToRender != ExplicitOnly) {
246         for (LiveIntervals::iterator liItr = lis->begin(), liEnd = lis->end();
247              liItr != liEnd; ++liItr) {
248
249           if ((TargetRegisterInfo::isPhysicalRegister(liItr->first) &&
250                (intervalTypesToRender & PhysPlusExplicit)) ||
251               (TargetRegisterInfo::isVirtualRegister(liItr->first) &&
252                (intervalTypesToRender & VirtPlusExplicit))) {
253             intervalSet.insert(liItr->second);
254           }
255         }
256       }
257
258       // If we need to process the explicit list...
259       if (intervalTypesToRender != All) {
260         for (std::set<std::pair<unsigned, unsigned> >::const_iterator
261                regRangeItr = intervalNumsToRender.begin(),
262                regRangeEnd = intervalNumsToRender.end();
263              regRangeItr != regRangeEnd; ++regRangeItr) {
264           const std::pair<unsigned, unsigned> &range = *regRangeItr;
265           for (unsigned reg = range.first; reg != range.second; ++reg) {
266             if (lis->hasInterval(reg)) {
267               intervalSet.insert(&lis->getInterval(reg));
268             }
269           }
270         }
271       }
272
273       intervalsTranslatedToCurrentFunction = true;
274     }
275   }
276
277   // ---------- TargetRegisterExtraInformation implementation ----------
278
279   TargetRegisterExtraInfo::TargetRegisterExtraInfo()
280     : mapsPopulated(false) {
281   }
282
283   void TargetRegisterExtraInfo::setup(MachineFunction *mf,
284                                       MachineRegisterInfo *mri,
285                                       const TargetRegisterInfo *tri,
286                                       LiveIntervals *lis) {
287     this->mf = mf;
288     this->mri = mri;
289     this->tri = tri;
290     this->lis = lis;
291   }
292
293   void TargetRegisterExtraInfo::reset() {
294     if (!mapsPopulated) {
295       initWorst();
296       //initBounds();
297       initCapacity();
298       mapsPopulated = true;
299     }
300
301     resetPressureAndLiveStates();
302   }
303
304   void TargetRegisterExtraInfo::clear() {
305     prWorst.clear();
306     vrWorst.clear();
307     capacityMap.clear();
308     pressureMap.clear();
309     //liveStatesMap.clear();
310     mapsPopulated = false;
311   }
312
313   void TargetRegisterExtraInfo::initWorst() {
314     assert(!mapsPopulated && prWorst.empty() && vrWorst.empty() &&
315            "Worst map already initialised?");
316
317     // Start with the physical registers.
318     for (unsigned preg = 1; preg < tri->getNumRegs(); ++preg) {
319       WorstMapLine &pregLine = prWorst[preg];
320
321       for (TargetRegisterInfo::regclass_iterator rcItr = tri->regclass_begin(),
322                                                  rcEnd = tri->regclass_end();
323            rcItr != rcEnd; ++rcItr) {
324         const TargetRegisterClass *trc = *rcItr;
325
326         unsigned numOverlaps = 0;
327         for (TargetRegisterClass::iterator rItr = trc->begin(),
328                                            rEnd = trc->end();
329              rItr != rEnd; ++rItr) {
330           unsigned trcPReg = *rItr;
331           if (tri->regsOverlap(preg, trcPReg))
332             ++numOverlaps;
333         }
334         
335         pregLine[trc] = numOverlaps;
336       }
337     }
338
339     // Now the register classes.
340     for (TargetRegisterInfo::regclass_iterator rc1Itr = tri->regclass_begin(),
341                                                rcEnd = tri->regclass_end();
342          rc1Itr != rcEnd; ++rc1Itr) {
343       const TargetRegisterClass *trc1 = *rc1Itr;
344       WorstMapLine &classLine = vrWorst[trc1];
345
346       for (TargetRegisterInfo::regclass_iterator rc2Itr = tri->regclass_begin();
347            rc2Itr != rcEnd; ++rc2Itr) {
348         const TargetRegisterClass *trc2 = *rc2Itr;
349
350         unsigned worst = 0;
351
352         for (TargetRegisterClass::iterator trc1Itr = trc1->begin(),
353                                            trc1End = trc1->end();
354              trc1Itr != trc1End; ++trc1Itr) {
355           unsigned trc1Reg = *trc1Itr;
356           unsigned trc1RegWorst = 0;
357
358           for (TargetRegisterClass::iterator trc2Itr = trc2->begin(),
359                                              trc2End = trc2->end();
360                trc2Itr != trc2End; ++trc2Itr) {
361             unsigned trc2Reg = *trc2Itr;
362             if (tri->regsOverlap(trc1Reg, trc2Reg))
363               ++trc1RegWorst;
364           }
365           if (trc1RegWorst > worst) {
366             worst = trc1RegWorst;
367           }    
368         }
369
370         if (worst != 0) {
371           classLine[trc2] = worst;
372         }
373       }
374     }
375   }
376
377   unsigned TargetRegisterExtraInfo::getWorst(
378                                         unsigned reg,
379                                         const TargetRegisterClass *trc) const {
380     const WorstMapLine *wml = 0;
381     if (TargetRegisterInfo::isPhysicalRegister(reg)) {
382       PRWorstMap::const_iterator prwItr = prWorst.find(reg);
383       assert(prwItr != prWorst.end() && "Missing prWorst entry.");
384       wml = &prwItr->second;
385     } else {
386       const TargetRegisterClass *regTRC = mri->getRegClass(reg);
387       VRWorstMap::const_iterator vrwItr = vrWorst.find(regTRC);
388       assert(vrwItr != vrWorst.end() && "Missing vrWorst entry.");
389       wml = &vrwItr->second;
390     }
391     
392     WorstMapLine::const_iterator wmlItr = wml->find(trc);
393     if (wmlItr == wml->end())
394       return 0;
395
396     return wmlItr->second;
397   }
398
399   void TargetRegisterExtraInfo::initCapacity() {
400     assert(!mapsPopulated && capacityMap.empty() &&
401            "Capacity map already initialised?");
402
403     for (TargetRegisterInfo::regclass_iterator rcItr = tri->regclass_begin(),
404            rcEnd = tri->regclass_end();
405          rcItr != rcEnd; ++rcItr) {
406       const TargetRegisterClass *trc = *rcItr;
407       unsigned capacity = std::distance(trc->allocation_order_begin(*mf),
408                                         trc->allocation_order_end(*mf));
409
410       if (capacity != 0)
411         capacityMap[trc] = capacity;
412     }
413   }
414
415   unsigned TargetRegisterExtraInfo::getCapacity(
416                                          const TargetRegisterClass *trc) const {
417     CapacityMap::const_iterator cmItr = capacityMap.find(trc);
418     assert(cmItr != capacityMap.end() &&
419            "vreg with unallocable register class");
420     return cmItr->second;
421   }
422
423   void TargetRegisterExtraInfo::resetPressureAndLiveStates() {
424     pressureMap.clear();
425     //liveStatesMap.clear();
426
427     // Iterate over all slots.
428     
429
430     // Iterate over all live intervals.
431     for (LiveIntervals::iterator liItr = lis->begin(),
432            liEnd = lis->end();
433          liItr != liEnd; ++liItr) {
434       LiveInterval *li = liItr->second;
435
436       const TargetRegisterClass *liTRC;
437
438       if (TargetRegisterInfo::isPhysicalRegister(li->reg))
439         continue;
440       
441       liTRC = mri->getRegClass(li->reg);
442      
443
444       // For all ranges in the current interal.
445       for (LiveInterval::iterator lrItr = li->begin(),
446              lrEnd = li->end();
447            lrItr != lrEnd; ++lrItr) {
448         LiveRange *lr = &*lrItr;
449         
450         // For all slots in the current range.
451         for (SlotIndex i = lr->start; i != lr->end; i = i.getNextSlot()) {
452
453           // Record increased pressure at index for all overlapping classes.
454           for (TargetRegisterInfo::regclass_iterator
455                  rcItr = tri->regclass_begin(),
456                  rcEnd = tri->regclass_end();
457                rcItr != rcEnd; ++rcItr) {
458             const TargetRegisterClass *trc = *rcItr;
459
460             if (trc->allocation_order_begin(*mf) ==
461                 trc->allocation_order_end(*mf))
462               continue;
463
464             unsigned worstAtI = getWorst(li->reg, trc);
465
466             if (worstAtI != 0) {
467               pressureMap[i][trc] += worstAtI;
468             }
469           }
470         }
471       }
472     } 
473   }
474
475   unsigned TargetRegisterExtraInfo::getPressureAtSlot(
476                                                  const TargetRegisterClass *trc,
477                                                  SlotIndex i) const {
478     PressureMap::const_iterator pmItr = pressureMap.find(i);
479     if (pmItr == pressureMap.end())
480       return 0;
481     const PressureMapLine &pmLine = pmItr->second;
482     PressureMapLine::const_iterator pmlItr = pmLine.find(trc);
483     if (pmlItr == pmLine.end())
484       return 0;
485     return pmlItr->second;
486   }
487
488   bool TargetRegisterExtraInfo::classOverCapacityAtSlot(
489                                                  const TargetRegisterClass *trc,
490                                                  SlotIndex i) const {
491     return (getPressureAtSlot(trc, i) > getCapacity(trc));
492   }
493
494   // ---------- MachineFunctionRenderer implementation ----------
495
496   template <typename Iterator>
497   std::string RenderMachineFunction::escapeChars(Iterator sBegin, Iterator sEnd) const {
498     std::string r;
499
500     for (Iterator sItr = sBegin; sItr != sEnd; ++sItr) {
501       char c = *sItr;
502
503       switch (c) {
504         case '<': r.append("&lt;"); break;
505         case '>': r.append("&gt;"); break;
506         case '&': r.append("&amp;"); break;
507         case ' ': r.append("&nbsp;"); break;
508         case '\"': r.append("&quot;"); break;
509         default: r.push_back(c); break;
510       }
511     }
512
513     return r;
514   }
515
516   RenderMachineFunction::LiveState
517   RenderMachineFunction::getLiveStateAt(const LiveInterval *li,
518                                         SlotIndex i) const {
519     const MachineInstr *mi = sis->getInstructionFromIndex(i);
520
521     if (li->liveAt(i)) {
522       if (mi == 0) {
523         if (vrm == 0 || 
524             (vrm->getStackSlot(li->reg) == VirtRegMap::NO_STACK_SLOT)) {
525           return AliveReg;
526         } else {
527           return AliveStack;
528         }
529       } else {
530         if (i.getSlot() == SlotIndex::DEF &&
531             mi->definesRegister(li->reg, tri)) {
532           return Defined;
533         } else if (i.getSlot() == SlotIndex::USE &&
534                    mi->readsRegister(li->reg)) {
535           return Used;
536         } else {
537           if (vrm == 0 || 
538               (vrm->getStackSlot(li->reg) == VirtRegMap::NO_STACK_SLOT)) {
539             return AliveReg;
540           } else {
541             return AliveStack;
542           }
543         }
544       }
545     }
546     return Dead;
547   }
548
549   /// \brief Render a machine instruction.
550   template <typename OStream>
551   void RenderMachineFunction::renderMachineInstr(OStream &os,
552                                                  const MachineInstr *mi) const {
553     std::string s;
554     raw_string_ostream oss(s);
555     oss << *mi;
556
557     os << escapeChars(oss.str());
558   }
559
560   template <typename OStream, typename T>
561   void RenderMachineFunction::renderVertical(const std::string &indent,
562                                              OStream &os,
563                                              const T &t) const {
564     if (ro.fancyVerticals()) {
565       os << indent << "<object\n"
566          << indent << "  class=\"obj\"\n"
567          << indent << "  type=\"image/svg+xml\"\n"
568          << indent << "  width=\"14px\"\n"
569          << indent << "  height=\"55px\"\n"
570          << indent << "  data=\"data:image/svg+xml,\n"
571          << indent << "    <svg xmlns='http://www.w3.org/2000/svg'>\n"
572          << indent << "      <text x='-55' y='10' "
573                       "font-family='Courier' font-size='12' "
574                       "transform='rotate(-90)' text-rendering='optimizeSpeed' "
575                       "fill='#000'>" << t << "</text>\n"
576          << indent << "    </svg>\">\n"
577          << indent << "</object>\n";
578     } else {
579       std::ostringstream oss;
580       oss << t;
581       std::string tStr(oss.str());
582
583       os << indent;
584       for (std::string::iterator tStrItr = tStr.begin(), tStrEnd = tStr.end();
585            tStrItr != tStrEnd; ++tStrItr) {
586         os << *tStrItr << "<br/> ";
587       }
588       os << "\n";
589     }
590   }
591
592   template <typename OStream>
593   void RenderMachineFunction::insertCSS(const std::string &indent,
594                                         OStream &os) const {
595     os << indent << "<style type=\"text/css\">\n"
596        << indent << "  body { font-color: black; }\n"
597        << indent << "  table.code td { font-family: monospace; "
598                     "border-width: 0px; border-style: solid; "
599                     "border-bottom: 1px solid #dddddd; white-space: nowrap; }\n"
600        << indent << "  table.code td.s-zp { background-color: #000000; }\n"
601        << indent << "  table.code td.s-up { background-color: #00ff00; }\n"
602        << indent << "  table.code td.s-op { background-color: #ff0000; }\n"
603        << indent << "  table.code td.l-na { background-color: #ffffff; }\n"
604        << indent << "  table.code td.l-def { background-color: #ff0000; }\n"
605        << indent << "  table.code td.l-use { background-color: #ffff00; }\n"
606        << indent << "  table.code td.l-sar { background-color: #000000; }\n"
607        << indent << "  table.code td.l-sas { background-color: #770000; }\n"
608        << indent << "  table.code th { border-width: 0px; "
609                     "border-style: solid; }\n"
610        << indent << "</style>\n";
611   }
612
613   template <typename OStream>
614   void RenderMachineFunction::renderFunctionSummary(
615                                     const std::string &indent, OStream &os,
616                                     const char * const renderContextStr) const {
617     os << indent << "<h1>Function: " << mf->getFunction()->getName()
618                  << "</h1>\n"
619        << indent << "<h2>Rendering context: " << renderContextStr << "</h2>\n";
620   }
621
622
623   template <typename OStream>
624   void RenderMachineFunction::renderPressureTableLegend(
625                                                       const std::string &indent,
626                                                       OStream &os) const {
627     os << indent << "<h2>Rendering Pressure Legend:</h2>\n"
628        << indent << "<table class=\"code\">\n"
629        << indent << "  <tr>\n"
630        << indent << "    <th>Pressure</th><th>Description</th>"
631                     "<th>Appearance</th>\n"
632        << indent << "  </tr>\n"
633        << indent << "  <tr>\n"
634        << indent << "    <td>No Pressure</td>"
635                     "    <td>No physical registers of this class requested.</td>"
636                     "    <td class=\"s-zp\">&nbsp;&nbsp;</td>\n"
637        << indent << "  </tr>\n"
638        << indent << "  <tr>\n"
639        << indent << "    <td>Low Pressure</td>"
640                     "    <td>Sufficient physical registers to meet demand.</td>"
641                     "    <td class=\"s-up\">&nbsp;&nbsp;</td>\n"
642        << indent << "  </tr>\n"
643        << indent << "  <tr>\n"
644        << indent << "    <td>High Pressure</td>"
645                     "    <td>Potentially insufficient physical registers to meet demand.</td>"
646                     "    <td class=\"s-op\">&nbsp;&nbsp;</td>\n"
647        << indent << "  </tr>\n"
648        << indent << "</table>\n";
649   }
650
651   template <typename OStream>
652   void RenderMachineFunction::renderCodeTablePlusPI(const std::string & indent,
653                                                     OStream &os) const {
654
655     os << indent << "<table cellpadding=0 cellspacing=0 class=\"code\">\n"
656        << indent << "  <tr>\n"
657        << indent << "    <th>index</th>\n"
658        << indent << "    <th>instr</th>\n";
659
660     // Header row:
661        
662     if (!ro.regClasses().empty()) {
663       for (MFRenderingOptions::RegClassSet::const_iterator
664              rcItr = ro.regClasses().begin(),
665              rcEnd = ro.regClasses().end();
666            rcItr != rcEnd; ++rcItr) {
667         const TargetRegisterClass *trc = *rcItr;
668         os << indent << "    <th>\n";
669         renderVertical(indent + "      ", os, trc->getName());
670         os << indent << "    </th>\n";
671       }
672     }
673
674     // FIXME: Is there a nicer way to insert space between columns in HTML?
675     if (!ro.regClasses().empty() && !ro.intervals().empty())
676       os << indent << "    <th>&nbsp;&nbsp;</th>\n";
677
678     if (!ro.intervals().empty()) {
679       for (MFRenderingOptions::IntervalSet::const_iterator
680              liItr = ro.intervals().begin(),
681              liEnd = ro.intervals().end();
682            liItr != liEnd; ++liItr) {
683
684         const LiveInterval *li = *liItr;
685         os << indent << "    <th>\n";
686         renderVertical(indent + "      ", os, li->reg);
687         os << indent << "    </th>\n";
688       }
689     }
690
691     os << indent << "  </tr>\n";
692
693     MachineInstr *mi = 0;
694
695     // Data rows:
696     for (SlotIndex i = sis->getZeroIndex(); i != sis->getLastIndex();
697          i = i.getNextSlot()) {
698       
699       os << indent << "  <tr height=6ex>\n";
700       
701       if (i.getSlot() == SlotIndex::LOAD) {
702         MachineBasicBlock *mbb = sis->getMBBFromIndex(i);
703         mi = sis->getInstructionFromIndex(i);
704
705         if (i == sis->getMBBStartIdx(mbb) || mi != 0 ||
706             ro.renderEmptyIndexes()) {
707           os << indent << "    <td rowspan=4>" << i << "&nbsp;</td>\n"
708              << indent << "    <td rowspan=4>\n";
709
710           if (i == sis->getMBBStartIdx(mbb)) {
711             os << indent << "      BB#" << mbb->getNumber() << ":&nbsp;\n";
712           } else if (mi != 0) {
713             os << indent << "      &nbsp;&nbsp;";
714             renderMachineInstr(os, mi);
715             os << "\n";
716           } else {
717             os << indent << "      &nbsp;\n";
718           }
719           os << indent << "    </td>\n";
720         } else {
721           i = i.getStoreIndex(); // <- Will be incremented to the next index.
722           continue;
723         }
724       }
725
726       if (!ro.regClasses().empty()) {
727         for (MFRenderingOptions::RegClassSet::const_iterator
728                rcItr = ro.regClasses().begin(),
729                rcEnd = ro.regClasses().end();
730              rcItr != rcEnd; ++rcItr) {
731           const TargetRegisterClass *trc = *rcItr;
732
733           os << indent << "    <td class=\"";
734
735           if (trei.getPressureAtSlot(trc, i) == 0) {
736             os << "s-zp";
737           } else if (trei.classOverCapacityAtSlot(trc, i)){
738             os << "s-op";
739           } else {
740             os << "s-up";
741           }
742
743           os << "\"></td>\n";
744         }
745       }
746   
747       // FIXME: Is there a nicer way to insert space between columns in HTML?
748       if (!ro.regClasses().empty() && !ro.intervals().empty())
749         os << indent << "    <td width=2em></td>\n";
750
751       if (!ro.intervals().empty()) {
752         for (MFRenderingOptions::IntervalSet::const_iterator
753                liItr = ro.intervals().begin(),
754                liEnd = ro.intervals().end();
755              liItr != liEnd; ++liItr) {
756           const LiveInterval *li = *liItr;
757           os << indent << "    <td class=\"";
758           switch (getLiveStateAt(li, i)) {
759             case Dead: os << "l-na"; break;
760             case Defined: os << "l-def"; break;
761             case Used: os << "l-use"; break;
762             case AliveReg: os << "l-sar"; break;
763             case AliveStack: os << "l-sas"; break;
764             default: assert(false && "Unrecognised live state."); break;
765           }
766           os << "\"></td>\n";
767         }
768       }
769       os << indent << "  </tr>\n";
770     }
771
772     os << indent << "</table>\n";
773
774     if (!ro.regClasses().empty())
775       renderPressureTableLegend(indent, os);
776   }
777
778   template <typename OStream>
779   void RenderMachineFunction::renderWarnings(const std::string &indent,
780                                              OStream &os) const {
781   }
782
783   template <typename OStream>
784   void RenderMachineFunction::renderFunctionPage(
785                                     OStream &os,
786                                     const char * const renderContextStr) const {
787     os << "<html>\n"
788        << "  <head>\n"
789        << "    <title>" << fqn << "</title>\n";
790
791     insertCSS("    ", os);
792
793     os << "  <head>\n"
794        << "  <body >\n";
795
796     renderFunctionSummary("    ", os, renderContextStr);
797
798     os << "    <br/><br/><br/>\n";
799
800     //renderLiveIntervalInfoTable("    ", os);
801
802     os << "    <br/><br/><br/>\n";
803
804     renderCodeTablePlusPI("    ", os);
805
806     os << "  </body>\n"
807        << "</html>\n";
808   }
809
810   void RenderMachineFunction::getAnalysisUsage(AnalysisUsage &au) const {
811     au.addRequired<SlotIndexes>();
812     au.addRequired<LiveIntervals>();
813     au.setPreservesAll();
814     MachineFunctionPass::getAnalysisUsage(au);
815   }
816
817   bool RenderMachineFunction::runOnMachineFunction(MachineFunction &fn) {
818     mf = &fn;
819     mri = &mf->getRegInfo();
820     tri = mf->getTarget().getRegisterInfo();
821     lis = &getAnalysis<LiveIntervals>();
822     sis = &getAnalysis<SlotIndexes>();
823
824     trei.setup(mf, mri, tri, lis);
825     ro.setup(mf, tri, lis);
826
827     fqn = mf->getFunction()->getParent()->getModuleIdentifier() + "." +
828           mf->getFunction()->getName().str();
829
830     return false;
831   }
832
833   void RenderMachineFunction::releaseMemory() {
834     trei.clear();
835     ro.clear();
836   }
837
838   void RenderMachineFunction::renderMachineFunction(
839                                                    const char *renderContextStr,
840                                                    const VirtRegMap *vrm,
841                                                    const char *renderSuffix) {
842     if (!ro.shouldRenderCurrentMachineFunction())
843       return; 
844
845     this->vrm = vrm;
846     trei.reset();
847
848     std::string rpFileName(mf->getFunction()->getName().str() +
849                            (renderSuffix ? renderSuffix : "") +
850                            outputFileSuffix);
851
852     std::string errMsg;
853     raw_fd_ostream outFile(rpFileName.c_str(), errMsg, raw_fd_ostream::F_Binary);
854
855     renderFunctionPage(outFile, renderContextStr);
856
857     ro.resetRenderSpecificOptions();
858   }
859
860   std::string RenderMachineFunction::escapeChars(const std::string &s) const {
861     return escapeChars(s.begin(), s.end());
862   }
863
864 }