1 //===-- llvm/CodeGen/RenderMachineFunction.cpp - MF->HTML -----s-----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "rendermf"
12 #include "RenderMachineFunction.h"
14 #include "VirtRegMap.h"
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"
32 char RenderMachineFunction::ID = 0;
33 INITIALIZE_PASS_BEGIN(RenderMachineFunction, "rendermf",
34 "Render machine functions (and related info) to HTML pages",
36 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
37 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
38 INITIALIZE_PASS_END(RenderMachineFunction, "rendermf",
39 "Render machine functions (and related info) to HTML pages",
42 static cl::opt<std::string>
43 outputFileSuffix("rmf-file-suffix",
44 cl::desc("Appended to function name to get output file name "
45 "(default: \".html\")"),
46 cl::init(".html"), cl::Hidden);
48 static cl::opt<std::string>
49 machineFuncsToRender("rmf-funcs",
50 cl::desc("Coma seperated list of functions to render"
52 cl::init(""), cl::Hidden);
54 static cl::opt<std::string>
55 pressureClasses("rmf-classes",
56 cl::desc("Register classes to render pressure for."),
57 cl::init(""), cl::Hidden);
59 static cl::opt<std::string>
60 showIntervals("rmf-intervals",
61 cl::desc("Live intervals to show alongside code."),
62 cl::init(""), cl::Hidden);
65 filterEmpty("rmf-filter-empty-intervals",
66 cl::desc("Don't display empty intervals."),
67 cl::init(true), cl::Hidden);
70 showEmptyIndexes("rmf-empty-indexes",
71 cl::desc("Render indexes not associated with instructions or "
73 cl::init(false), cl::Hidden);
76 useFancyVerticals("rmf-fancy-verts",
77 cl::desc("Use SVG for vertical text."),
78 cl::init(true), cl::Hidden);
81 prettyHTML("rmf-pretty-html",
82 cl::desc("Pretty print HTML. For debugging the renderer only.."),
83 cl::init(false), cl::Hidden);
88 bool MFRenderingOptions::renderingOptionsProcessed;
89 std::set<std::string> MFRenderingOptions::mfNamesToRender;
90 bool MFRenderingOptions::renderAllMFs = false;
92 std::set<std::string> MFRenderingOptions::classNamesToRender;
93 bool MFRenderingOptions::renderAllClasses = false;
95 std::set<std::pair<unsigned, unsigned> >
96 MFRenderingOptions::intervalNumsToRender;
97 unsigned MFRenderingOptions::intervalTypesToRender = ExplicitOnly;
99 template <typename OutputItr>
100 void MFRenderingOptions::splitComaSeperatedList(const std::string &s,
102 std::string::const_iterator curPos = s.begin();
103 std::string::const_iterator nextComa = std::find(curPos, s.end(), ',');
104 while (nextComa != s.end()) {
106 std::copy(curPos, nextComa, std::back_inserter(elem));
109 curPos = llvm::next(nextComa);
110 nextComa = std::find(curPos, s.end(), ',');
113 if (curPos != s.end()) {
115 std::copy(curPos, s.end(), std::back_inserter(elem));
121 void MFRenderingOptions::processOptions() {
122 if (!renderingOptionsProcessed) {
124 processRegClassNames();
125 processIntervalNumbers();
126 renderingOptionsProcessed = true;
130 void MFRenderingOptions::processFuncNames() {
131 if (machineFuncsToRender == "*") {
134 splitComaSeperatedList(machineFuncsToRender,
135 std::inserter(mfNamesToRender,
136 mfNamesToRender.begin()));
140 void MFRenderingOptions::processRegClassNames() {
141 if (pressureClasses == "*") {
142 renderAllClasses = true;
144 splitComaSeperatedList(pressureClasses,
145 std::inserter(classNamesToRender,
146 classNamesToRender.begin()));
150 void MFRenderingOptions::processIntervalNumbers() {
151 std::set<std::string> intervalRanges;
152 splitComaSeperatedList(showIntervals,
153 std::inserter(intervalRanges,
154 intervalRanges.begin()));
155 std::for_each(intervalRanges.begin(), intervalRanges.end(),
156 processIntervalRange);
159 void MFRenderingOptions::processIntervalRange(
160 const std::string &intervalRangeStr) {
161 if (intervalRangeStr == "*") {
162 intervalTypesToRender |= All;
163 } else if (intervalRangeStr == "virt-nospills*") {
164 intervalTypesToRender |= VirtNoSpills;
165 } else if (intervalRangeStr == "spills*") {
166 intervalTypesToRender |= VirtSpills;
167 } else if (intervalRangeStr == "virt*") {
168 intervalTypesToRender |= AllVirt;
169 } else if (intervalRangeStr == "phys*") {
170 intervalTypesToRender |= AllPhys;
172 std::istringstream iss(intervalRangeStr);
174 if ((iss >> reg1 >> std::ws)) {
176 intervalNumsToRender.insert(std::make_pair(reg1, reg1 + 1));
180 if (c == '-' && (iss >> reg2)) {
181 intervalNumsToRender.insert(std::make_pair(reg1, reg2 + 1));
183 dbgs() << "Warning: Invalid interval range \""
184 << intervalRangeStr << "\" in -rmf-intervals. Skipping.\n";
188 dbgs() << "Warning: Invalid interval number \""
189 << intervalRangeStr << "\" in -rmf-intervals. Skipping.\n";
194 void MFRenderingOptions::setup(MachineFunction *mf,
195 const TargetRegisterInfo *tri,
197 const RenderMachineFunction *rmf) {
206 void MFRenderingOptions::clear() {
207 regClassesTranslatedToCurrentFunction = false;
210 intervalsTranslatedToCurrentFunction = false;
214 void MFRenderingOptions::resetRenderSpecificOptions() {
216 intervalsTranslatedToCurrentFunction = false;
219 bool MFRenderingOptions::shouldRenderCurrentMachineFunction() const {
222 return (renderAllMFs ||
223 mfNamesToRender.find(mf->getFunction()->getName()) !=
224 mfNamesToRender.end());
227 const MFRenderingOptions::RegClassSet& MFRenderingOptions::regClasses() const{
228 translateRegClassNamesToCurrentFunction();
232 const MFRenderingOptions::IntervalSet& MFRenderingOptions::intervals() const {
233 translateIntervalNumbersToCurrentFunction();
237 bool MFRenderingOptions::renderEmptyIndexes() const {
238 return showEmptyIndexes;
241 bool MFRenderingOptions::fancyVerticals() const {
242 return useFancyVerticals;
245 void MFRenderingOptions::translateRegClassNamesToCurrentFunction() const {
246 if (!regClassesTranslatedToCurrentFunction) {
248 for (TargetRegisterInfo::regclass_iterator rcItr = tri->regclass_begin(),
249 rcEnd = tri->regclass_end();
250 rcItr != rcEnd; ++rcItr) {
251 const TargetRegisterClass *trc = *rcItr;
252 if (renderAllClasses ||
253 classNamesToRender.find(trc->getName()) !=
254 classNamesToRender.end()) {
255 regClassSet.insert(trc);
258 regClassesTranslatedToCurrentFunction = true;
262 void MFRenderingOptions::translateIntervalNumbersToCurrentFunction() const {
263 if (!intervalsTranslatedToCurrentFunction) {
266 // If we're not just doing explicit then do a copy over all matching
268 if (intervalTypesToRender != ExplicitOnly) {
269 for (LiveIntervals::iterator liItr = lis->begin(), liEnd = lis->end();
270 liItr != liEnd; ++liItr) {
271 LiveInterval *li = liItr->second;
273 if (filterEmpty && li->empty())
276 if ((TargetRegisterInfo::isPhysicalRegister(li->reg) &&
277 (intervalTypesToRender & AllPhys))) {
278 intervalSet.insert(li);
279 } else if (TargetRegisterInfo::isVirtualRegister(li->reg)) {
280 if (((intervalTypesToRender & VirtNoSpills) && !rmf->isSpill(li)) ||
281 ((intervalTypesToRender & VirtSpills) && rmf->isSpill(li))) {
282 intervalSet.insert(li);
288 // If we need to process the explicit list...
289 if (intervalTypesToRender != All) {
290 for (std::set<std::pair<unsigned, unsigned> >::const_iterator
291 regRangeItr = intervalNumsToRender.begin(),
292 regRangeEnd = intervalNumsToRender.end();
293 regRangeItr != regRangeEnd; ++regRangeItr) {
294 const std::pair<unsigned, unsigned> &range = *regRangeItr;
295 for (unsigned reg = range.first; reg != range.second; ++reg) {
296 if (lis->hasInterval(reg)) {
297 intervalSet.insert(&lis->getInterval(reg));
303 intervalsTranslatedToCurrentFunction = true;
307 // ---------- TargetRegisterExtraInformation implementation ----------
309 TargetRegisterExtraInfo::TargetRegisterExtraInfo()
310 : mapsPopulated(false) {
313 void TargetRegisterExtraInfo::setup(MachineFunction *mf,
314 MachineRegisterInfo *mri,
315 const TargetRegisterInfo *tri,
316 LiveIntervals *lis) {
323 void TargetRegisterExtraInfo::reset() {
324 if (!mapsPopulated) {
328 mapsPopulated = true;
331 resetPressureAndLiveStates();
334 void TargetRegisterExtraInfo::clear() {
339 //liveStatesMap.clear();
340 mapsPopulated = false;
343 void TargetRegisterExtraInfo::initWorst() {
344 assert(!mapsPopulated && prWorst.empty() && vrWorst.empty() &&
345 "Worst map already initialised?");
347 // Start with the physical registers.
348 for (unsigned preg = 1; preg < tri->getNumRegs(); ++preg) {
349 WorstMapLine &pregLine = prWorst[preg];
351 for (TargetRegisterInfo::regclass_iterator rcItr = tri->regclass_begin(),
352 rcEnd = tri->regclass_end();
353 rcItr != rcEnd; ++rcItr) {
354 const TargetRegisterClass *trc = *rcItr;
356 unsigned numOverlaps = 0;
357 for (TargetRegisterClass::iterator rItr = trc->begin(),
359 rItr != rEnd; ++rItr) {
360 unsigned trcPReg = *rItr;
361 if (tri->regsOverlap(preg, trcPReg))
365 pregLine[trc] = numOverlaps;
369 // Now the register classes.
370 for (TargetRegisterInfo::regclass_iterator rc1Itr = tri->regclass_begin(),
371 rcEnd = tri->regclass_end();
372 rc1Itr != rcEnd; ++rc1Itr) {
373 const TargetRegisterClass *trc1 = *rc1Itr;
374 WorstMapLine &classLine = vrWorst[trc1];
376 for (TargetRegisterInfo::regclass_iterator rc2Itr = tri->regclass_begin();
377 rc2Itr != rcEnd; ++rc2Itr) {
378 const TargetRegisterClass *trc2 = *rc2Itr;
382 for (TargetRegisterClass::iterator trc1Itr = trc1->begin(),
383 trc1End = trc1->end();
384 trc1Itr != trc1End; ++trc1Itr) {
385 unsigned trc1Reg = *trc1Itr;
386 unsigned trc1RegWorst = 0;
388 for (TargetRegisterClass::iterator trc2Itr = trc2->begin(),
389 trc2End = trc2->end();
390 trc2Itr != trc2End; ++trc2Itr) {
391 unsigned trc2Reg = *trc2Itr;
392 if (tri->regsOverlap(trc1Reg, trc2Reg))
395 if (trc1RegWorst > worst) {
396 worst = trc1RegWorst;
401 classLine[trc2] = worst;
407 unsigned TargetRegisterExtraInfo::getWorst(
409 const TargetRegisterClass *trc) const {
410 const WorstMapLine *wml = 0;
411 if (TargetRegisterInfo::isPhysicalRegister(reg)) {
412 PRWorstMap::const_iterator prwItr = prWorst.find(reg);
413 assert(prwItr != prWorst.end() && "Missing prWorst entry.");
414 wml = &prwItr->second;
416 const TargetRegisterClass *regTRC = mri->getRegClass(reg);
417 VRWorstMap::const_iterator vrwItr = vrWorst.find(regTRC);
418 assert(vrwItr != vrWorst.end() && "Missing vrWorst entry.");
419 wml = &vrwItr->second;
422 WorstMapLine::const_iterator wmlItr = wml->find(trc);
423 if (wmlItr == wml->end())
426 return wmlItr->second;
429 void TargetRegisterExtraInfo::initCapacity() {
430 assert(!mapsPopulated && capacityMap.empty() &&
431 "Capacity map already initialised?");
433 for (TargetRegisterInfo::regclass_iterator rcItr = tri->regclass_begin(),
434 rcEnd = tri->regclass_end();
435 rcItr != rcEnd; ++rcItr) {
436 const TargetRegisterClass *trc = *rcItr;
437 unsigned capacity = std::distance(trc->allocation_order_begin(*mf),
438 trc->allocation_order_end(*mf));
441 capacityMap[trc] = capacity;
445 unsigned TargetRegisterExtraInfo::getCapacity(
446 const TargetRegisterClass *trc) const {
447 CapacityMap::const_iterator cmItr = capacityMap.find(trc);
448 assert(cmItr != capacityMap.end() &&
449 "vreg with unallocable register class");
450 return cmItr->second;
453 void TargetRegisterExtraInfo::resetPressureAndLiveStates() {
455 //liveStatesMap.clear();
457 // Iterate over all slots.
460 // Iterate over all live intervals.
461 for (LiveIntervals::iterator liItr = lis->begin(),
463 liItr != liEnd; ++liItr) {
464 LiveInterval *li = liItr->second;
466 const TargetRegisterClass *liTRC;
468 if (TargetRegisterInfo::isPhysicalRegister(li->reg))
471 liTRC = mri->getRegClass(li->reg);
474 // For all ranges in the current interal.
475 for (LiveInterval::iterator lrItr = li->begin(),
477 lrItr != lrEnd; ++lrItr) {
478 LiveRange *lr = &*lrItr;
480 // For all slots in the current range.
481 for (SlotIndex i = lr->start; i != lr->end; i = i.getNextSlot()) {
483 // Record increased pressure at index for all overlapping classes.
484 for (TargetRegisterInfo::regclass_iterator
485 rcItr = tri->regclass_begin(),
486 rcEnd = tri->regclass_end();
487 rcItr != rcEnd; ++rcItr) {
488 const TargetRegisterClass *trc = *rcItr;
490 if (trc->allocation_order_begin(*mf) ==
491 trc->allocation_order_end(*mf))
494 unsigned worstAtI = getWorst(li->reg, trc);
497 pressureMap[i][trc] += worstAtI;
505 unsigned TargetRegisterExtraInfo::getPressureAtSlot(
506 const TargetRegisterClass *trc,
508 PressureMap::const_iterator pmItr = pressureMap.find(i);
509 if (pmItr == pressureMap.end())
511 const PressureMapLine &pmLine = pmItr->second;
512 PressureMapLine::const_iterator pmlItr = pmLine.find(trc);
513 if (pmlItr == pmLine.end())
515 return pmlItr->second;
518 bool TargetRegisterExtraInfo::classOverCapacityAtSlot(
519 const TargetRegisterClass *trc,
521 return (getPressureAtSlot(trc, i) > getCapacity(trc));
524 // ---------- MachineFunctionRenderer implementation ----------
526 void RenderMachineFunction::Spacer::print(raw_ostream &os) const {
529 for (unsigned i = 0; i < ns; ++i) {
534 RenderMachineFunction::Spacer RenderMachineFunction::s(unsigned ns) const {
538 raw_ostream& operator<<(raw_ostream &os, const RenderMachineFunction::Spacer &s) {
543 template <typename Iterator>
544 std::string RenderMachineFunction::escapeChars(Iterator sBegin, Iterator sEnd) const {
547 for (Iterator sItr = sBegin; sItr != sEnd; ++sItr) {
551 case '<': r.append("<"); break;
552 case '>': r.append(">"); break;
553 case '&': r.append("&"); break;
554 case ' ': r.append(" "); break;
555 case '\"': r.append("""); break;
556 default: r.push_back(c); break;
563 RenderMachineFunction::LiveState
564 RenderMachineFunction::getLiveStateAt(const LiveInterval *li,
566 const MachineInstr *mi = sis->getInstructionFromIndex(i);
568 // For uses/defs recorded use/def indexes override current liveness and
569 // instruction operands (Only for the interval which records the indexes).
570 if (i.isUse() || i.isDef()) {
571 UseDefs::const_iterator udItr = useDefs.find(li);
572 if (udItr != useDefs.end()) {
573 const SlotSet &slotSet = udItr->second;
574 if (slotSet.count(i)) {
584 // If the slot is a load/store, or there's no info in the use/def set then
585 // use liveness and instruction operand info.
590 (vrm->getStackSlot(li->reg) == VirtRegMap::NO_STACK_SLOT)) {
596 if (i.isDef() && mi->definesRegister(li->reg, tri)) {
598 } else if (i.isUse() && mi->readsRegister(li->reg)) {
602 (vrm->getStackSlot(li->reg) == VirtRegMap::NO_STACK_SLOT)) {
613 RenderMachineFunction::PressureState
614 RenderMachineFunction::getPressureStateAt(const TargetRegisterClass *trc,
616 if (trei.getPressureAtSlot(trc, i) == 0) {
618 } else if (trei.classOverCapacityAtSlot(trc, i)){
624 /// \brief Render a machine instruction.
625 void RenderMachineFunction::renderMachineInstr(raw_ostream &os,
626 const MachineInstr *mi) const {
628 raw_string_ostream oss(s);
631 os << escapeChars(oss.str());
634 template <typename T>
635 void RenderMachineFunction::renderVertical(const Spacer &indent,
638 if (ro.fancyVerticals()) {
639 os << indent << "<object\n"
640 << indent + s(2) << "class=\"obj\"\n"
641 << indent + s(2) << "type=\"image/svg+xml\"\n"
642 << indent + s(2) << "width=\"14px\"\n"
643 << indent + s(2) << "height=\"55px\"\n"
644 << indent + s(2) << "data=\"data:image/svg+xml,\n"
645 << indent + s(4) << "<svg xmlns='http://www.w3.org/2000/svg'>\n"
646 << indent + s(6) << "<text x='-55' y='10' "
647 "font-family='Courier' font-size='12' "
648 "transform='rotate(-90)' "
649 "text-rendering='optimizeSpeed' "
650 "fill='#000'>" << t << "</text>\n"
651 << indent + s(4) << "</svg>\">\n"
652 << indent << "</object>\n";
654 std::ostringstream oss;
656 std::string tStr(oss.str());
659 for (std::string::iterator tStrItr = tStr.begin(), tStrEnd = tStr.end();
660 tStrItr != tStrEnd; ++tStrItr) {
661 os << *tStrItr << "<br/>";
667 void RenderMachineFunction::insertCSS(const Spacer &indent,
668 raw_ostream &os) const {
669 os << indent << "<style type=\"text/css\">\n"
670 << indent + s(2) << "body { font-color: black; }\n"
671 << indent + s(2) << "table.code td { font-family: monospace; "
672 "border-width: 0px; border-style: solid; "
673 "border-bottom: 1px solid #dddddd; white-space: nowrap; }\n"
674 << indent + s(2) << "table.code td.p-z { background-color: #000000; }\n"
675 << indent + s(2) << "table.code td.p-l { background-color: #00ff00; }\n"
676 << indent + s(2) << "table.code td.p-h { background-color: #ff0000; }\n"
677 << indent + s(2) << "table.code td.l-n { background-color: #ffffff; }\n"
678 << indent + s(2) << "table.code td.l-d { background-color: #ff0000; }\n"
679 << indent + s(2) << "table.code td.l-u { background-color: #ffff00; }\n"
680 << indent + s(2) << "table.code td.l-r { background-color: #000000; }\n"
681 << indent + s(2) << "table.code td.l-s { background-color: #770000; }\n"
682 << indent + s(2) << "table.code th { border-width: 0px; "
683 "border-style: solid; }\n"
684 << indent << "</style>\n";
687 void RenderMachineFunction::renderFunctionSummary(
688 const Spacer &indent, raw_ostream &os,
689 const char * const renderContextStr) const {
690 os << indent << "<h1>Function: " << mf->getFunction()->getName()
692 << indent << "<h2>Rendering context: " << renderContextStr << "</h2>\n";
696 void RenderMachineFunction::renderPressureTableLegend(
697 const Spacer &indent,
698 raw_ostream &os) const {
699 os << indent << "<h2>Rendering Pressure Legend:</h2>\n"
700 << indent << "<table class=\"code\">\n"
701 << indent + s(2) << "<tr>\n"
702 << indent + s(4) << "<th>Pressure</th><th>Description</th>"
703 "<th>Appearance</th>\n"
704 << indent + s(2) << "</tr>\n"
705 << indent + s(2) << "<tr>\n"
706 << indent + s(4) << "<td>No Pressure</td>"
707 "<td>No physical registers of this class requested.</td>"
708 "<td class=\"p-z\"> </td>\n"
709 << indent + s(2) << "</tr>\n"
710 << indent + s(2) << "<tr>\n"
711 << indent + s(4) << "<td>Low Pressure</td>"
712 "<td>Sufficient physical registers to meet demand.</td>"
713 "<td class=\"p-l\"> </td>\n"
714 << indent + s(2) << "</tr>\n"
715 << indent + s(2) << "<tr>\n"
716 << indent + s(4) << "<td>High Pressure</td>"
717 "<td>Potentially insufficient physical registers to meet demand.</td>"
718 "<td class=\"p-h\"> </td>\n"
719 << indent + s(2) << "</tr>\n"
720 << indent << "</table>\n";
723 template <typename CellType>
724 void RenderMachineFunction::renderCellsWithRLE(
725 const Spacer &indent, raw_ostream &os,
726 const std::pair<CellType, unsigned> &rleAccumulator,
727 const std::map<CellType, std::string> &cellTypeStrs) const {
729 if (rleAccumulator.second == 0)
732 typename std::map<CellType, std::string>::const_iterator ctsItr =
733 cellTypeStrs.find(rleAccumulator.first);
735 assert(ctsItr != cellTypeStrs.end() && "No string for given cell type.");
737 os << indent + s(4) << "<td class=\"" << ctsItr->second << "\"";
738 if (rleAccumulator.second > 1)
739 os << " colspan=" << rleAccumulator.second;
744 void RenderMachineFunction::renderCodeTablePlusPI(const Spacer &indent,
745 raw_ostream &os) const {
747 std::map<LiveState, std::string> lsStrs;
748 lsStrs[Dead] = "l-n";
749 lsStrs[Defined] = "l-d";
750 lsStrs[Used] = "l-u";
751 lsStrs[AliveReg] = "l-r";
752 lsStrs[AliveStack] = "l-s";
754 std::map<PressureState, std::string> psStrs;
755 psStrs[Zero] = "p-z";
757 psStrs[High] = "p-h";
761 os << indent << "<table cellpadding=0 cellspacing=0 class=\"code\">\n"
762 << indent + s(2) << "<tr>\n";
764 // Render the header row...
766 os << indent + s(4) << "<th>index</th>\n"
767 << indent + s(4) << "<th>instr</th>\n";
769 // Render class names if necessary...
770 if (!ro.regClasses().empty()) {
771 for (MFRenderingOptions::RegClassSet::const_iterator
772 rcItr = ro.regClasses().begin(),
773 rcEnd = ro.regClasses().end();
774 rcItr != rcEnd; ++rcItr) {
775 const TargetRegisterClass *trc = *rcItr;
776 os << indent + s(4) << "<th>\n";
777 renderVertical(indent + s(6), os, trc->getName());
778 os << indent + s(4) << "</th>\n";
782 // FIXME: Is there a nicer way to insert space between columns in HTML?
783 if (!ro.regClasses().empty() && !ro.intervals().empty())
784 os << indent + s(4) << "<th> </th>\n";
786 // Render interval numbers if necessary...
787 if (!ro.intervals().empty()) {
788 for (MFRenderingOptions::IntervalSet::const_iterator
789 liItr = ro.intervals().begin(),
790 liEnd = ro.intervals().end();
791 liItr != liEnd; ++liItr) {
793 const LiveInterval *li = *liItr;
794 os << indent + s(4) << "<th>\n";
795 renderVertical(indent + s(6), os, li->reg);
796 os << indent + s(4) << "</th>\n";
800 os << indent + s(2) << "</tr>\n";
802 // End header row, start with the data rows...
804 MachineInstr *mi = 0;
807 for (SlotIndex i = sis->getZeroIndex(); i != sis->getLastIndex();
808 i = i.getNextSlot()) {
810 // Render the slot column.
811 os << indent + s(2) << "<tr height=6ex>\n";
813 // Render the code column.
815 MachineBasicBlock *mbb = sis->getMBBFromIndex(i);
816 mi = sis->getInstructionFromIndex(i);
818 if (i == sis->getMBBStartIdx(mbb) || mi != 0 ||
819 ro.renderEmptyIndexes()) {
820 os << indent + s(4) << "<td rowspan=4>" << i << " </td>\n"
821 << indent + s(4) << "<td rowspan=4>\n";
823 if (i == sis->getMBBStartIdx(mbb)) {
824 os << indent + s(6) << "BB#" << mbb->getNumber() << ": \n";
825 } else if (mi != 0) {
826 os << indent + s(6) << " ";
827 renderMachineInstr(os, mi);
829 // Empty interval - leave blank.
831 os << indent + s(4) << "</td>\n";
833 i = i.getStoreIndex(); // <- Will be incremented to the next index.
838 // Render the class columns.
839 if (!ro.regClasses().empty()) {
840 std::pair<PressureState, unsigned> psRLEAccumulator(Zero, 0);
841 for (MFRenderingOptions::RegClassSet::const_iterator
842 rcItr = ro.regClasses().begin(),
843 rcEnd = ro.regClasses().end();
844 rcItr != rcEnd; ++rcItr) {
845 const TargetRegisterClass *trc = *rcItr;
846 PressureState newPressure = getPressureStateAt(trc, i);
848 if (newPressure == psRLEAccumulator.first) {
849 ++psRLEAccumulator.second;
851 renderCellsWithRLE(indent + s(4), os, psRLEAccumulator, psStrs);
852 psRLEAccumulator.first = newPressure;
853 psRLEAccumulator.second = 1;
856 renderCellsWithRLE(indent + s(4), os, psRLEAccumulator, psStrs);
859 // FIXME: Is there a nicer way to insert space between columns in HTML?
860 if (!ro.regClasses().empty() && !ro.intervals().empty())
861 os << indent + s(4) << "<td width=2em></td>\n";
863 if (!ro.intervals().empty()) {
864 std::pair<LiveState, unsigned> lsRLEAccumulator(Dead, 0);
865 for (MFRenderingOptions::IntervalSet::const_iterator
866 liItr = ro.intervals().begin(),
867 liEnd = ro.intervals().end();
868 liItr != liEnd; ++liItr) {
869 const LiveInterval *li = *liItr;
870 LiveState newLiveness = getLiveStateAt(li, i);
872 if (newLiveness == lsRLEAccumulator.first) {
873 ++lsRLEAccumulator.second;
875 renderCellsWithRLE(indent + s(4), os, lsRLEAccumulator, lsStrs);
876 lsRLEAccumulator.first = newLiveness;
877 lsRLEAccumulator.second = 1;
880 renderCellsWithRLE(indent + s(4), os, lsRLEAccumulator, lsStrs);
882 os << indent + s(2) << "</tr>\n";
885 os << indent << "</table>\n";
887 if (!ro.regClasses().empty())
888 renderPressureTableLegend(indent, os);
891 void RenderMachineFunction::renderFunctionPage(
893 const char * const renderContextStr) const {
895 << s(2) << "<head>\n"
896 << s(4) << "<title>" << fqn << "</title>\n";
900 os << s(2) << "<head>\n"
901 << s(2) << "<body >\n";
903 renderFunctionSummary(s(4), os, renderContextStr);
905 os << s(4) << "<br/><br/><br/>\n";
907 //renderLiveIntervalInfoTable(" ", os);
909 os << s(4) << "<br/><br/><br/>\n";
911 renderCodeTablePlusPI(s(4), os);
913 os << s(2) << "</body>\n"
917 void RenderMachineFunction::getAnalysisUsage(AnalysisUsage &au) const {
918 au.addRequired<SlotIndexes>();
919 au.addRequired<LiveIntervals>();
920 au.setPreservesAll();
921 MachineFunctionPass::getAnalysisUsage(au);
924 bool RenderMachineFunction::runOnMachineFunction(MachineFunction &fn) {
927 mri = &mf->getRegInfo();
928 tri = mf->getTarget().getRegisterInfo();
929 lis = &getAnalysis<LiveIntervals>();
930 sis = &getAnalysis<SlotIndexes>();
932 trei.setup(mf, mri, tri, lis);
933 ro.setup(mf, tri, lis, this);
934 spillIntervals.clear();
938 fqn = mf->getFunction()->getParent()->getModuleIdentifier() + "." +
939 mf->getFunction()->getName().str();
944 void RenderMachineFunction::releaseMemory() {
947 spillIntervals.clear();
952 void RenderMachineFunction::rememberUseDefs(const LiveInterval *li) {
954 if (!ro.shouldRenderCurrentMachineFunction())
957 for (MachineRegisterInfo::reg_iterator rItr = mri->reg_begin(li->reg),
958 rEnd = mri->reg_end();
959 rItr != rEnd; ++rItr) {
960 const MachineInstr *mi = &*rItr;
961 if (mi->readsRegister(li->reg)) {
962 useDefs[li].insert(lis->getInstructionIndex(mi).getUseIndex());
964 if (mi->definesRegister(li->reg)) {
965 useDefs[li].insert(lis->getInstructionIndex(mi).getDefIndex());
970 void RenderMachineFunction::rememberSpills(
971 const LiveInterval *li,
972 const std::vector<LiveInterval*> &spills) {
974 if (!ro.shouldRenderCurrentMachineFunction())
977 for (std::vector<LiveInterval*>::const_iterator siItr = spills.begin(),
978 siEnd = spills.end();
979 siItr != siEnd; ++siItr) {
980 const LiveInterval *spill = *siItr;
981 spillIntervals[li].insert(spill);
982 spillFor[spill] = li;
986 bool RenderMachineFunction::isSpill(const LiveInterval *li) const {
987 SpillForMap::const_iterator sfItr = spillFor.find(li);
988 if (sfItr == spillFor.end())
993 void RenderMachineFunction::renderMachineFunction(
994 const char *renderContextStr,
995 const VirtRegMap *vrm,
996 const char *renderSuffix) {
997 if (!ro.shouldRenderCurrentMachineFunction())
1003 std::string rpFileName(mf->getFunction()->getName().str() +
1004 (renderSuffix ? renderSuffix : "") +
1008 raw_fd_ostream outFile(rpFileName.c_str(), errMsg, raw_fd_ostream::F_Binary);
1010 renderFunctionPage(outFile, renderContextStr);
1012 ro.resetRenderSpecificOptions();
1015 std::string RenderMachineFunction::escapeChars(const std::string &s) const {
1016 return escapeChars(s.begin(), s.end());